连接字符串数组,并在F#之前添加另一个字符串

pro*_*eek 1 string f# concatenation

例如,我有一个字符串数组行

lines = [| "hello"; "world" |]
Run Code Online (Sandbox Code Playgroud)

我想创建一个字符串行,将行中的元素连接起来,前面加上"code ="字符串.例如,我需要code="helloworld"从lines数组中获取字符串.

我可以用这段代码得到连接的字符串

let concatenatedLine = lines |> String.concat "" 
Run Code Online (Sandbox Code Playgroud)

我测试了这段代码,在前面添加了"code ="字符串,但是我收到了error FS0001: The type 'string' is not compatible with the type 'seq<string>'错误.

let concatenatedLine = "code=" + lines |> String.concat "" 
Run Code Online (Sandbox Code Playgroud)

这有什么问题?

Ste*_*sen 5

+绑定比强|>,所以你需要添加一些括号:

let concatenatedLine = "code=" + (lines |> String.concat "")
Run Code Online (Sandbox Code Playgroud)

否则编译器会解析表达式,如:

let concatenatedLine = (("code=" + lines) |> (String.concat ""))
                         ^^^^^^^^^^^^^^^
                         error FS0001 
Run Code Online (Sandbox Code Playgroud)