如何向Array.IndexOf添加不区分大小写的选项

我有一个字符串

string str="hello"; 

这是我的arrays

 string[] myarr = new string[] {"good","Hello", "this" , "new"}; 

我需要从数组中找到“hello”的索引(不使用循环)

所以我用过

 int index = Array.IndexOf(myarr, str); 

这返回-1,但我期待结果为1。

我甚至尝试使用StringComparer.OrdinalIgnoreCase但无济于事。

希望有人能提供帮助。 谢谢。

既然你正在寻找索引。 试试这种方式。

 Array.FindIndex(myarr, t => t.IndexOf(str, StringComparison.InvariantCultureIgnoreCase) >=0); 

意识到 !! 标记的答案可能有一些问题,比如

 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)); 

得到正确的结果。

Rrgards比特

Array.IndexOf调用区分大小写的默认“Equals”方法。 试试这个:

 Array.FindIndex(myarr, t => t.Equals(str, StringComparison.InvariantCultureIgnoreCase)) 
 var result = myarr.FindIndex(s => s.Equals(str, StringComparison.OrdinalIgnoreCase)); 

1)如果你只想搜索一次并想保留源数组,你可以使用:

  public static void example1() { string[] myarr = { "good", "Hello", "this", "new" }; var str = "new"; var res= Array.FindIndex(myarr, x=>string.Equals(x, str, StringComparison.InvariantCultureIgnoreCase)); } 

2)如果你多次搜索,最好使用它:

 public static void example1() { string[] myarr = {"good", "Hello", "this", "new"}; var str = "new"; var res = Array.IndexOf(Array.ConvertAll(myarr, ToStringlowerCase), str.ToLowerInvariant()); } 

3)上面的答案是错误的:

 string array[] = {"hello", "hi", "bye" , "welcome" , "hell"} Int Indexofary = Array.FindIndex(array, t => t.IndexOf("hell", StringComparison.InvariantCultureIgnoreCase) >=0); 

根本不会工作,因为它不搜索字符串,而是搜索子字符串。

算法迭代数组中的单词,当第一个单词“你好”时,算法试图找到’地狱’的索引,这个索引是1. 1是>然后是0,算法将完成而不去其他单词。

如果您不想搜索子字符串但想要搜索字符串,则应修复algorythm。 该算法可以通过添加检查子字符串从第1个字符开始来t.IndexOf(str, StringComparison.InvariantCultureIgnoreCase) == 0并且字的长度等于str.Length == t.Length 。 固定:

  public static int example3() { string[] myarr = { "hHello", "hi", "bye", "welcome", "hell" }; var str = "hell"; return Array.FindIndex(myarr, t => t.IndexOf(str, StringComparison.InvariantCultureIgnoreCase) == 0 && str.Length == t.Length); } 

我有一个类似的问题,我需要项目的索引,但它必须不区分大小写,我在网上看了几分钟,什么都没找到,所以我只是写了一个小方法来完成它,这就是我做:

 private static int getCaseInvariantIndex(List ItemsList, string searchItem) { List lowercaselist = new List(); foreach (string item in ItemsList) { lowercaselist.Add(item.ToLower()); } return lowercaselist.IndexOf(searchItem.ToLower()); } 

将此代码添加到同一文件中,并将其调用如下:

 int index = getCaseInvariantIndexFromList(ListOfItems, itemToFind); 

希望这有帮助,祝你好运!

由于Array.IndexOf是通用的,因此制作通用扩展函数是有意义的:

 public static int IndexOf(this T[] source, T value) { return IndexOf(source, value, StringComparison.InvariantCultureIgnoreCase); } public static int IndexOf(this T[] source, T value, StringComparison stringComparison) { if (typeof(T) == typeof(string)) return Array.FindIndex(source, m => m.ToString().Equals(value.ToString(), stringComparison)); else return Array.IndexOf(source, value); }