Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save erdembayar/3b1bc6d52b6e057f3bee4b4c75fc0cb8 to your computer and use it in GitHub Desktop.
Save erdembayar/3b1bc6d52b6e057f3bee4b4c75fc0cb8 to your computer and use it in GitHub Desktop.
//List<int> indexes = "fooStringfooBar".AllIndexesOf("foo");
//List<int> indexes2 = "fooStringfoofooBar".AllIndexesOf("foo");
//List<int> indexes3 = "oooStringoooBar".AllIndexesOf("ooo");
//List<int> indexes4 = "oooStringooooooBar".AllIndexesOf("ooo");
//List<int> indexes5 = "ooooStringooooooBar".AllIndexesOf("ooo");
public static List<int> AllIndexesOf(this string str,char c, int allowedNum)
{
var value = new String(c, allowedNum);
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;
if (index > 0 && str[index-1] == value[0])
{
continue;
}
indexes.Add(index);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment