开发者

F# case insensitive string compare

Is there a syntactically cleaner way to preform a case insensitive string compari开发者_开发知识库son in F# than the following

System.String.Equals("test", "TeSt", System.StringComparison.CurrentCultureIgnoreCase)


Also, you can use F# type extensions mechanics:

> type System.String with
-   member s1.icompare(s2: string) =
-     System.String.Equals(s1, s2, System.StringComparison.CurrentCultureIgnoreCase);;
> "test".icompare "tEst";;
val it : bool = true


How about writing an extension method to make this shorter.


For any interested, a partial active pattern for this:

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())
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜