Android String replace All does not work

9k Views Asked by At

In Android application, I am using

String num = "10,000,000.00";
num.replaceAll("," , "");

Expected: 10000000.00

But I am getting : 10 000 000.00

Works in most of the devices. Facing issue in few devices like Galaxy Core, Galaxy Grand duos, Galaxy Young, Galaxy Note II and Xperia C

Kindly help.

3

There are 3 best solutions below

0
On BEST ANSWER

replaceAll method is not modifying the num's value. Instead it is creating a new String object and returning it. So all you need to do is assign the value what replaceAll is returning to num variable.

But I think you need replace method to remove the , characters rather than replaceAll which first parameter is regex (lets not mix the regex in this case).

The doc says:

replace(CharSequence target, CharSequence replacement)
Replaces each substring of this string that matches the literal target sequence with the specified literal replacement sequence.

So it all goes up to this:

num = num.replace(",", "");
0
On

Use below java code:

String num = "10,000,000.00";
num.replaceAll("," , "").replaceAll("\\s+" , "");

replaceAll method is used for the regex

Try with replace method

 String num = "10,000,000.00";
 num.replace("," , "");
3
On
public class DataData
{
    public static void main(String args[]) throws ParseException
    {

        String num = "10,000,000.00";
        num= num.replaceAll("," , "");
        System.out.println(num);///// output 10000000.00
    }
}