Fsharp-strings
F#-文字列
F#では、文字列型は不変テキストをUnicode文字のシーケンスとして表します。
文字列リテラル
文字列リテラルは、引用符( ")文字で区切られます。
改行、タブなどの特別な用途のためにいくつかの特殊文字があります。 バックスラッシュ(\)文字を使用してエンコードされます。 バックスラッシュ文字と関連文字は、エスケープシーケンスを作成します。 次の表は、F#でサポートされているエスケープシーケンスを示しています。
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) |
エスケープシーケンスを無視する方法
次の2つの方法により、コンパイラはエスケープシーケンスを無視します-
- @記号を使用します。
- 文字列を三重引用符で囲みます。
文字列リテラルの前に@記号が付いている場合、それは* verbatim文字列*と呼ばれます。その方法では、2つの引用符文字が1つの引用符文字として解釈されることを除いて、文字列内のすべてのエスケープシーケンスは無視されます。
文字列が三重引用符で囲まれている場合、二重引用符文字を含むすべてのエスケープシーケンスも無視されます。
例
次の例は、XMLまたは埋め込まれた引用符を含む他の構造を操作する方法を示すこの手法を示しています-
//Using a verbatim string
let xmldata = @"<book author = ""Lewis, C.S"" title = ""Narnia"">"
printfn "%s" xmldata
あなたがプログラムをコンパイルして実行すると、次の出力が得られます-
<book author = "Lewis, C.S" title = "Narnia">
文字列の基本的な演算子
次の表は、文字列の基本的な操作を示しています-
Value | Description |
---|---|
collect : (char → string) → string → string | Creates a new string whose characters are the results of applying a specified function to each of the characters of the input string and concatenating the resulting strings. |
concat : string → seq<string> → string | Returns a new string made by concatenating the given strings with a separator. |
exists : (char → bool) → string → bool | Tests if any character of the string satisfies the given predicate. |
forall : (char → bool) → string → bool | Tests if all characters in the string satisfy the given predicate. |
init : int → (int → string) → string | Creates a new string whose characters are the results of applying a specified function to each index and concatenating the resulting strings. |
iter : (char → unit) → string → unit | Applies a specified function to each character in the string. |
iteri : (int → char → unit) → string → unit | Applies a specified function to the index of each character in the string and the character itself. |
length : string → int | Returns the length of the string. |
map : (char → char) → string → string | Creates a new string whose characters are the results of applying a specified function to each of the characters of the input string. |
mapi : (int → char → char) → string → string | Creates a new string whose characters are the results of applying a specified function to each character and index of the input string. |
replicate : int → string → string | Returns a string by concatenating a specified number of instances of a string. |
次の例は、上記の機能のいくつかの使用を示しています-
例1
String.collect関数は、指定された関数を入力文字列の各文字に適用し、結果の文字列を連結した結果の文字である新しい文字列を作成します。
let collectTesting inputS =
String.collect (fun c -> sprintf "%c " c) inputS
printfn "%s" (collectTesting "Happy New Year!")
あなたがプログラムをコンパイルして実行すると、次の出力が得られます-
H a p p y N e w Y e a r !
例2
String.concat関数は、指定された文字列のシーケンスをセパレータで連結し、新しい文字列を返します。
let strings = [ "Tutorials Point"; "Coding Ground"; "Absolute Classes" ]
let ourProducts = String.concat "\n" strings
printfn "%s" ourProducts
あなたがプログラムをコンパイルして実行すると、次の出力が得られます-
Tutorials Point
Coding Ground
Absolute Classes
実施例3
String.replicateメソッドは、指定された数の文字列のインスタンスを連結して文字列を返します。
printfn "%s" <| String.replicate 10 "*! "
あなたがプログラムをコンパイルして実行すると、次の出力が得られます-
*! *! *! *! *! *! *! *! *! *!