F # Convert String Array to String

C # I can use string.Join("", lines)to convert a string array to a string. What can I do to do the same with F #?

ADDED

I need to read lines from a file, perform some operation, and then combine all the lines into one 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

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

Decision

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 ""
+5
source share
3 answers

to use String.concat?

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

Edition:

in your code replace File.WriteAllLines with File.WriteAllText

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

open System.IO

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

Copied from the fsi console window:

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

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

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

val s : string = "Hello, World!"

>

EDIT:

String.Join .NET Framework, , , String.concat F #. , - , .

, , , , String.concat , , String.Join F #.

+4
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"
0
source

All Articles