gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-04 09:37:26 25 4
gpt4 key购买 nike

使用 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)

我收到这个错误
error FS0039: The value or constructor 'Join' is not defined

我试过这个代码 let concatenatedLine = lines |> String.concat ""得到这个错误
error FS0001: This expression was expected to have type
string []
but here has type
string

解决方案
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)

这个也有效。
let concatenatedLine = lines |> String.concat ""

最佳答案

使用 String.concat 吗?

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

编辑:

在你的代码中用 File.WriteAllText 替换 File.WriteAllLines
let concatenatedLine = 
["a"; "b"]
|> String.concat ", "

open System.IO

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

关于string - F# 将字符串数组转换为字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6157369/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com