0

I have a string of the following form for instance -:

String str = "The game received average review scores of 96.92% and 98/100 for the Xbox 360 version";

and I want the output as follows -:

Output = "The game received average review scores of % and / for the Xbox version"

and I want to be able to filter out any number that's there in the string whether its a decimal or a floating point number, I tried using a brute force way of doing this like this -:

String str = "The game received average review scores of 96.92% and 98/100 for the Xbox 360 version";

String newStr = "";

for(int i = 0 ; i < str.length() ; ++i){
if(!Character.isDigit(str.charAt(i)))
   newStr += str.charAt(i);
}

System.out.println(newStr);

but this doesn't solve the purpose for me, how to go about solving this problem?

AnkitSablok
  • 2,715
  • 5
  • 26
  • 51
  • You might check out [this answer](http://stackoverflow.com/a/19169192/1578604) which is basically the opposite of what you're looking for. With some tweaking, you can use it. – Jerry Oct 04 '13 at 20:50

2 Answers2

2

You can use following String#replaceAll call to replace all numbers (followed by 0 or more spaces) by an empty string:

str.replaceAll("\\d+(,\\d+)*(?:\\.\\d+)?\\s*", "");
anubhava
  • 664,788
  • 59
  • 469
  • 547
1

You can perhaps use something like this:

str.replaceAll("\\d+(?:[.,]\\d+)*\\s*", "");
Jerry
  • 67,172
  • 12
  • 92
  • 128