How To Remove Substring From String Java

How To Remove Substring From String Java | Java provides a few inbuilt methods to replace the substring from the string like replace() and replaceAll(). These methods can be used not only to replace a substring but can also to remove a substring from the given string.

Let us see how to remove substring from string in Java by demonstrating the methods. Also see:- Replace Last Occurrence of Character in String Java

Remove Substring From String Java using replace()

The replace() method of the String class is used to remove or replace a substring from the string in Java. It replaces the first parameter with the second parameter and is available in the string class of the Java library java.lang package. Since java.lang is the default package hence there is no need to import it.

Method Syntax:- public String replace(CharSequence target, CharSequence replacement)

Parameter:- target – The sequence of char values to be replaced; replacement – The replacement sequence of char values
Return:- The resultant string after replacement

This method replaces each substring of this string that matches the literal target sequence with the specified literal replacement sequence. The replacement proceeds from the beginning of the string to the end, for example, replacing “bb” with “c” in the string “bbb” will result in “cb” rather than “bc”.

Program to Remove Substring From String Java using replace() Method

public class Main {
   public static void main(String[] args) {
      String string = "Know Program - Java Programming";
      String substring = "Program";
      
      // remove substring
      String resultantString = string.replace(substring, "");
      
      // display result
      System.out.println("String after removing \"" 
                         + substring + "\" substring: \n" 
                         + resultantString);
   }
}

Output:-

String after removing “Program” substring:
Know – Java ming

In the given string “Know Program – Java Programming”, the substring “Program” exists two times. On calling replace method we are replacing the substring with the empty string. Hence all the existing substrings are removed from the given string. Also see:- Java replace() vs replaceAll() Method

Remove Substring From String Java using replaceAll()

Similar to the replace() method String class also contains the replaceAll() method which is used to replace or remove the substring based on the regular expression.

Method Syntax:- public String replaceAll(String regex, String replacement)

  • Parameter:- regex – the regular expression to which this string is to be matched; replacement – the string to be substituted for each match.
  • Return:- The resulting String.
  • Throws:- PatternSyntaxException – if the regular expression’s syntax is invalid.

It replaces each substring of this string that matches the given with the given replacement.

Program to Remove Substring From String Java using replaceAll() Method

public class Main {
   public static void main(String[] args) {
      String string = "Know Program - Java Programming";
      String substring = "Program";
      
      // remove substring
      String resultantString = string.replaceAll(substring, "");
      
      // display result
      System.out.println("String after removing \"" 
                   + substring + "\" substring: \n" 
                   + resultantString);
   }
}

Output:-

String after removing “Program” substring:
Know – Java ming

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. Required fields are marked *