The string type represents immutable text as a sequence of Unicode characters. The string
is an alias for System.String
in .NET.
The string declaration and initialization can be done in different ways.
You can declare the string variable as shown below.
let str1:string = ""
You can declare and initialize a string variable to null
.
let str2:string = null
To declare and initialize a string variable with a regular string literal.
let sqlServerPath:string = "C:\\Program Files (x86)\\Microsoft SQL Server";
To declare and initialize a string variable with a verbatim string literal.
let vsPath:string = @"C:\Program Files (x86)\Microsoft Visual Studio\2019\Community";
In local variables, you can use implicit typing.
let str3 = "It is a sample message of a strongly-typed System.String!";
You can concatenate multiple string variables using the +
operator.
let firstName = "John "
let lastName = "Doe"
let fullName = firstName + lastName
Console.WriteLine(fullName)
You can extract a substring from any string by specifying starting and end index as shown below.
let str4 = "It is a sample message of a strongly-typed System.String!";
Console.WriteLine(str4.[0..21])// Substring from index 0 to 21
Console.WriteLine(str4.[43..56])// Substring from index 43 to 56
It will display the following substring on the console.
It is a sample message
System.String!
You can use the Equals()
method or comparison (=
) operator to compare two strings.
let s1:string = "Hello";
let s2:string = "World";
let s3:string = "Hello";
Console.WriteLine(s1.Equals(s2))
Console.WriteLine(s1.Equals(s3))
printfn "%b" (s1=s2)
printfn "%b" (s1=s3)
String literals are delimited by the quotation mark ("
) character.
\
) is used to encode certain special characters.Escape sequences supported in F# string literals are shown in the following table.
Character | Escape Sequence |
---|---|
Alert | \a |
Backspace | \b |
Form feed | \f |
Newline | \n |
Carriage return | \r |
Tab | \t |
Vertical tab | \v |
Backslash | \\ |
Quotation mark | \" |
Apostrophe | \' |