Join
Blogs
Questions
Videos
Tags
Members
Search
 
 
Create your own blog, earn points and get popular!

Java String Replace Example

  1. /*
  2. Java String replace example.
  3. This Java String Replace example describes how replace method of java String
  4. class can be used to replace character or substring by new one.
  5. */
  6.  
  7. public class JavaStringReplaceExample{
  8.    
  9.     public static void main(String args[]){
  10.        
  11.         /*
  12.         Java String class defines three methods to replace character or
  13.         substring from the given Java String object.
  14.        
  15.         1) String replace(int oldChar, int newChar)        
  16.         This method replaces a specified character with new character
  17.         and returns a new string object.
  18.        
  19.         2) String replaceFirst(String regularExpression, String newString)        
  20.         Replaces the first substring of this string that matches the
  21.         given regular expression with the given new string.
  22.        
  23.         3) String replaceAll(String regex, String replacement)        
  24.         Replaces the each substring of this string that matches the
  25.         given regular expression with the given new string.
  26.         */
  27.        
  28.         String str="Replace Region";
  29.        
  30.         /*
  31.         Replaces all occurrences of given character with new one
  32.         and returns new String object.
  33.         */
  34.        
  35.         System.out.println(str.replace( 'R','A' ));
  36.        
  37.         /*
  38.         Replaces only first occurrences of given String with new one
  39.         and returns new String object.
  40.         */
  41.        
  42.         System.out.println(str.replaceFirst("Re", "Ra"));
  43.        
  44.         /*
  45.         Replaces all occurrences of given String with new one and
  46.         returns new String object.
  47.         */
  48.        
  49.         System.out.println(str.replaceAll("Re", "Ra"));
  50.        
  51.     }
  52.  
  53. }
  54.  
  55. /*
  56. OUTPUT of the above given Java String Replace Example would be:
  57. Aeplace Aegion
  58. Raplace Region
  59. Raplace Ragion
  60. */
Your rating: None Average: 3.9 (9 votes)
Share this
Anonymous's picture

thanx a lot.... it helped me

thanx a lot.... it helped me very much...
Anonymous's picture

it very good.

it very good.
Anonymous's picture

Good thing about replace()

Good thing about replace() method is that it accept regular expression similar to split of String Class which allow you to replace character matching to your regex, which is very convinient and flexible way of doing it.
Anonymous's picture

relaceAll() method has a

relaceAll() method has a special behavior if you use escaped double quotes they will be unescaped. So: "a string".replaceAll("string", "\"mirror\"") will not produce a \"mirror\". Instead, the escaped double quotes will be unescaped and thus the output will be a "mirror". you can use JDK5 repalce(charSequence) to avoid this. See Java String replace example with regex for more details.
Anonymous's picture

thnks a lot ..it helped me

thnks a lot ..it helped me very much.
Anonymous's picture

Thanks a lot.......

Thanks a lot.......
Anonymous's picture

How do I replace a \ with

How do I replace a \ with nothing?
Anonymous's picture

we can not replace \. If any

we can not replace \. If any body have solution for this,please post here. thanks Indian.
Anonymous's picture

How do you replace '[' and

How do you replace '[' and ']' with space?
Anonymous's picture

to replace \

to replace \ use string.replace("\"", "");
Anonymous's picture

and to replace '[' & ']' use

and to replace '[' & ']' use string.replace("[", " ").replace("]", " ");
Anonymous's picture

Thank you very much

Thank you very much Dude.............SOOO helpful answer and I am soooo thankful to you....:)
Anonymous's picture

To Replace \ with "str"

To Replace \ with "str" replace("\\","str")
Anonymous's picture

can we replace String with

can we replace String with regex
Anonymous's picture

how do I replace " (inch

how do I replace " (inch symbol) character with some other character ?
Anonymous's picture

There was a questions on an

There was a questions on an interview to replace a given character from String ? what do you recommend for that converting string into String as suggested here or directly using String.
Anonymous's picture

How do you do two

How do you do two replacements on one line? (Like trying to replace all of the 'e's with 3s and 'a's with 4s, so that "abe" would be "4b3")
Anonymous's picture

I wanna replace a char at a

I wanna replace a char at a specific position in the string ...Help pls
Anonymous's picture

Nice...thx 4 helping

Nice...thx 4 helping

Post new comment