Replace In String Java

Replace In String Java | Java provides a replace method to replace or remove the unwanted strings or characters. The replace() method is introduced since JDK 1.5. There are two variations in replace() method, below are the method details:-

1) public string replace(char oldchar, char newchar)
Parameters:-
oldchar- the character needed to be replaced
newchar- is the character that replaces the old character.
Returns: the replaced string.
Exception: NullPointerException if the target string is equal to null.

2) public String replace(CharSequence target, CharSequence replacement)
Parameters:-
target- the CharSequence needed to be replaced
replacement- is the CharSequence that replaces the old CharSequence.
Returns:- the replaced string.
Exception:- NullPointerException if the target string is equal to null.

Except for these two methods String class contains very similar other methods:-
1) public String replaceFirst(String regex, String replacement):- It replaces only the first occurrence of regex in the given string.
2) public String replaceAll(String regex, String replacement):- It replaces all the occurrences of regex in the given string.

Java replace() method Example

This program explains the first variation of the replace() method which takes the character as a parameter. Example of replace(char oldchar, char newchar)

public class Main {
   public static void main(String args[]) {
      String string = "Jeve Progremming Lenguege";
      String replace = string.replace('e', 'a');
      System.out.println(replace);
   }
}

Output:-

Java Programming Languaga

In this replace() method if the given string doesn’t contain the character which we want to replace in that case it returns the reference of the string, but not the new copied string. But if the character occurs in the string then it replaces all existence of the character from the string with the new character and returns the new string.

public class Main {
   public static void main(String args[]) {
      String str1 = "www knowprogram com";
      String replaceStr1 = str1.replace('@', '#');
      String replaceStr2 = str1.replace(' ', '.');

      System.out.println(System.identityHashCode(str1));
      System.out.println(System.identityHashCode(replaceStr1));
      System.out.println(System.identityHashCode(replaceStr2));

      str1 = "Hello";
      System.out.println(str1);
      System.out.println(replaceStr1);
      System.out.println(replaceStr2);
   }
}

Output:-

1143839598
1143839598
110718392
Hello
www knowprogram com
www.knowprogram.com

Replace Method In Java

This program explains the replace() method which takes the string as a parameter. Example of replace(CharSequence target, CharSequence replacement)

public class Main {
   public static void main(String args[]) {
      String string1 = "Java Programming Language";
      String replace = string1.replace("Program", "PROGRAM");
      System.out.println(replace);
   }
}

Output:-

Java PROGRAMming Language

public class Main {
   public static void main(String args[]) {
      String string1 = "Java Programming Language";
      String replace = string1.replace("a", "@");
      System.out.println(replace);
   }
}

Output:-

[email protected]@ [email protected] [email protected]@ge

This replace() method replace from the beginning to the end of the string. Let us understand it through an example:-

public class Main {
   public static void main(String args[]) {
      String string1 = "Brrrh";
      String replace = string1.replace("rr", "@");
      System.out.println(replace);
   }
}

Output:-

[email protected]

Since it replaces from the beginning to the end of the string therefore it gives the result the [email protected] but not the [email protected]

If you enjoyed this post, share it with your friends. Do you want to share more information about the topic discussed above or do you find anything incorrect? Let us know in the comments. Thank you!

Leave a Comment

Your email address will not be published.