➤ Sum of digits in a String
➤ Count No of Vowels String
➤ String Pattern Programs in Java
➤ Take String Input In Java using Scanner Class
➤ Take Multiple String Input in Java using Scanner
➤ How To Reverse a String In Java
➤ Java Remove Special Characters From String
➤ How To Remove Character From a String Java
➤ String Palindrome In Java
➤ Sort String In Java
➤ How to Compare Strings In Java
➤ Find Second Occurrence of Character in String
➤ Java Replace nth Occurrence String
➤ Find Last Occurrence of Character in String Java
➤ Check If String Contains Uppercase & Lowercase
➤ Java Check If Char is Uppercase
➤ Check If String is Uppercase Java
➤ Swap Characters in String Java
➤ Java String indexOf() Method
➤ How to Replace Dot in Java?
➤ How to Find Length of String in Java
➤ Substring Method In Java
➤ Split Method In 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 remove a substring from the given string.
Now 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 substring with the empty string. Hence all the existing substrings are removed from the given string.
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!