File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes String ReplaceAll Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "String ReplaceAll " Watch "String ReplaceAll " New topic
Author

String ReplaceAll

manoj kumar jena
Greenhorn

Joined: Sep 24, 2007
Posts: 25
System.out.println("12.0".replaceAll(".0",""));

The above line prints 12 but the following line prints a blank string, why? it should print 10.
:!:
System.out.println("10.0".replaceAll(".0",""));
Punit Singh
Ranch Hand

Joined: Oct 16, 2008
Posts: 952
manoj kumar jena wrote:System.out.println("12.0".replaceAll(".0",""));

The above line prints 12 but the following line prints a blank string, why? it should print 10.
:!:
System.out.println("10.0".replaceAll(".0",""));



replaceAll(regex,replacementString); this function uses regular expression for matching.
For ".0" : dot means any character in regular expression, 0 means 0, so regex engine will try to find anycharacter ending with 0.
In first try it will find 10--------> 1 character ending with 0
in Second try it will find .0------> . character ending with 0.
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 19070
    
  40

And as a side note... the correct regex for what you want is...



Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Bauke Scholtz
Ranch Hand

Joined: Oct 08, 2006
Posts: 2458
If you aren't going to take benefit from regexps, but just want to do a replace of the exact plain vanilla value, just use String#replace() instead. Requires at least Java 1.5 though.
manoj kumar jena
Greenhorn

Joined: Sep 24, 2007
Posts: 25
Thank you very much to all of you for your immediate and valuable response
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: String ReplaceAll