No, the correct regular expression is something more complex, and it needs positive look-behind.
str = str.replaceAll("\\.0*$|(?<=\\.[0-9]{0,2147483646})0*$", "");
you have to escape the .
, because otherwise it means "any character", you have to anchor the regex only to the end of the string, and you have to say that the 0
digits must be after a .
plus some optional non-zero digits.
Addendum: there is a "special case" that should be handled: 1.00
. We handle it by using the |
. The first sub-expression means "a dot plus only zeroes" and matches even the dot (that in this way is deleted)
And remember that in Java strings are immutable, so replaceAll
will create a new string.
Note the use of {0,2147483646}
: if you used a *
you would get a Look-behind group does not have an obvious maximum length
, because the *
would be converted to {0,2147483647}
and considering the "+1" length of the \\.
it would overflow, so we put the maximum number of digits possible (2147483647, maximum value of a signed int) minus 1 for the dot.
Test example: http://ideone.com/0NDTSq