Last active
August 23, 2018 19:16
-
-
Save erdembayar/3b1bc6d52b6e057f3bee4b4c75fc0cb8 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//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