【问题标题】:How to search a Substring in String array in C#如何在 C# 中搜索字符串数组中的子字符串
【发布时间】:2023-04-05 19:22:02
【问题描述】:

如何在字符串数组中搜索子字符串?我需要在字符串数组中搜索一个子字符串。字符串可以位于数组(元素)的任何部分或元素内。 (字符串中间)我尝试过:Array.IndexOf(arrayStrings,searchItem) 但 searchItem 必须是在 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

我需要检查 arrayStrings 中是否存在 lineVar 变量。 lineVar 可以有不同的长度和值。

在数组字符串中找到该子字符串的最佳方法是什么?

【问题讨论】:

    标签: c# arrays substring


    【解决方案1】:

    如果您只需要一个布尔真/假答案来判断 lineVar 是否存在于数组中的任何字符串中,请使用以下命令:

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

    如果您需要索引,那就有点棘手了,因为它可能出现在数组的多个项目中。如果您不是在寻找布尔值,您能解释一下您需要什么吗?

    【讨论】:

      【解决方案2】:

      老派:

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

      如果你需要所有的索引:

      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
      }
      

      【讨论】:

        【解决方案3】:

        如果需要数组元素中包含子串的第一个元素的索引,可以这样做...

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

        如果您需要包含子字符串的元素值,只需将数组与您刚刚获得的索引一起使用,就像这样...

        string fullString = arrayStrings[index];
        

        注意:上面的代码会找到匹配的第一个匹配项。同理,你 如果你想要数组中的最后一个元素,可以使用 Array.FindLastIndex() 方法 包含子字符串。

        您需要将数组转换为 List&lt;string&gt;,然后使用 ForEach 扩展方法和 Lambda 表达式来获取包含子字符串的每个元素。

        【讨论】:

          【解决方案4】:

          使用 C# 查找字符串数组中的子字符串

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

          搜索结果的输出:

          欢迎来到圣何塞

          欢迎来到旧金山

          欢迎来到圣马丁

          这个字符串中间有 Welcome to San

          【讨论】:

            猜你喜欢
            • 2022-01-23
            • 2011-07-04
            • 1970-01-01
            • 2011-07-22
            • 2013-01-09
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多