2011-05-31 50 views
1

我有一個字符串的陣列線,例如串聯陣列串,和預先計算另一個字符串與F#

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

我想使該串接中的元素用線串行「代碼=」字符串預先。例如,我需要從行數組中獲取字符串code="helloworld"

我能得到的連接字符串與此代碼

let concatenatedLine = lines |> String.concat "" 

我測試此代碼前面加上「代碼=」串如下,但我得到了error FS0001: The type 'string' is not compatible with the type 'seq<string>'錯誤。

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

這是怎麼回事?

回答

5

+結合比|>強,所以你需要添加一些括號:

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

否則編譯器解析表達式,如:

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

我想你想嘗試以下(向前管道操作員具有較低的優先權)

let concatenatedLine = "code=" + (lines |> String.concat "") 
相關問題