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 Replace and Replace All methods Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


Win a copy of The Mikado Method this week in the Agile and other Processes forum!
JavaRanch » Java Forums » Java » Beginning Java
Reply Bookmark "String Replace and Replace All methods" Watch "String Replace and Replace All methods" New topic
Author

String Replace and Replace All methods

sakshi sa
Greenhorn

Joined: Jan 18, 2011
Posts: 7
Hi All,

when i practicing string methods, i didn't understand difference b/w string replace and string replace All.

could you please tell me that difference and please provide sample code on string methods.
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 9952
    
    6

Did you read the api?

String replace(char oldChar, char newChar)
Returns a new string resulting from replacing all occurrences of oldChar in this string with newChar.
String replace(CharSequence target, CharSequence replacement)
Replaces each substring of this string that matches the literal target sequence with the specified literal replacement sequence.
String replaceAll(String regex, String replacement)
Replaces each substring of this string that matches the given regular expression with the given replacement.


There are two 'replace' methods, and one 'replaceAll' method. the first replace method takes a SINGLE CHARACTER, and replaces it with a SINGLE CHARACTER.

The second will take a CharSequence - basically a string - and will replace it with another string (note - I used a little 's' there on purpose).

Finally, the 'replaceAll' takes a regular expression, which lets you do much more powerful substitutions, and replaces it with a String. So you could pass "r.n" and it would replace "run", "ran", or "ron" (or many other things) with what you supply.

Never ascribe to malice that which can be adequately explained by stupidity.
Atul Darne
Ranch Hand

Joined: Jul 05, 2009
Posts: 117

replace function replaces a single character
and replace all function replaces each occurrence of the character with a new character provided.


Regards, Atul.
I came to this world on a Learner's License
sakshi sa
Greenhorn

Joined: Jan 18, 2011
Posts: 7
thanks.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19216

Atul Darne wrote:replace function replaces a single character

That used to be the case, but since Java 5.0 there's another one.

and replace all function replaces each occurrence of the character with a new character provided.

No it doesn't. That's what replace(char, char) does. replaceAll doesn't even replace characters; it replaces regular expressions.


SCJP 1.4 - SCJP 6 - SCWCD 5
How To Ask Questions How To Answer Questions
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 32708
    
    4
sakshi sa,
Your post was moved to a new topic.
 
I agree. Here's the link: http://zeroturnaround.com/jrebel - it saves me about five hours per week
 
subject: String Replace and Replace All methods
 
Similar Threads
how to replace ? with \? from string
Help on String methods!!!
replace()
mock question
matching a string in a line