简体   繁体   English

F# 将字符串数组转换为字符串

[英]F# Convert String Array to String

With C#, I can use string.Join("", lines) to convert string array to string.使用 C#,我可以使用string.Join("", lines)将字符串数组转换为字符串。 What can I do to do the same thing with F#?我可以用 F# 做同样的事情吗?

ADDED添加

I need to read lines from a file, do some operation, and then concatenate all the lines into a single line.我需要从文件中读取行,执行一些操作,然后将所有行连接成一行。

When I run this code当我运行这段代码

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)

I got this error我收到了这个错误

error FS0039: The value or constructor 'Join' is not defined

I tried this code let concatenatedLine = lines |> String.concat "" to get this error我试过这段代码let concatenatedLine = lines |> String.concat ""得到这个错误

error FS0001: This expression was expected to have type
    string []    
but here has type
    string

Solution解决方案

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)

and this one also works.这一个也有效。

let concatenatedLine = lines |> String.concat ""

use String.concat?使用 String.concat?

["a"; "b"]
|> String.concat ", " // "a, b"

EDITED:编辑:

in your code replace File.WriteAllLines with File.WriteAllText在您的代码中将 File.WriteAllLines替换为File.WriteAllText

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

open System.IO

let path = @"..."
File.WriteAllText(path, concatenatedLine)

Copied from an fsi console window:从 fsi 控制台 window 复制:

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

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

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

val s : string = "Hello, World!"

>

EDIT:编辑:

Using String.Join from the .NET framework class library is, of course, less idiomatic than using String.concat from the F# core library.当然,使用 .NET 框架 class 库中的 String.Join 比使用 F# 核心库中的 String.concat 更不习惯。 I can only presume that is why someone voted my answer down, since that person did not extend the courtesy of explaining the vote.我只能假设这就是为什么有人拒绝我的回答,因为那个人没有提供解释投票的礼貌。

The reason I posted this answer, as I mentioned in my comment below, is that the use of String.concat in all of the other answers might mislead casual readers into thinking that String.Join is not at all available in F#.正如我在下面的评论中提到的那样,我发布此答案的原因是,在所有其他答案中使用 String.concat 可能会误导普通读者认为 String.Join 在 F# 中根本不可用。

List.ofSeq "abcd" |> List.toArray |> (fun s -> System.String s) |> printfn "%A"
List.ofSeq "abcd" |> List.toArray |> (fun s -> new System.String(s)) |> printfn "%A"

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

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