本文概述
- F#字符串串联示例
- F#订阅示例
- F#使用Char()方法示例获取给定索引的Char
- F#字符串比较示例
- F#字符串包含示例
- F#字符串修剪()示例
let StringExample = let s = "Hello" s+" "+"FSharp"printf "%s" StringExample
输出:
Hello FSharp
F#订阅示例
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")
输出:
SharpSharp
F#使用Char()方法示例获取给定索引的Char
let StringExample (s1:string) (index :int) = s1.Chars(index)let result = StringExample "FSharp" 5printf "%c" result
输出:
p
F#字符串比较示例在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)
输出:
truefalsetruefalse
F#字符串包含示例F#中的Contains()方法在给定字符串中搜索指定的字符串。
let StringExample = let s1:string = "Hello FSharp" printfn "%b" (s1.Contains("FS")) printfn "%b" (s1.Contains(".Net"))
输出:
truefalse
F#字符串修剪()示例F#的Trim方法删除开头和结尾的空格。
let StringExample = let s:string = "Hello" printfn "%s" (s+" FSharp") printfn "%s"(s.Trim()+"FSharp")
【F#字符串内置函数】输出:
Hello FSharpHelloFSharp