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

Posted by Tommy on Stack Overflow See other posts from Stack Overflow or by Tommy
Published on 2010-04-14T21:52:47Z Indexed on 2010/04/14 22:13 UTC
Read the original article Hit count: 232

Filed under:
|
|
|

Alright, so what i have, is a large string i need to parse, and what i need to happen, is 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 index's would be added to the List. and the List would contain 0 and the other index whatever it would be.

Ive been playing around, and the string.IndexOf does almost what i'm looking for, and ive written some code. But i cant 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?

© Stack Overflow or respective owner

Related posts about c#

Related posts about string-manipulation