What are F# strings?

Key takeaways:

  • F# string is a sequence of Unicode characters enclosed in quotes "" and cannot be changed once declared.

  • F# strings provide string interpolation using $, allowing expressions to be embedded directly in strings.

  • Strings can be accessed using indexes and sliced ​​using ranges in F#.

  • F# allows the use of verbatim strings (@) for special characters and triple-quoted strings (""") for multiline text, simplifying string handling.

  • F# provides various built-in functions for string manipulation, such as String.length, String.Contains, casing conversions, etc.

F# is a programming language that runs on .NET. Just like any other programming language, strings are one of the fundamental data types in F#. In this Answer, we’ll understand F# strings with examples.

Basics of F# strings

A string is a sequence of Unicode characters inside quotation marks "". In F#, we can’t change it once we make a string. It stays the way it was when we created it.

let greeting = "Hello, World!"
printfn "%s" greeting
Basic string

In the above code example, we declared a variable named greeting with the string value "Hello, World!" and then printed that string to the console using printfn with the format specifier %s which indicates that the corresponding argument should be formatted as a string.

String interpolation

F# has string interpolation using $. This enables us to embed expressions directly in strings:

let age = 25
let output = $"My age is {age}"

printfn "%s" output
String interpolation

In the above code example, we declared an integer variable age with the value 25, used string interpolation to create a string output with the value "My age is 25", and printed that string to the console using printfn with the format specifier %s.

String formatting

String formatting is the process of constructing strings in a specific format, often by inserting variable values or expressions into predefined patterns. In F#, string formatting can be accomplished using the Printf.sprintf function:

let pi = 3.14159
let output = Printf.sprintf "The value of pi is %f" pi

printfn "%s" output
String formatting

In the code above, we made a variable called pi and gave it the value 3.14159. Then, we used Printf.sprintf to make a message that says, "The value of pi is 3.141590". After that, we showed this message on the screen using printfn with the format %s, which is used to print text.

String indexing

String indexing refers to accessing a specific character in a string by its index. In F#, we can use the .[index] notation to achieve this:

let string1 = "abc"
printfn "%c" string1.[1] 
String indexing and slicing

In the above code, string1.[1] accesses the character at index 1 (which is zero-based) in the string "abc", resulting in the character 'b'.

String slicing

String slicing allows us to extract a portion of a string by specifying a range of indices. In F#, we can use the .[start..end] notation for slicing:

let string2 = "Hello, World!"
let slice = string2.[0..4] 
printfn "%s" slice
String slicing

In the above code, string2.[0..4] slices the string "Hello, World!" from index 0 to 4, inclusive, resulting in the substring "Hello".

Verbatim string

We can use a string with special characters like quotes or backslashes without escaping them using a verbatim string, followed by @ prefix.

let verbatimString = @"This is a verbatim string containing ""double quotes"" and \backslashes\."

printfn "%s" verbatimString
Verbatim string

In the above code, we declared a verbatim string variable verbatimString with the value @"This is a verbatim string containing ""double quotes"" and \backslashes\." and then printed that string to the console using printfn with the format specifier %s.

Note: The double quotation marks "" inside the verbatim string are used to represent a single double quotation mark " within that string.

Triple-quoted string

A triple-quoted string allows us to include multiline text without needing escape characters, such as backslashes or newline characters. In F#, triple-quoted strings are enclosed within three double quotation marks """ """.

let tripleQuotedString = """
    This is a triple-quoted string
    that can span multiple lines
    and includes "double quotes" without escaping.
    """

printfn "%s" tripleQuotedString
Triple-quoted string

In the above code, we declared a triple-quoted string variable tripleQuotedString containing multiple lines of text without the need for escape characters for line breaks or double quotes, and then printed that string to the console using printfn with the format specifier %s.

Note: Triple-quoted strings handle double quotation marks differently from verbatim strings. In a triple-quoted string, double quotation marks " " can be included directly without the need for escaping.

String operators

To concatenate strings, we can also use + operator.

let string1 = "Hello, " + "world"

printfn "%s" string1
String operator

In the above code, we concatenated two string literals "Hello, " and "world" using the + operator, assigned the result to the variable string1, and then printed the value of string1 to the console using printfn with the format specifier %s.

Functions

Finally, F# has functions for string manipulation. A few of these are provided below:

  • String.length: Length of the string

  • String.Contains: Checks for a substring in the string

  • String.StartsWith and String.EndsWith: Checks if a string starts or ends with a substring

  • String.ToUpper() and String.ToLower(): Convert to uppercase or lowercase

let sample = "F# is cool!"

let length = String.length sample
let contain = sample.Contains "cool"
let starts = sample.StartsWith "F#"
let ends = sample.EndsWith "cool!"
let upperCaseSample = sample.ToUpper()
let lowerCaseSample = sample.ToLower()

printfn "Length: %i" length
printfn "Contains 'cool': %b" contain
printfn "Starts with 'F#': %b" starts
printfn "Ends with 'cool!': %b" ends
printfn "Uppercase: %s" upperCaseSample
printfn "Lowercase: %s" lowerCaseSample
String functions

In the above code, we first assigned the string "F# is cool!" to the variable named sample. Then, we computed various properties of the string sample and printed the results using printfn.

  • Line 3: We determined the length of the string using String.length and stored the result in the variable length.

  • Line 4: We checked if the string contains the substring cool using the Contains method and stored the result in the variable contain.

  • Line 5: We checked if the string starts with F# using the StartsWith method and stored the result in the variable starts.

  • Line 6: We checked if the string ends with cool! using the EndsWith method and stored the result in the variable ends.

  • Lines 7–8: We converted the string into uppercase and stored it in a variable named upperCaseSample. Conversely, we converted the string into lowercase and stored it in a variable named lowerCaseSample.

Finally, we printed the results using printfn with appropriate format specifiers.

Conclusion

F# offers various operations and functions for handling strings efficiently and conveniently, from simple string concatenation to advanced features like string interpolation, verbatim strings, and triple-quoted strings. Understanding F# strings allows developers to write cleaner and more expressive code.

Free Resources

Copyright ©2025 Educative, Inc. All rights reserved