简体   繁体   中英

F# Seq<Seq<char>> convert to string

Playing with F# and trying to get from string abcdefghijklmnop another string aeimbfjncgkodhlp .

let input ="abcdefghijklmnop"

let convertList (input:string)=
    input
    |> Seq.mapi(fun i ei->  i % 4, ei)
    |> Seq.groupBy (fst)
    |> Seq.map(fun (g,s)-> s |> Seq.map snd)

let result = convertList input

result

In my function final result is seq<seq<char>> , how to convert seq<seq<char>> to string?

In F#, the string type implements the seq<_> interface, which means that you can treat strings as sequences of characters, but getting the other way round is sadly a bit ugly.

One option is to turn seq<seq<char>> to just one seq<char> using Seq.concat , then convert it to seq<string> which is something you can then concatenate using String.concat :

result |> Seq.concat |> Seq.map string |> String.concat ""

Probably a more efficient, but less elegant option is to convert seq<char> to array<char> which you can then pass to System.String constructor:

result |> Seq.concat |> Seq.toArray |> System.String

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM