开发者

Concatenate array of strings, and prepending another string with F#

I have a string array lines, for example

lines = [| "hello"; "world" |]

I'd like to make a string line that concatenates the elements in lines with "code=" string prepended. For example, I need to get the string code="helloworld" from the lines array.

I could get the concatenated string with this code

let concatenatedLine = lines |> String.concat "" 

And I tested this code to prepend "code=" string as follows, but I got error FS0001: The type 'string' is not compatible with the type 'seq<string>' error.

let concatenatedLine = "code=" + lines |> String.concat "" 

What'开发者_开发知识库s wrong with this?


+ binds stronger than |>, so you need to add some parentheses:

let concatenatedLine = "code=" + (lines |> String.concat "")

Otherwise the compiler parses the expression like:

let concatenatedLine = (("code=" + lines) |> (String.concat ""))
                         ^^^^^^^^^^^^^^^
                         error FS0001 


I think you want to try the following ( forward piping operator has lower precedence)

let concatenatedLine = "code=" + (lines |> String.concat "" )
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜