F#将String Array转换为String

pro*_*eek 7 string f#

使用C#,我可以使用string.Join("", lines)将字符串数组转换为字符串.用F#做同样的事情我能做些什么?

添加

我需要从文件中读取行,执行一些操作,然后将所有行连接成一行.

当我运行此代码时

open System.IO
open String

let lines = 
  let re = System.Text.RegularExpressions.Regex(@"#(\d+)")
  [|for line in File.ReadAllLines("tclscript.do") ->
      re.Replace(line.Replace("{", "{{").Replace("}", "}}").Trim(), "$1", 1)|]

let concatenatedLine = String.Join("", lines)

File.WriteAllLines("tclscript.txt", concatenatedLine)
Run Code Online (Sandbox Code Playgroud)

我收到了这个错误

error FS0039: The value or constructor 'Join' is not defined
Run Code Online (Sandbox Code Playgroud)

我尝试使用此代码let concatenatedLine = lines |> String.concat ""来获取此错误

error FS0001: This expression was expected to have type
    string []    
but here has type
    string
Run Code Online (Sandbox Code Playgroud)

open System.IO
open System 

let lines = 
  let re = System.Text.RegularExpressions.Regex(@"#(\d+)")
  [|for line in File.ReadAllLines("tclscript.do") ->
      re.Replace(line.Replace("{", "{{").Replace("}", "}}"), "$1", 1) + @"\n"|]

let concatenatedLine = String.Join("", lines)
File.WriteAllText("tclscript.txt", concatenatedLine)
Run Code Online (Sandbox Code Playgroud)

这一个也有效.

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

des*_*sco 15

使用String.concat?

["a"; "b"]
|> String.concat ", " // "a, b"
Run Code Online (Sandbox Code Playgroud)

编辑:

在你的代码替换File.WriteAllLinesFile.WriteAllText

let concatenatedLine = 
    ["a"; "b"]
    |> String.concat ", "

open System.IO

let path = @"..."
File.WriteAllText(path, concatenatedLine)
Run Code Online (Sandbox Code Playgroud)


pho*_*oog 5

从fsi控制台窗口复制:

> open System;;
> let stringArray = [| "Hello"; "World!" |];;

val stringArray : string [] = [|"Hello"; "World!"|]

> let s = String.Join(", ", stringArray);;

val s : string = "Hello, World!"

>
Run Code Online (Sandbox Code Playgroud)

编辑:

与使用F#核心库中的String.concat相比,使用.NET框架类库中的String.Join当然要少。我只能假定这就是为什么有人否决了我的答案的原因,因为那个人没有扩大解释投票的礼貌。

正如我在下面的评论中提到的那样,我发布此答案的原因是,在所有其他答案中都使用String.concat可能会误导普通读者以为String.Join在F#中根本不可用。