Remove substring from String in Java

In this post, we will see how to remove substring from String in java.

There are multiple ways to remove substring from String in java.

Using String’s replace method to remove substring from String in Java

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

The first method introduces a new character to a string that is used to replace all the old characters in that string.

After all the old characters are replaced, the method returns the string with the new characters.

If the new character is not found in the string, the method returns this string.

Output:

mosquito in your collar

The second method replaces a CharSequence which is simply a sequence of characters with the desired sequence of characters from a string.

This method works the same way as the first method, only that it replaces a sequence of characters.

Output:

Mary likes mangoes

If you want to remove substring from the String in java, you can simply replace the substring with empty String.

Here is an example:

Output:

Mary likes

Using String’s replaceFirst method to remove substring from String in Java

This method uses a regular expression to identify a string that matches the regular expression and is replaced with the passed string if present.

This method uses compile() and matcher() methods from the Pattern class behind the scenes to extract the string using the regular expression.

The method returns a string, and if the regular expression is invalid, the method throws a PatternSyntaxException error.

We will create a regular expression that extracts a number from a string and replaces it with another number as a string.

Note that this number will only replace the first two numbers in the string, and the other numbers will not be altered.

Output:

Peter is 30 years old and Jane is 60 years old

If you want to remove substring from String, you can use empty String with replaceFirst() method

Here is an example:

Output:

Peter is years old, and Jane is 60 years old

Using String’s replaceAll method to remove substring from String in Java

Unlike thereplaceFirst, thereplaceAll method uses the regular expression to replace all the occurrences in the string.

Similar toreplaceFirst, This method uses compile() and matcher() methods to extract a string using a regular expression and also throws a PatternSyntaxException when the regular expression is invalid.

We will use a regular expression that extracts all numbers from a string and replaces all the occurrences with a number.

\d – This is a regular expression that matches any digit from 0 to 9.

Output:

Peter is 30 years old and Jane is 30 years old

If you want to remove substring from String, you can use empty String with replaceAll() method

Here is an example:

Output:

Peter is years old, and Jane is years old

Using StringBuilder’s delete() method to remove substring from String in Java

The StringBuilder contains a mutable sequence of characters used to modify a string by adding and removing characters.

The empty constructor of StringBuilder creates a string builder with an initial capacity of 16 characters, and if the internal buffer overflows, it is automatically made larger.

The delete() method accepts two int parameters indicating the start and the end of the substring to be removed from the string.

The start index is inclusive, while the last index is exclusive as it deducts 1 from the second parameter.

When the start is equal to the end, no changes are made, and a StringIndexOutOfBoundsException is thrown when the start is negative, greater than the length of the string, or greater than the end of the string.

Output:

Abdul quit alcohol

Using StringBuilder’s replace() method to remove substring from String in Java

The replace() method is similar to the delete() method, only that it has a third parameter that replaces the characters that have been removed from the string.

If the string you want to replace is large, the size will be increased to accommodate its length.

This method also returns a StringBuilder, and you can use the toString() method to print out the modified string.

Output:

The bike broke down on a hill

Conclusion

In this tutorial, you have learned how to remove a substring from a string by replacing and deleting characters. The methods covered include using string replace(),replaceFirst(), replaceAll() and finally using StringBuilder delete() and replace() methods.

Related Posts

  • Java String contains IgnoreCase
    23 June

    Java String contains IgnoreCase

    Table of ContentsUsing String.toLowerCase()Using Pattern.compile()Using String’s MatchesUsing StringUtils.containsIgnoreCase()Complete program for Java String contains IgnoreCaseConclusion We often have to check if a string contains a substring or not in a case-insensitive manner. There are multiple ways to do so. In this article, we will take a look at these methods for java String contains IgnoreCase checks. […]

  • Find and count occurences of substring in String in Java
    02 June

    Find and count occurrences of substring in string in java

    Table of ContentsUsing the indexOf() methodUsing regular expressionUsing split() methodUsing the countMatches() method from Apache Commons library In this post, we will see how to find and count occurrences of substring in string in java. Using the indexOf() method The indexOf() method in java is a specialized function to find the index of the first […]

  • Print double quotes in java
    23 May

    Print double quotes in java

    Table of ContentsPrint double quotes in java using backslash characterPrint double quotes in java using unicode characterPrint double quotes in java by appending double quote character 💡 Outline You can print double quotes in java by escape double quotes using backslash character(\). [crayon-60f8d65414acf134719410/] When you print on console using System.out.println, you use double quotes and […]

  • Java Split String by Space
    13 May

    Java split string by space

    Learn about how to split String in java in different ways.

  • Split String by new line in java
    09 May

    How to split String by newline in java

    Learn about how to split String by newline in java

  • 15 April

    How to declare a String array in java

    Learn about how to declare String array in java.

Leave a Reply

Your email address will not be published. Required fields are marked *

Subscribe to our newletter

Get quality tutorials to your inbox. Subscribe now.