I have the following String myString="city(Denver) AND state(Colorado)";
It has repeating "(" and ")"...
How can I retrieve state name, i.e. Colorado. I tried the following:
String state = myString.substring(myString.indexOf("state(")+1,myString.indexOf(")"));
But it give indexOutOfBoundException
Is there any way to specify that I need the second "(" in myString?
I need the result: String state = "Colorado";
Use lastIndexOf
. Also increase the initial offset to allow for the number of characters in the sub-string state(
:
String state = myString.substring(myString.indexOf("state(") + 6, myString.lastIndexOf(")"));
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With