Remove Substring From String in Java

Asad Riaz Oct 12, 2023
  1. replace() Method to Remove Substring in Java
  2. StringBuffer.replace() Method to Remove Character From String in Java
  3. replaceAll() Method to Remove Substring From String in Java
Remove Substring From String in Java

In this tutorial, we will learn how to remove a substring from any given string in Java.

replace() Method to Remove Substring in Java

The first and most commonly used method to remove/replace any substring is the replace() method of Java String class.

string.replace(char oldChar, char newChar)

The first parameter is the substring to be replaced, and the second parameter is the new substring to replace the first parameter.

Example Codes:

import java.text.*;
import java.util.List;

public class SimpleTesting {
  public static void main(String[] args) {
    String originalstring = "This is Simple Testing";
    String replace = originalstring.replace('i', 'a');
    System.out.println("Original String: " + originalstring);
    System.out.println("Modified String with Replacement: " + originalstring.replace('i', 'a'););
  }
}

Output:

Original String: This is Simple Testing
Modified String with Replacement: Thas as Sample Testang

StringBuffer.replace() Method to Remove Character From String in Java

This method could remove/replace any substring in the given index range. Java StringBuffer is similar to String, but is mutable.

The syntax of StringBuffer.replace() method is,

StringBuffer replace(int start, int end, String str)

start and end are the beginning and ending index of the specified range. start is inclusive, and end is exclusive; therefore, the actual range is [start, end-1].

str is the string that replaces the content in the above-specified range.

Example Codes:

import java.text.*;
import java.util.List;

public class SimpleTesting {
  public static void main(String[] args) {
    StringBuffer originalString = new StringBuffer("Simple Testing");
    System.out.println("Original String: " + originalString);
    originalString.replace(5, 6, "y");
    System.out.println("Modified String: " + originalString);
  }
}

Output:

Original String: Simple Testing
Modified String: Simply Testing

replaceAll() Method to Remove Substring From String in Java

Another similar method to replace() method is to use replaceAll() method of Java String class.

Its syntax is,

String replaceAll(String regex, String replace)

regex is the pattern of the regular expression.

replace is the string to replace the existing one.

Example Codes:

import java.text.*;
import java.util.List;

public class SimpleTesting {
  public static void main(String[] args) {
    String originalstring = "This is Simple Testing Code";
    System.out.println("Original String: " + originalstring);
    System.out.println("Modified String: " + originalstring.replaceAll("Code", ""));
  }
}

Output:

Original String: This is Simple Testing Code
Modified String: This is Simple Testing 

A more enhanced point to use the replaceAll() method is to use a pattern of the regular expression to remove substrings that match the pattern all at once.

Example Codes:

import java.text.*;
import java.util.List;

public class SimpleTesting {
  public static void main(String[] args) {
    String originalstring = "This Test is from Simple Testing Code to Test function ";
    System.out.println("Original String: " + originalstring);
    System.out.println("Modified String: " + originalstring.replaceAll("Tes.*?\\b", ""));
  }
}

Output:

Original String: This Test is from Simple Testing Code to Test function 
Modified String: This  is from Simple  Code to  function 
Note
Both replace() and replaceAll() methods replace all occurrences. The difference between them is that the replaceAll() method uses a regular expression pattern.

Related Article - Java String