Javatpoint Logo
Javatpoint Logo

How to Remove substring from String in Java

Java provides several methods for removing substrings from Strings.

1) Using replace method

This method belongs to the Java String class and is overloaded to provide two different implementations of the same method.

In the first approach, a new character is added to a string to replace every previous character.

This way returns the string with the new characters after all the old characters have been updated.

Syntax:

The procedure returns this string if the new character cannot be located in the string.

Example:

Output:

How to Remove substring from String in Java

2) Using Charsequence

The second technique substitutes the desired string of characters for the CharSequence, which is just a collection of characters.

Syntax:

This operation and the first merely differ in that it substitutes a string of characters.

Example:

Output:

How to Remove substring from String in Java

3) Replace the Substring with an empty string

Java allows you to easily replace a substring you want to delete from the String with an empty String.

Syntax:

Example:

Output:

How to Remove substring from String in Java

4) Using String's replaceFirst method

This method searches for a string that matches a regular expression and, if one is found, replaces it with the given string.

Behind the scenes, this function extracts the text using a regular expression by using the compile() and matcher() methods of the Pattern class.

Syntax:

A regular expression will be created to extract a number from a string and replace it with another number as a string.

Note: Only the first two digits of the string will be changed by this number; the remaining digits will remain unchanged.

Example:

Output:

How to Remove substring from String in Java

5) replaceFirst() method

The replaceFirst() method can be used with an empty String to delete a substring from a String.

Syntax:

Example:

Output:

How to Remove substring from String in Java

6) Using replaceAll method

The replaceAll method, as contrast to replaceFirst, utilizes a regular expression to replace every instance of the string.

Similar to replaceFirst, this method extracts a string using a regular expression by using the compile() and matcher() methods. It also produces a PatternSyntaxException if the regular expression is incorrect.

Syntax:

We will employ a regular expression that pulls out all numbers from a string and substitutes numbers for every instance.

\d: This regular expression recognizes any digit between 0 and 9.

Example:

Output:

How to Remove substring from String in Java

7) replaceAll() method

With the replaceAll() method, you can use an empty String to remove a substring from a string.

Syntax:

Example:

Output:

How to Remove substring from String in Java

8) Using String Builder's delete() method

In order to add and remove characters from a string, the StringBuilder holds a modifiable sequence of characters.

A string builder with an initial capacity of 16 characters is created by the empty StringBuilder function Object() { [native code] }, and if the internal buffer overflows, a larger string builder is created automatically.

The start and end of the substring to be deleted from the string are specified as the first and second int parameters of the delete() function.

The last index is exclusive since it subtracts one from the second parameter, but the start index is inclusive.

Syntax:

When the start is negative, bigger than the string's length, or bigger than the string's end, a StringIndexOutOfBoundsException is thrown. There are no adjustments done when the start and end are equal.

Example:

Output:

How to Remove substring from String in Java

9) Using StringBuilder replace() Method

The sole difference between the replace() function and the delete() method is the third parameter, which is used to replace the characters that have been removed from the string.

If there is a large string that needs to be replaced, the size will be increased to accommodate the length of the string.

Syntax:

The function toString() { [native code] }() function can be used to print the updated string after this method returns a StringBuilder.

Example:

Output:

How to Remove substring from String in Java

Conclusion

You've learnt how to replace and delete characters to remove a substring from a string in this article. The techniques presented include the use of the StringBuilder delete() and replace() methods, as well as the string replace(), replaceFirst(), and replaceAll() functions.







Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA