Created
July 9, 2012 14:25
-
-
Save minitech/3076861 to your computer and use it in GitHub Desktop.
Another way to loop
This file contains hidden or 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
// 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