-3

Although I have seen a question similar to this one asked quite a few times, I actually mean remove all trailing zeroes.

I would like to convert something like

"1903895810000"

to

"190389581"

I am looking for a String.replace() solution

2
  • 2
    Doesn't seem to me like you're looking for anything because this question has already been asked 1000 times before Commented Jan 16, 2016 at 22:55
  • Have you made any attempt at doing it using replace()? Commented Jan 16, 2016 at 22:56

1 Answer 1

9

Simple regexp with replaceAll will do it.

String s = "1903895810000";
System.out.println(s.replaceAll("0+$", ""));

[EDIT]:

s.replace(0, "") will not work here, because it will remove all zeros from the string, so you can't use it. So, here I used replaceAll, that uses regular expressions to match replacement string. This simple regexp 0+$ matches any number of zeros 0+ followed by end-of-string $, so it would be "some zeroes at the end".

Sign up to request clarification or add additional context in comments.

3 Comments

If you are going to answer such poor questions as this at least explain what your code is doing
But how? If OP couldn't write that regex, then an explanation of the regex is appropriate, so OP (and other readers) can learn something. This answer doesn't help much.
Added an explanation.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.