truy37
truy37

Reputation: 91

How do I filter a string on just numbers, dots and commas?

Okay, so ive got a long string, and I want to remove everything thats inside, except for decimal numbers, comma's and dots,

I have tried:

str = str.replace("[^0-9\\.\\,]","");

But this just ends up in nothing..

Can anyone help me?

Thanks!

Upvotes: 9

Views: 12952

Answers (2)

Martijn
Martijn

Reputation: 12102

Try str.replaceAll("[^0-9.,]+","");

Upvotes: 5

jjnguy
jjnguy

Reputation: 138884

You don't need to escape the characters in the character group. You should also be using replaceAll().

str = str.replaceAll("[^0-9.,]+","");

Upvotes: 23

Related Questions