有没有比以下代码更简洁的方式在F#中执行不区分大小写的字符串比较?
System.String.Equals("test", "TeSt", System.StringComparison.CurrentCultureIgnoreCase)
System.String.Equals("test", "TeSt", System.StringComparison.CurrentCultureIgnoreCase)
此外,您还可以使用F#类型扩展机制:
> type System.String with
- member s1.icompare(s2: string) =
- System.String.Equals(s1, s2, System.StringComparison.CurrentCultureIgnoreCase);;
> "test".icompare "tEst";;
val it : bool = true
对于任何感兴趣的人,这是一个部分的活动模式:
let (|InvariantEqualI|_|) (str:string) arg =
if String.Compare(str, arg, StringComparison.InvariantCultureIgnoreCase) = 0
then Some() else None
let (|OrdinalEqualI|_|) (str:string) arg =
if String.Compare(str, arg, StringComparison.OrdinalIgnoreCase) = 0
then Some() else None
// Contains substring
(str1.ToUpper()).Contains (str2.ToUpper())
// Equality
(str1.ToUpper()).Contains (str2.ToUpper())