March 23, 2022

Java String replace Method With Examples

In Java String class there are four replace() methods to replace occurrences of character or string with another character or string.

  • String replace(char oldChar, char newChar)- Returns a string resulting from replacing all occurrences of oldChar in this string with newChar. See example.
  • String replace(CharSequence target, CharSequence replacement)- Replaces each substring of this string that matches the target sequence with the specified literal replacement sequence. Note that the replacement starts from the beginning of the string to the end, or example, replacing "aa" with "b" in the string "aaa" will result in "ba" rather than "ab". See example.
  • String replaceAll(String regex, String replacement)- Replaces each substring of this string that matches the given regular expression with the given replacement. See example.
  • String replaceFirst(String regex, String replacement)- Replaces the first substring of this string that matches the given regular expression with the given replacement. See example.
Let’s see examples of these Java String replace() methods to make their usage clearer.

Java String replace() method for replacing chars

In the example there is a String where values are delimited using comma which is replaced by colon (:) using replace() method.

public class StringReplace {
  public static void main(String[] args) {
    String str = "A001,BOA,Nicki,12000";
    str = str.replace(',', ':');
    System.out.println(str);
  }
}
Output
A001:BOA:Nicki:12000

Java String replace() method for replacing substrings

In this variant of replace method CharSequence is passed as parameter rather than char. CharSequence is an interface which is implemented by String, StringBuffer and StringBuilder so objects of these classes can be passed.

public class StringReplace {
  public static void main(String[] args) {
    String str = "String misspelt as strong so replace strong with string";
    str = str.replace("strong", "string");
    System.out.println(str);
  }
}
Output
String misspelt as string so replace string with string

Java String replaceAll() method

With replaceAll() method you can pass a regular expression and all the substrings matching the regular expression are replaced with replacement string.

In the following example regex matches any number of spaces which are replaced with no space.

public class StringReplace {
  public static void main(String[] args) {
    String str = "  Test   String    ";
    // Matches any number of spaces
    str = str.replaceAll("\\s+", "");
    System.out.println(str);
  }
}
Output
TestString

Java String replaceFirst() method

With replaceFirst() method you can pass a regular expression and only the first substring matching the regular expression is replaced with replacement string.

public class StringReplace {
  public static void main(String[] args) {
    String str = "Hello world, Hello again";
    str = str.replaceFirst("Hello", "Hey");
    System.out.println(str);
  }
}
Output
Hey world, Hello again

That's all for the topic Java String replace Method With Examples. If something is missing or you have something to share about the topic please write a comment.


You may also like

No comments:

Post a Comment