Search code examples
javaandroidregexstringreplaceall

Android String replace All does not work


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.


Solution

  • 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(",", "");