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.

Was this post helpful?

Related Posts

  • 28 October

    Convert UUID to String in Java

    Table of ContentsIntroductionUUID class in JavaConvert UUID to String in Java Introduction In this article, we will have a look on How to Convert UUID to String in Java. We will also shed some light on the concept of UUID, its use, and its corresponding representation in Java class. Let us have a quick look […]

  • 27 September

    Repeat String N times in Java

    Table of ContentsIntroductionWhat is a String in Java?Repeat String N times in JavaSimple For Loop to Repeat String N Times in JavaUsing Recursion to Repeat String N Times in JavaString.format() method to Repeat String N Times in JavaUsing String.repeat() method in Java 11 to Repeat String N TimesRegular Expression – Regex to Repeat String N […]

  • 15 September

    How to Replace Space with Underscore in Java

    Table of ContentsReplace space with underscore in java1. Using replace() method2. Using replaceAll() method Learn about how to replace space with underscore in java. Replace space with underscore in java 1. Using replace() method Use String’s replace() method to replace space with underscore in java. String’s replace() method returns a string replacing all the CharSequence […]

  • 15 September

    How to Replace Comma with Space in Java

    Table of ContentsReplace comma with space in java1. Using replace() method2. Using replaceAll() method Learn about how to replace comma with space in java. Replace comma with space in java 1. Using replace() method Use String’s replace() method to replace comma with space in java. Here is syntax of replace() method: [crayon-63dcf353f26dc448798253/] [crayon-63dcf353f26e2573250736/] Output: 1 […]

  • 11 June

    Remove Parentheses From String in Java

    Table of ContentsJava StringsRemove Parentheses From a String Using the replaceAll() MethodRemove Parentheses From a String by TraversingConclusion Java uses the Strings data structure to store the text data. This article discusses methods to remove parentheses from a String in Java. Java Strings Java Strings is a class that stores the text data at contiguous […]

  • Escape percent sign in java
    08 February

    Escape percent sign in String’s format method in java

    Table of ContentsEscape Percent Sign in String’s Format Method in JavaEscape Percent Sign in printf() Method in Java In this post, we will see how to escape Percent sign in String’s format() method in java. Escape Percent Sign in String’s Format Method in Java String’s format() method uses percent sign(%) as prefix of format specifier. […]

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.