Create a gist now

Instantly share code, notes, and snippets.

Another way to loop
// Alternative looping.
// I wouldn't do this using a foreach loop, but it is much nicer than
// the original here:
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) {
if (count == n)
return index;
return -1;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment