F#不区分大小写的字符串比较
在F#中是否有一种语法上比下面更干净的方法来进行不区分大小写的字符串比较F#不区分大小写的字符串比较,f#,F#,在F#中是否有一种语法上比下面更干净的方法来进行不区分大小写的字符串比较 System.String.Equals("test", "TeSt", System.StringComparison.CurrentCultureIgnoreCase) 写一个扩展方法来缩短它怎么样。另外,你可以使用力学: > type System.String with - member s1.icompare(s2: string) = - System.String.Equals(s1, s
System.String.Equals("test", "TeSt", System.StringComparison.CurrentCultureIgnoreCase)
写一个扩展方法来缩短它怎么样。另外,你可以使用力学:
> 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
很好的建议,只是为了扩展您的答案,我使用了下面的let(=?)s1s2=System.String.Equals(s1,s2,System.StringComparison.CurrentCultureInoRecase)
// Contains substring
(str1.ToUpper()).Contains (str2.ToUpper())
// Equality
(str1.ToUpper()).Contains (str2.ToUpper())