F#允许我们使用+(加号)运算符连接两个字符串。
let StringExample = let s = "Hello" s+" "+"FSharp" printf "%s" StringExample
输出:
链接:https://www.learnfk.comhttps://www.learnfk.com/fsharp/f-sharp-string-built-in-functions.html
来源:LearnFk无涯教程网
Hello FSharp
let StringExample (s1:string):string = printf "%s" (s1.Substring(2)) s1.[2..6] // You can get substring by passing starting and ending index. printf "%s" (StringExample "F Sharp")
输出:
链接:https://www.learnfk.comhttps://www.learnfk.com/fsharp/f-sharp-string-built-in-functions.html
来源:LearnFk无涯教程网
Sharp Sharp
let StringExample (s1:string) (index :int) = s1.Chars(index) let result = StringExample "FSharp" 5 printf "%c" result
输出:
链接:https://www.learnfk.comhttps://www.learnfk.com/fsharp/f-sharp-string-built-in-functions.html
来源:LearnFk无涯教程网
p
在F#中,我们可以使用equals()方法或比较(=)运算符来比较两个字符串。
let stringExample = let s1:string = "hello"; let s2:string = "hello"; let s3:string = "csharp"; printfn "%b" (s1.Equals(s2)) printfn "%b" (s1.Equals(s3)) printfn "%b" (s1=s2) printfn "%b" (s1=s3)
输出:
链接:https://www.learnfk.comhttps://www.learnfk.com/fsharp/f-sharp-string-built-in-functions.html
来源:LearnFk无涯教程网
true false true false
The Contains () method in F# searches the specified string in the given string.
let StringExample = let s1:string = "Hello FSharp" printfn "%b" (s1.Contains("FS")) printfn "%b" (s1.Contains(".Net"))
输出:
链接:https://www.learnfk.comhttps://www.learnfk.com/fsharp/f-sharp-string-built-in-functions.html
来源:LearnFk无涯教程网
true false
F#的Trim方法删除开始和结束空格。
let StringExample = let s:string = "Hello" printfn "%s" (s+" FSharp") printfn "%s"(s.Trim()+"FSharp")
输出:
链接:https://www.learnfk.comhttps://www.learnfk.com/fsharp/f-sharp-string-built-in-functions.html
来源:LearnFk无涯教程网
Hello FSharp HelloFSharp
祝学习愉快!(内容编辑有误?请选中要编辑内容 -> 右键 -> 修改 -> 提交!)