• Post Reply Bookmark Topic Watch Topic
  • New Topic

Problem with replaceAll method  RSS feed

 
Andrew Carney
Ranch Hand
Posts: 96
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,

I have a number represented as a String which I would like to amend.
The number is always coming in this format: XX.0, for example: 33.0, 15.0, 70.0. I am running a replaceAll(".0","") method on it and the problem is this:
When the number doesn't end with 0 it's working OK, i.e.: 33.0->33, 15.0->15
When the number ends with 0 it's not working as expected, i.e. instead of turning 70.0 to 70 I am getting an empty String back.
Any idea why?
 
Lavanya Mothilal
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Roy,

Your question is confusing.

When the number doesn't end with 0 it's working OK, i.e.: 33.0->33, 15.0->15


I think you have given a wrong example here.


When the number ends with 0 it's not working as expected, i.e. instead of turning 70.0 to 70 I am getting an empty String back.


I tried the code
System.out.println("33.0".replaceAll(".0", ""));
- It printed 33 as the output.
 
Andrew Carney
Ranch Hand
Posts: 96
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Try this: System.out.println("30.0".replaceAll(".0", ""));
Are you getting 30?
 
Andrew Carney
Ranch Hand
Posts: 96
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think it treats it as a regular expression.
So the question i how do I do it with regular expressions instead...
[ March 07, 2008: Message edited by: Roy Cohen ]
 
Alan Moore
Ranch Hand
Posts: 262
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"." is a regex metacharacter that matches any character. In your code, the regex was matching twice; the first time the "." in the regex matched the "3", and the second time it matched the ".".
[ March 07, 2008: Message edited by: Alan Moore ]
 
Andrew Carney
Ranch Hand
Posts: 96
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Alan, it's indeed working.
Small change big difference.
Will you also be kind to explain what was wrong in my code and why is it working now?

Thank you in advance!
 
Alan Moore
Ranch Hand
Posts: 262
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Roy Cohen:
Will you also be kind to explain what was wrong in my code and why is it working now?

Didn't I just do that? Maybe you could narrow down the question a little.

One thing I would like to point out: since the string you're replacing is always the same, you could use replace() instead of replaceAll() and avoid regexes altogether. This assumes you're running at least Java 5, which is when that method was added.
 
Andrew Carney
Ranch Hand
Posts: 96
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks again Alan, I didn't see your explanation the first time.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!