简体   繁体   English

F#序列 <Seq<char> &gt;转换为字符串

[英]F# Seq<Seq<char>> convert to string

Playing with F# and trying to get from string abcdefghijklmnop another string aeimbfjncgkodhlp . 玩F#并尝试从字符串abcdefghijklmnop获取另一个字符串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? 在我的函数中,最终结果是seq<seq<char>> ,如何将seq<seq<char>>转换为字符串?

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. 在F#中, string类型实现了seq<_>接口,这意味着您可以将字符串视为字符序列,但是seq<_>则有点丑陋。

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 : 一种选择是使用Seq.concatseq<seq<char>>变成一个seq<char> ,然后将其转换为seq<string> ,然后可以使用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: 可能更有效但不太优雅的选择是将seq<char>转换为array<char> ,然后可以将其传递给System.String构造函数:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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