C# - Finding All Indices of a Substring [duplicate]

▼魔方 西西 提交于 2019-12-11 14:14:31

问题


EDIT: So it turns out what I had before was correct, I was simply counting the indexes wrong. Thank you for your input though.

Working on a method that finds all substring indices in a given string from the user. I am having issues with getting correct positions from userString.IndexOf. I know it's finding all occurrences of the substring, but the integer index is off by a substantial amount.

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

I think it may be an issue with position += userString.IndexOf(userSubString, position); but am not entirely sure how I would go about setting the new start position while maintaing an accurate record of the substring locations.


回答1:


Remove the += in front of position

   position = userString.IndexOf(userSubString, position);

Also you should change your code to save the initial found position and set the position variable to search after the previous one

    // 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) + ", ";
    }

As a final note, if this search produces many hits it is better to change the string concatenation using a StringBuilder class instance

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



回答2:


A concise way to find these indexes using 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") will find you the indexes 1 and 3.




回答3:


You have another problem here. Let's say you call:

getSubStringPositions("abcabcabcabc", "abcabc");

You function will incorrectly report that the string occurs twice, when in fact the substring occurs 3 times, like so:

  • abcabc.abcabc
  • abc.abcabc.abc <-- your function jumps over this one
  • abcabc.abcabc.


来源:https://stackoverflow.com/questions/19596639/c-sharp-finding-all-indices-of-a-substring

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!