字串Strings

注意

本文中的 API 參考連結將帶您前往 MSDN。The API reference links in this article will take you to MSDN. docs.microsoft.com API 參考不完整。The docs.microsoft.com API reference is not complete.

string類型以 Unicode 字元序清單示不可變的文字。The string type represents immutable text as a sequence of Unicode characters. stringSystem.String 在 .NET Framework 中的別名。string is an alias for System.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. 下表顯示F#字串常值中支援的 Escape 序列。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
垂直 TabVertical tab \v
反斜線Backslash \\
引號Quotation mark \"
省略號Apostrophe \'
Unicode 字元Unicode character \DDD(其中D表示十進位數; 範圍為 000-255, \231例如 = "ç")\DDD (where D indicates a decimal digit; range of 000 - 255; for example, \231 = "ç")
Unicode 字元Unicode character \xHH(其中H表示十六進位數位; 00-FF 的範圍, \xE7例如 = "ç")\xHH (where H indicates a hexadecimal digit; range of 00 - FF; for example, \xE7 = "ç")
Unicode 字元Unicode character \uHHHH(UTF-16)(其中H表示十六進位數位; 0000-FFFF 的範圍; 例如, \u00E7 = "ç")\uHHHH (UTF-16) (where H indicates a hexadecimal digit; range of 0000 - FFFF; for example, \u00E7 = "ç")
Unicode 字元Unicode character \U00HHHHHH(UTF-32)(其中H表示十六進位數位; 000000-10ffff 且的範圍; 例如, \U0001F47D = "👽")\U00HHHHHH (UTF-32) (where H indicates a hexadecimal digit; range of 000000 - 10FFFF; for example, \U0001F47D = "👽")

重要

\DDD Escape 序列是十進位標記法,而非八進位標記法,就像大多數其他語言一樣。The \DDD escape sequence is decimal notation, not octal notation like in most other languages. 因此,數位89有效\032 ,而的序列則代表空格(U + 0020),而八進位標記法中的相同程式碼點則為\040Therefore, digits 8 and 9 are valid, and a sequence of \032 represents a space (U+0020), whereas that same code point in octal notation would be \040.

注意

受限於 0-255 (0xff)的範圍, \DDD\x escape 序列實際上是ISO-8859-1字元集,因為這符合第一個 256 Unicode 程式碼點。Being constrained to a range of 0 - 255 (0xFF), the \DDD and \x escape sequences are effectively the ISO-8859-1 character set, since that matches the first 256 Unicode code points.

如果前面加上 @ 符號,常值就是逐字字串。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.

此外,字串可以用三個引號括住。Additionally, a string may be enclosed by triple quotes. 在此情況下,會忽略所有的逸出序列,包括雙引號字元。In this case, all escape sequences are ignored, including double quotation mark characters. 若要指定包含內嵌加上引號之字串的字串,您可以使用逐字字串或以三個引號括住的字串。To specify a string that contains an embedded quoted string, you can either use a verbatim string or a triple-quoted string. 如果您使用逐字字串,您必須指定兩個引號字元來表示單一引號字元。If you use a verbatim string, you must specify two quotation mark characters to indicate a single quotation mark character. 如果您使用三加引號的字串,您可以使用單引號字元,而不將它們剖析為字串的結尾。If you use a triple-quoted string, you can use the single quotation mark characters without them being parsed as the end of the string. 當您使用包含內嵌引號的 XML 或其他結構時,這項技術會很有用。This technique can be useful when you work with XML or other structures that include embedded quotation marks.

// Using a verbatim string
let xmlFragment1 = @"<book author=""Milton, John"" title=""Paradise Lost"">"

// Using a triple-quoted string
let xmlFragment2 = """<book author="Milton, John" title="Paradise Lost">"""

在程式碼中,會接受具有分行符號的字串,而且分行符號會以逐字的方式轉譯為分行符號,除非反斜線字元是分行符號前的最後一個字元。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 white space on the next line is ignored when the backslash character is used. 下列程式碼會產生具有str1"abc\ndef"的字串,以及具有str2"abcdef"的字串。The following code produces a string str1 that has value "abc\ndef" and a string str2 that has value "abcdef".

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

您可以使用類似陣列的語法來存取字串中的個別字元,如下所示。You can access individual characters in a string by using array-like syntax, as follows.

printfn "%c" str1.[1]

輸出為 bThe 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

您可以依不帶正負號的位元組陣列來表示byte[]ASCII 字串,類型為。You can represent ASCII strings by arrays of unsigned bytes, type byte[]. 您可以將尾碼B新增至字串常值,以表示它是 ASCII 字串。You add the suffix B to a string literal to indicate that it is an ASCII string. 搭配位元組陣列使用的 ASCII 字串常值支援與 Unicode 字串相同的轉義順序,但 Unicode 逸出序列除外。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. +運算子會維持與 .NET Framework 字串處理功能的相容性。The + operator maintains compatibility with the .NET Framework string handling features.

下列範例說明字串串連。The following example illustrates string concatenation.

let string1 = "Hello, " + "world"

String 類別String Class

因為中F#的字串類型實際上是 .NET Framework System.String System.String類型,所以所有成員都可以使用。Because the string type in F# is actually a .NET Framework System.String type, all the System.String members are available. 這包括+運算子,用來串連字號串Length 、屬性和Chars屬性(property),這會以 Unicode 字元陣列的形式傳回字串。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. 如需字串的詳細資訊, System.String請參閱。For more information about strings, see System.String.

藉由使用CharsSystem.String屬性,您可以藉由指定索引來存取字串中的個別字元,如下列程式碼所示。By using the Chars property of System.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

字串處理的其他功能會包含在String FSharp.Core命名空間的模組中。Additional functionality for string handling is included in the String module in the FSharp.Core namespace. 如需詳細資訊,請參閱Core. 字串模組For more information, see Core.String Module.

另請參閱See also