0

I'm getting response from the server like this "[ 61 ]" I want to get rid of the parentheses and keep the numbers only.

This is what I did, but this does not work for dynamic responses, it only fits static number, but if the number gets bigger, this doesn't work

@Override
public void onResponse(String response) {
    StringBuffer sb = new StringBuffer(response);

    sb.deleteCharAt(0);
    sb.deleteCharAt(7);
    numberofvotes.setText("Counts : " + sb.toString().trim());
}

How can I remove parentheses anywhere in the string?

Sotirios Delimanolis
  • 274,122
  • 60
  • 696
  • 724

1 Answers1

3

To keep only the number, you can use String::replaceAll which use regular expression like this :

//response = "[ 61 ]";
response.replaceAll("[^\\d]", ""); // Output '61'

Which mean replace all non digits.

Or like Pshemo mention you can use :

response.replaceAll("\\D", ""); 
Youcef LAIDANI
  • 55,661
  • 15
  • 90
  • 140
  • 1
    BTW this kind of negation also exist with other predefined character sets. For instance `\s` vs `\S`, or even ones in form `\p{name}` vs `\P{name}`. – Pshemo Mar 03 '18 at 18:19