Home
Manage Your Code
Snippet: Find Occurence index(s) of substring within a string (C#)
Title: Find Occurence index(s) of substring within a string Language: C#
Description: method to find number of occurences of a string and index of, within another string Views: 397
Author: Adam Emrick Date Added: 9/18/2007
Copy Code  
1 private static int[] StringContainsIndexOf(string subString, string fullString)
2        {
3            ArrayList arrList = new ArrayList();
4            for (int i = 0; i < fullString.Length; i++)
5            {
6                int loc = fullString.IndexOf(subString, i);
7                if (loc == -1)
8                    break;
9
10                arrList.Add(loc);
11                i = loc;
12            }
13
14            int[] output = new int[arrList.Count];
15            for (int i = 0; i < arrList.Count; i++)
16                output[i] = (int)arrList[i];
17
18            return output;
19        }