How to ignore the case sensitivity in List<string> while calculating of an index of an entry
本问题已经有最佳答案,请猛点这里访问。
假设我有这个代码:
1 2 3 4 5 6 | private readonly static List<string> ExtPos = new List<string> {".dat",".wef"}; private static int ExtToPos(string ext) { return ExtPos.IndexOf(ext /*, StringComparer.InvariantCultureIgnoreCase*/); } |
如何在内容搜索中忽略字母大小写?
谢谢
您可以使用findindex:
1 | int ix = ExtPos.FindIndex(x =>".DAT".Equals(x, StringComparison.CurrentCultureIgnoreCase)); |
或者你也可以使用字符串比较器:它对
1 | int ix = ExtPos.FindIndex(x => StringComparer.CurrentCultureIgnoreCase.Equals(".DAT", x)); |
使用findindex方法。这样地:
1 2 3 4 5 | private static int ExtToPos(string ext) { return ExtPos.FindIndex(_ => string.Equals(_, ext, StringComparison.InvariantCultureIgnoreCase)); } |