我有一个字符串
string str="hello";
这是我的数组
string[] myarr = new string[] {"good","Hello", "this" , "new"};
我需要从数组中找到“hello”的索引(不使用循环)
所以我用过
int index = Array.IndexOf(myarr, str);
这会返回 -1,但我期望结果为 1。
我什至尝试过 StringComparer.OrdinalIgnoreCase
但无济于事。
希望有人能帮忙。谢谢。
最佳答案
注意!!标记的答案可能有一些问题,例如
string array[] = {"hello", "hi", "bye" , "welcome" , "hell"}
如果你使用答案中描述的相同方法找到单词“hell”的索引
Int Indexofary = Array.FindIndex(array, t => t.IndexOf("hell", StringComparison.InvariantCultureIgnoreCase) >=0);
您将得到结果 Indexofary = 0 而不是 4。
而不是使用
Array.FindIndex(array, t => t.Equals("hell", StringComparison.InvariantCultureIgnoreCase));
得到正确的结果。
规则 位
关于c# - 如何向 Array.IndexOf 添加不区分大小写的选项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8935161/