caesay caesay - 3 months ago 31
ASP.NET (C#) Question

Finding ALL positions of a substring in a large string in C#

I have a large string I need to parse, and I need to find all the instances of

extract"(me,i-have lots. of]punctuation
, and store them to a list.

So say this piece of string was in the beginning and middle of the larger string, both of them would be found, and their indexes would be added to the
List
. and the
List
would contain
0
and the other index whatever it would be.

I've been playing around, and the
string.IndexOf
does almost what I'm looking for, and I've written some code. But I can't seem to get it to work:

List<int> inst = new List<int>();
int index = 0;
while (index < source.LastIndexOf("extract\"(me,i-have lots. of]punctuation", 0) + 39)
{
int src = source.IndexOf("extract\"(me,i-have lots. of]punctuation", index);
inst.Add(src);
index = src + 40;
}



  • inst
    = The list

  • source
    = The large string



Any better ideas?

Answer

Here's an example extension method for it:

public static List<int> AllIndexesOf(this string str, string value) {
    if (String.IsNullOrEmpty(value))
        throw new ArgumentException("the string to find may not be empty", "value");
    List<int> indexes = new List<int>();
    for (int index = 0;; index += value.Length) {
        index = str.IndexOf(value, index);
        if (index == -1)
            return indexes;
        indexes.Add(index);
    }
}

If you put this into a static class and import the namespace with using, it appears as a method on any string, and you can just do:

List<int> indexes = "fooStringfooBar".AllIndexesOf("foo");

For more information on extension methods, http://msdn.microsoft.com/en-us/library/bb383977.aspx

Also the same using an iterator:

public static IEnumerable<int> AllIndexesOf(this string str, string value) {
    if (String.IsNullOrEmpty(value))
        throw new ArgumentException("the string to find may not be empty", "value");
    for (int index = 0;; index += value.Length) {
        index = str.IndexOf(value, index);
        if (index == -1)
            break;
        yield return index;
    }
}
Comments