konstantin konstantin - 1 year ago 70
Java Question

Return a specific part from a String in java

I want to change a string so that only the numbers in the parenthesis will be kept. An example of the string is


And the output should be


How can I return only the numbers in the parenthesis?

Answer Source
String str = "username123(151231)";
String newstr= str.substring(str.indexOf("(")+1,str.indexOf(")"));

From OP's question it is given there is one set of parenthesis. For other cases use regex.

Matcher m = Pattern.compile("\\(([^)]+)\\)").matcher(str);
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download