Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Find index of a second character occurrence in a string?

Tags:

java

string

How do I find the second index of a character in a string. For instance:

String a="aa{aaaaaaa{aaa}";

I'd like to find the index value of the second {. Here it is 10.

like image 237
Sreekanth Avatar asked Oct 21 '25 11:10

Sreekanth


2 Answers

Find the first one, move one right, then find the next one from there. That's the second :)

int secondIndex = a.indexOf('{', a.indexOf('{')+1);
like image 169
gaborsch Avatar answered Oct 24 '25 03:10

gaborsch


Try to overloaded version of indexOf(), which takes the starting index as 2nd parameter.

str.indexOf("{", str.indexOf("{") + 1);
like image 36
Kalpana Avatar answered Oct 24 '25 01:10

Kalpana