Java String replace: The Complete Guide

Java string replace() method returns the new string resulting from replacing all occurrences of old characters in the string with new characters.

Java String replace

Java string replace() is a built-in method that returns the string replacing all the old char or CharSequence to a new char or CharSequence. However, it is sometimes required to replace some characters in a String with a new character.

It might also be required to change an entire sequence of characters with a new sequence of characters, i.e., replacing an old CharSequence with a new CharSequence. The Java.lang.String.replace() is the String class method designed just for this purpose.

Figure

Java String replace Example

Syntax

public String replace(char old, char new),

Where old is the character to be replaced and new is the character to be inserted in its place.

Return value

It returns a string by replacing every occurrence of old with new.

Internal Implementation

public String replace(char oldChar, char newChar) {
    if (oldChar != newChar) {
      int len = value.length;
      int i = -1;
      char[] val = value; /* avoid getfield opcode */

      while (++i < len) {
        if (val[i] == oldChar) {
          break;
        }
      }
      if (i < len) {
        char buf[] = new char[len];
        for (int j = 0; j < i; j++) {
          buf[j] = val[j];
        }
        while (i < len) {
          char c = val[i];
          buf[i] = (c == oldChar) ? newChar : c;
          i++;
        }
        return new String(buf, true);
      }
    }
    return this;
  }

Example1.java:

The following example demonstrates how the replace() method changes all occurrences of an old character with a new character.

public class Example1 {
  public static void main(String[] args) {
    System.out.println("This program will replace all occurences of 's' with 'z'");
    String s = new String("This is a demo string. This is s.");

    System.out.println(s);
    String s_new = new String();
    s_new = s.replace('s', 'z');

    System.out.println(s_new);

  }
}

Output

This program will replace all occurrences of ‘s’ with ‘z’
This is a demo string. This is s.
Thiz iz a demo ztring. Thiz iz z.

Here, the Java replace() method is used to replace all occurrences of the old character’ s,’ replaced with the new character ‘z’.

Example2.java

Since JDK 1.5, Java also allows replacing an entire CharSequence with a new one. The following example demonstrates such a case:

public class Example2 {
  public static void main(String[] args) {
    String s = new String("He decided to go to New York.");
    System.out.println(s);

    String s2 = new String();
    s2 = s.replace("New York", "Moscow");
    System.out.println(s2);
  }
}

Output

He decided to go to New York.
He decided to go to Moscow.

Here, the sequence “New York” gets replaced with “Moscow”. Multiple occurrences of a sequence can be replaced in the same way. The same is demonstrated in the next example.

Example3.java

public class Example3 {
  public static void main(String[] args) {
    String s = new String("I had a bike. But I sold the bike to get a new bike.");
    System.out.println(s);

    String s2 = new String();
    s2 = s.replace("bike", "car");
    System.out.println(s2);
  }
}

See the output.

I had a bike. But I sold the bike to get a new bike.
I had a car. But I sold the car to get a new car.

Here, all occurrences of “bike” are replaced with “car” by using the replace() method.

In conclusion, the Java.lang.String.replace() method replaces all the occurrences of a character/sequence of characters with a new character/sequence of characters.

That’s it for this tutorial.

Recommended Posts

Java String replaceAll()

Java String getBytes()

How To Convert String Characters To Lowercase

Java String toCharArray()

Java String indexOf()

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.