How to use substring and indexOf for a String with repeating characters?
问题 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"; 回答1: Use lastIndexOf . Also increase the initial offset to allow for the number of