简体   繁体   English

如何在 C# 中的字符串数组中搜索子字符串

[英]How to search a Substring in String array in C#

How to search for a Substring in String array?如何在字符串数组中搜索子字符串? I need to search for a Substring in the string array.我需要在字符串数组中搜索一个子字符串。 The string can be located in any part of the array (element) or within an element.字符串可以位于数组(元素)的任何部分或元素内。 (middle of a string) I have tried : Array.IndexOf(arrayStrings,searchItem) but searchItem has to be the EXACT match to be found in arrayStrings. (字符串的中间)我试过: Array.IndexOf(arrayStrings,searchItem)但 searchItem 必须是在 arrayStrings 中找到的完全匹配。 In my case searchItem is a portion of a complete element in arrayStrings.在我的情况下,searchItem 是 arrayStrings 中完整元素的一部分。

string [] arrayStrings = {
   "Welcome to SanJose",
   "Welcome to San Fancisco","Welcome to New York", 
   "Welcome to Orlando", "Welcome to San Martin",
   "This string has Welcome to San in the middle of it" 
};
lineVar = "Welcome to San"
int index1 = 
   Array.IndexOf(arrayStrings, lineVar, 0, arrayStrings.Length);
// index1 mostly has a value of -1; string not found

I need to check whether lineVar variable is present in arrayStrings.我需要检查 arrayStrings 中是否存在 lineVar 变量。 lineVar can be of different length and value. lineVar 可以具有不同的长度和值。

What would be the best way to find this substring within an array string?在数组字符串中查找此子字符串的最佳方法是什么?

If all you need is a bool true/false answer as to whether the lineVar exists in any of the strings in the array, use this:如果您只需要一个布尔值真/假答案,以确定lineVar是否存在于数组中的任何字符串中,请使用以下命令:

 arrayStrings.Any(s => s.Contains(lineVar));

If you need an index, that's a bit trickier, as it can occur in multiple items of the array.如果您需要索引,那就有点棘手了,因为它可能出现在数组的多个项目中。 If you aren't looking for a bool, can you explain what you need?如果你不是在寻找 bool,你能解释一下你需要什么吗?

Old school:老套:

int index = -1;

for(int i = 0; i < arrayStrings.Length; i++){
   if(arrayStrings[i].Contains(lineVar)){
      index = i;
      break;
   }
}

If you need all the indexes:如果您需要所有索引:

List<Tuple<int, int>> indexes = new List<Tuple<int, int>>();

for(int i = 0; i < arrayStrings.Length; i++){
   int index = arrayStrings[i].IndexOf(lineVar);
   if(index != -1)
     indexes.Add(new Tuple<int, int>(i, index)); //where "i" is the index of the string, while "index" is the index of the substring
}

If you need the Index of the first element that contains the substring in the array elements, you can do this...如果您需要包含数组元素中子字符串的第一个元素的索引,您可以这样做...

int index = Array.FindIndex(arrayStrings, s => s.StartsWith(lineVar, StringComparison.OrdinalIgnoreCase)) // Use 'Ordinal' if you want to use the Case Checking.

If you need the element's value that contains the substring, just use the array with the index you just got, like this...如果您需要包含子字符串的元素值,只需将数组与您刚刚获得的索引一起使用,就像这样......

string fullString = arrayStrings[index];

Note: The above code will find the first occurrence of the match.注意:上面的代码将找到匹配项的第一次出现。 Similary, you can use Array.FindLastIndex() method if you want the last element in the array that contains the substring.类似地,如果您想要包含子字符串的数组中的最后一个元素,则可以使用 Array.FindLastIndex() 方法。

You will need to convert the array to a List<string> and then use the ForEach extension method along with Lambda expressions to get each element that contains the substring.您需要将数组转换为List<string> ,然后使用ForEach扩展方法和 Lambda 表达式来获取包含子字符串的每个元素。

To find the substrings in the String array using C#使用 C# 在 String 数组中查找子字符串

    List<string> searchitem = new List<string>();
    string[] arrayStrings = {
       "Welcome to SanJose",
       "Welcome to San Fancisco","Welcome to New York",
       "Welcome to Orlando", "Welcome to San Martin",
       "This string has Welcome to San in the middle of it"
    };
   string searchkey = "Welcome to San";
   for (int i = 0; i < arrayStrings.Length; i++)
   {
    if (arrayStrings[i].Contains(searchkey))//checking whether the searchkey contains in the string array
    {
     searchitem.Add(arrayStrings[i]);//adding the matching item to the list 
    }
   string searchresult = string.Join(Environment.NewLine, searchitem);

Output for Searchresult:搜索结果的输出:

Welcome to SanJose欢迎来到圣何塞

Welcome to San Fancisco欢迎来到旧金山

Welcome to San Martin欢迎来到圣马丁

This string has Welcome to San in the middle of it这个字符串中间有 Welcome to San

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM