Skip to content

Instantly share code, notes, and snippets.

@Buildstarted
Created August 4, 2012 02:43
Show Gist options
  • Save Buildstarted/3253682 to your computer and use it in GitHub Desktop.
Save Buildstarted/3253682 to your computer and use it in GitHub Desktop.
Splits a string by delimiters but retails delimiter
private IEnumerable<string> GetIdentifierParts(string source)
{
char[] splitBy = new[] { '.', '#' };
int start = 0;
int index = 0;
string previousCharacter = "";
while ((index = source.IndexOfAny(splitBy, start)) != -1)
{
index++;
index = Interlocked.Exchange(ref start, index);
yield return previousCharacter + source.Substring(index, start - index - 1);
previousCharacter = source.Substring(start - 1, 1);
}
if (start < source.Length)
{
yield return previousCharacter + source.Substring(start);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment