1

I need to replace all the 's around numbers, to nothing.. for example:

'1' to 1 

'100' to 100

which is the optimal way to do this? is there a regex to do this so I can use it in the replace() function of the String class?

1
  • Can you clarify that all these numbers are integers? Are there any doubles like '10.5' or '10.3333'? Commented Aug 4, 2016 at 19:36

2 Answers 2

4

You can use replaceAll method with regex support:

str = str.replaceAll("'(\\d+)'", "$1");

(\\d+) will match and group digits surrounded by single quotes on either side and then we use $1 in replacement which is the back-reference to captured value in regex.

Sign up to request clarification or add additional context in comments.

Comments

-1

If it's in a String and you want the integer why don't you just parse it.

int a = Integer.parseInt("100");

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.