The string type represents immutable text as a sequence of Unicode characters. string is an alias for String in the .NET Framework.
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.
\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".
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.
The output is b.
Or you can extract substrings by using array slice syntax, as shown in the following code.
The output is as follows.
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.
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.
Additional functionality for string handling is included in the String module in the FSharp.Core namespace. For more information, see Core.String Module (F#).