27

How do I split string into a list of chars in F sharp for example if I want to split the word "Hello" into a list of chars ie.

"Hello" ->['H';'e';'l';'l';'o']

I have tried Split([| |]) but it only splits a string depending on the parameter u pass in.

I have tried this to but it still did not work

let splitChar (text:string) = 
    [for c in text ->c] 

let splitChar (text:string) = 
    [for c in text do yield c] 
1
  • 2
    You don't need to split anything. The characters are already available through the String.Chars property Aug 25, 2017 at 10:25

4 Answers 4

31

You can use Seq.toList to convert a string to a list of characters:

Seq.toList "Hello"
4
  • 3
    That works because a string is already a sequence of characters. There's no need to use Seq.toList to work with individual characters Aug 25, 2017 at 10:28
  • 1
    True, and it would be helpful for the question to be clarified so that we know why OP wants specifically a list as output. The result of Seq.toList "Hello" gives a char list as output Aug 25, 2017 at 10:33
  • When I use seq.toLIst the compiler complains that "The field constructor toList is not defined
    – Ivan
    Aug 25, 2017 at 10:37
  • 1
    It should be a capital S in Seq. Also, the first instance has a capital I in toLIst in your example, when it should be capitalized as toList Aug 25, 2017 at 10:38
13

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] 
6

In .NET, you can use String.ToCharArray method. To get back into a String, you can use String(Char[]) now that F# using constructors as functions.

"Hello".ToCharArray() |> List.ofArray

It may be better performance to just use the F# Array module. I'm guessing List.ofArray is more efficient than List.ofSeq. If that doesn't matter, similar to Chad's answer, the F# idiomatic way is:

"Hello" |> List.ofSeq
5

I've noticed occasions where I would initially think to split something into a char array or a list first - but where I should be more succinct.

let msg = "hello-world"

let resultA = 
    msg.[0..0].ToUpper() + msg.[1..].Replace('-', ' ')

let resultB = 
    Seq.toList (msg)
    |> (fun list -> (string list.Head).ToUpper() + (String.Concat(list.Tail)).Replace('-', ' '))

// val resultA : string = "Hello world"
// val resultB : string = "Hello world"

The 'resultA' way is nicer I think.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.