Find Occurence index(s) of substring within a string

Settembre 19th, 2007

Description: method to find number of occurences of a string and index of, within another string

Link: http://www.codekeep.net/snippets/240bbe37-3efd-413f-bd42-aefdbb2e3451.aspx

 private static int[] StringContainsIndexOf(string subString, string fullString)
        {
            ArrayList arrList = new ArrayList();
            for (int i = 0; i < fullString.Length; i++)
            {
                int loc = fullString.IndexOf(subString, i);
                if (loc == -1)
                    break;

                arrList.Add(loc);
                i = loc;
            }

            int[] output = new int[arrList.Count];
            for (int i = 0; i < arrList.Count; i++)
                output[i] = (int)arrList[i];

            return output;
        }

  • .NET
1 Star2 Stars3 Stars4 Stars5 Stars (No Ratings Yet)
Loading ... Loading ...

Trackback URI | Comments RSS

Leave a Reply

You must be logged in to post a comment.