【问题标题】:C# - Finding All Indices of a Substring [duplicate]C# - 查找子字符串的所有索引
【发布时间】:2013-11-04 23:21:07
【问题描述】:

编辑:所以事实证明我之前的数据是正确的,我只是计算索引错误。不过还是感谢您的意见。

使用一种方法来查找用户给定字符串中的所有子字符串索引。我在从 userString.IndexOf 获取正确位置时遇到问题。我知道它正在查找所有出现的子字符串,但整数索引有很大的偏差。

private static void getSubStringPositions(string userString, string userSubString)
{
    string subStringPositions = "";
    int position = 0;

    if (userString.IndexOf(userSubString, position) == -1)
    {
        Console.WriteLine("Your sub-string was not found in your string.\n");
        return;
    }
    while (position < userString.Length)
    {
        position += userString.IndexOf(userSubString, position);
        subStringPositions += Convert.ToString(position) + ", ";
    }

    Console.WriteLine("The occurernce(s) for your sub-string are: " + subStringPositions + "\n\n");
    return;
}

我认为这可能是position += userString.IndexOf(userSubString, position); 的问题,但我不完全确定如何设置新的起始位置,同时保持子字符串位置的准确记录。

【问题讨论】:

  • 找到的位置是相对于基本字符串的,而不是相对于起始位置的。

标签: c# string loops indexing indices


【解决方案1】:

使用 LINQ 查找这些索引的简洁方法:

public static IEnumerable<int> FindIndexes(string text, string query)
{
    return Enumerable.Range(0, text.Length - query.Length)
        .Where(i => query.Equals(text.Substring(i, query.Length));
}

FindIndexes("abcbcbc", "bcb") 会找到索引13

【讨论】:

    【解决方案2】:

    这里还有一个问题。假设您致电:

    getSubStringPositions("abcabcabcabc", "abcabc");

    您的函数会错误地报告字符串出现两次,而实际上子字符串出现了 3 次,如下所示:

    • abcabc.abcabc
    • abc.abcabc.abc
    • abcabc.abcabc.

    【讨论】:

      【解决方案3】:

      去掉位置前面的+=

         position = userString.IndexOf(userSubString, position);
      

      您还应该更改代码以保存最初找到的位置并将位置变量设置为在前一个之后搜索

          // Initial check...
          position = userString.IndexOf(userSubString);
          if(position == -1)
          {
              Console.WriteLine("Your sub-string was not found in your string.\n");
              return;
          }
          // Save the found position and enter the loop
          subStringPositions = Convert.ToString(position) + ", ";
      
          while (position < userString.Length)
          {
              // Search restart from the character after the previous found substring
              position = userString.IndexOf(userSubString, position + 1);
              subStringPositions += Convert.ToString(position) + ", ";
          }
      

      最后一点,如果此搜索产生很多命中,最好使用StringBuilder 类实例更改字符串连接

          StringBuilder subStringPositions = new StringBuilder();
          subStringPositions.Append(Convert.ToString(position) + ", ");
      
          while (position < userString.Length)
          {
              // Search restart from the character after the previous found substring
              position = userString.IndexOf(userSubString, position + 1);
              subStringPositions.Append(Convert.ToString(position) + ", ";
          }
          Console.WriteLine("The occurrence(s) for your sub-string are: " + 
                            subStringPositions.ToString() + "\n\n");
      

      【讨论】:

      • 给我我需要的澄清。非常感谢您的宝贵时间。
      猜你喜欢
      • 1970-01-01
      • 2012-05-19
      • 2012-05-29
      • 1970-01-01
      • 2012-11-15
      • 2021-12-18
      • 1970-01-01
      • 2021-09-09
      • 2015-12-31
      相关资源
      最近更新 更多