How can we replace specific part of String and StringBuffer in Java?


The String class of the java.lang package represents a set of characters. All string literals in Java programs, such as "abc", are implemented as instances of this class.

Example

 Live Demo

public class StringExample {
   public static void main(String[] args) {
      String str = new String("Hello how are you");
      System.out.println("Contents of the String: "+str);
   }
}

Output

Hello how are you

Strings objects are immutable, once you create a String object you cannot change their values, if you try to do so instead of changing the value a new object is created with the required value and the reference shifts to the newly created one leaving the previous object unused.

The StringBuffer (and StringBuilder) class is used when there is a necessity to make a lot of modifications to a String.

Unlike Strings, objects of type StringBuffer can be modified over and over again without leaving behind a lot of new unused objects. It is a thread-safe, mutable sequence of characters.

Example

 Live Demo

public class StringBufferExample {
   public static void main(String[] args) {
      StringBuffer buffer = new StringBuffer();
      buffer.append("Hello ");
      buffer.append("how ");
      buffer.append("are ");
      buffer.append("you");
      System.out.println("Contents of the string buffer: "+buffer);
   }
}

Output

Contents of the string buffer: Hello how are you

Replacing a specific part of the String

The replace() method of the String class accepts two String values −

  • One representing the part of the String (substring) to be replaced.

  • Another one representing the String with which you need to replace the specified substring.

Using this method you can replace a part of a Sting in java.

Example

 Live Demo

public class StringReplace {
   public static void main(String[] args) {
      String str = new String("Hello how are you, welcome to TutorialsPoint");
      System.out.println("Contents of the String: "+str);
      str = str.replace("welcome to TutorialsPoint", "where do you live");
      System.out.println("Contents of the String after replacement: "+str);
   }
}

Output

Contents of the String: Hello how are you, welcome to TutorialsPoint
Contents of the String after replacement: Hello how are you, where do you live

Replacing a specific part of the StringBuffer

Similarly, the replace() method of the StringBuffer class accepts −

  • Two integer values representing the start and end positions of the substring that is to be replaced.

  • A String value with which the above specified substring should be replaced.

Using this method, you can replace a substring of the StringBuffer with the required String.

Example

 Live Demo

public class StringBufferReplace {
   public static void main(String[] args) {
      StringBuffer buffer = new StringBuffer();
      buffer.append("Hello ");
      buffer.append("how ");
      buffer.append("are ");
      buffer.append("you ");
      buffer.append("welcome to TutorialsPoint");
      System.out.println("Contents of the string buffer: "+buffer);
      buffer.replace(18, buffer.length(), "where do you live");
      System.out.println("Contents of the string buffer after replacement: "+buffer);
   }
}

Output

Contents of the string buffer: Hello how are you welcome to TutorialsPoint
Contents of the string buffer after replacement: Hello how are you where do you live

Updated on: 10-Sep-2019

203 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements