A string is essentially a sequence of characters, as it implements IEnumerable<char>
. Internally, a string is an array of char values. That array is exposed through the Chars indexer.
You can use any Seq
method on a string, eg :
"abc" |> Seq.iter (fun x->printfn "%c" x)
Individual characters are also available
You can also use the optimized functions of the String module :
"abc" |> String.iter (fun x->printfn "%c" x)
The String
module uses the methods of the String
class to improve performance. For example, the String.length function is an alias for the String.Length
property, so it doesn't have to iterate over all characters like Seq
would do:
let length (str:string) =
let str = emptyIfNull str
str.Length
Other functions, like String.iter use the Chars
indexer directly :
let iter (f : (char -> unit)) (str:string) =
let str = emptyIfNull str
for i = 0 to str.Length - 1 do
f str.[i]