Skip to content

Instantly share code, notes, and snippets.

@minitech
Created July 9, 2012 14:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save minitech/3076861 to your computer and use it in GitHub Desktop.
Save minitech/3076861 to your computer and use it in GitHub Desktop.
Another way to loop
// Alternative looping.
// I wouldn't do this using a foreach loop, but it is much nicer than
// the original here: http://stackoverflow.com/a/11367974/707111
public int getNthIndex(string str, char c, int n)
{
int index = 0;
int count = 0;
if(n == 0)
return -1;
foreach(char i in str)
{
if (i == c) {
count++;
if (count == n)
return index;
}
index++;
}
return -1;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment