By arunraj
Use the following code snippet to find the nth occurance of a character in a string.
/*
* This method returns the position of nth occurance(starting from zero)
* of a character sequence in the given string.
* It returns -1 if the character sequence is not found.
*/
public int nthOccurance(String str,int n,String srchStr)
{
int pos = str.indexOf(srchStr, 0);
while (n-- > 0 && pos != -1)
{
pos = str.indexOf(srchStr, pos+1);
return pos;
}
}
* This method returns the position of nth occurance(starting from zero)
* of a character sequence in the given string.
* It returns -1 if the character sequence is not found.
*/
public int nthOccurance(String str,int n,String srchStr)
{
int pos = str.indexOf(srchStr, 0);
while (n-- > 0 && pos != -1)
{
pos = str.indexOf(srchStr, pos+1);
return pos;
}
}