当我期待它为-1时,我的IndexOf结果产生了积极的结果
我正在搜索的字符串类似于
string source = "|BLAH|BLAH|RESULTS|BLAH|BLAH|";
我的搜索字符串是
string toFind = "|RESULTS�|";
它包含一些不应该存在的未知字符,但是。我期待这一行返回-1
int indexFound = source.IndexOf(toFind, System.StringComparison.CurrentCultureIgnoreCase);
这是返回字符串| RESULTS |的索引。 谁知道为什么?或者如何克服这个?我想要完全匹配。
答案 0 :(得分:1)
文档说明可忽略的字符或在进行文化敏感的比较时被忽略。
试试这个
int indexFound = source.IndexOf(toFind, System.StringComparison.OrdinalIgnoreCase);