Share via


Strings (F#)

The string type represents immutable text as a sequence of Unicode characters. string is an alias for String in the .NET Framework.

Remarks

String literals are delimited by the quotation mark (") character. The backslash character (\) is used to encode certain special characters. The backslash and the next character together are known as an escape sequence. Escape sequences supported in F# string literals are shown in the following table.

Character

Escape sequence

Backspace

\b

Newline

\n

Carriage return

\r

Tab

\t

Backslash

\\

Quotation mark

\"

Apostrophe

\'

Unicode character

\uXXXX or \UXXXXXXXX (where X indicates a hexadecimal digit)

If preceded by the @ symbol, the literal is a verbatim string. This means that any escape sequences are ignored, except that two quotation mark characters are interpreted as one quotation mark character.

In code, strings that have line breaks are accepted and the line breaks are interpreted literally as newlines, unless a backslash character is the last character before the line break. Leading whitespace on the next line is ignored when the backslash character is used. The following code produces a string str1 that has value "abc\r\n def" and a string str2 that has value "abcdef".

let str1 = "abc
     def"
let str2 = "abc\
     def"

When working with strings, F# preserves the line break characters just as they are found, so whether you are reading a string from a file with line feed (LF or \n) or carriage return and line feed (CRLF or \r\n) as the newline character, they are preserved.

You can access individual characters in a string by using array-like syntax, as follows.

printfn "%c" str1.[1]

The output is b.

Or you can extract substrings by using array slice syntax, as shown in the following code.

printfn "%s" (str1.[0..2])
printfn "%s" (str2.[3..5])

The output is as follows.

abc
def

You can represent ASCII strings by arrays of unsigned bytes, type byte[]. You add the suffix B to a string literal to indicate that it is an ASCII string. ASCII string literals used with byte arrays support the same escape sequences as Unicode strings, except for the Unicode escape sequences.

// "abc" interpreted as a Unicode string.
let str1 : string = "abc"
// "abc" interpreted as an ASCII byte array. 
let bytearray : byte[] = "abc"B 

String Operators

There are two ways to concatenate strings: by using the + operator or by using the ^ operator. The + operator maintains compatibility with the .NET Framework string handling features.

The following example illustrates string concatenation.

let string1 = "Hello, " + "world"

String Class

Because the string type in F# is actually a .NET Framework String type, all the String members are available. This includes the + operator, which is used to concatenate strings, the Length property, and the Chars property, which returns the string as an array of Unicode characters. For more information about strings, see String.

By using the Chars property of String, you can access the individual characters in a string by specifying an index, as is shown in the following code.

let printChar (str : string) (index : int) =
    printfn "First character: %c" (str.Chars(index))

String Module

Additional functionality for string handling is included in the String module in the FSharp.Core namespace. For more information, see Core.String Module (F#).

See Also

Reference

String

Other Resources

F# Language Reference