Sreekanth
Sreekanth

Reputation: 407

Find index of a second character occurrence in a 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.

Upvotes: 4

Views: 14028

Answers (2)

Kalpana
Kalpana

Reputation: 561

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

str.indexOf("{", str.indexOf("{") + 1);

Upvotes: 4

gaborsch
gaborsch

Reputation: 15758

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);

Upvotes: 10

Related Questions