Remove Parentheses From String in Java

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 memory locations. The String class implements the CharSequence interface along with other interfaces.

Strings are a constant data structure and hence their values can not be changed once created.

However, there are several methods implemented within the class that makes working with Java Strings easier and more robust.

There are two ways in which you can remove the parentheses from a String in Java.

  • You can traverse the whole string and append the characters other than parentheses to the new String.
  • You can use the replaceAll() method of the String class to remove all the occurrences of parentheses.

Let us see each one of them separately.

Remove Parentheses From a String Using the replaceAll() Method

Yet another method to remove the parentheses from the Java String is to invoke the replaceAll() method.

The replaceAll() method is a method of the String class. You can invoke the method on an instance of the String class.

Let us see the definition of the method.

  • The method accepts two arguments.
    • String regex: This argument is a regular expression. You shall input a regular expression to the method. The method checks the passed regex within the original string.
    • Regular expressions are a set of characters that represent a pattern in a text.
    • String replacement: This argument represents the string that will be inserted in the place of the matched pattern.

For example, let us say you have a string “{a}[b(c)d]”. In order to replace the parentheses, you shall pass a regular expression (“[\[\](){}]”) and the empty string ‘replacement’.

This will replace all the parentheses with a blank string. In essence, the parentheses will be removed.

You can read more about regex here.

Note that the replaceAll() method returns a string. Therefore, you must reassign the returned string to the original string in order to reflect the changes in the original string.

Let us see the code.

Output:

New string is: abcd

Remove Parentheses From a String by Traversing

The idea behind removing the parentheses using this method is simple. You can traverse the String using a loop and pick characters only if they are not parentheses.

You can follow the steps given below to perform the aforementioned task.

  • Take a temporary empty string.
  • Traverse through the string using a loop.
  • Extract the characters from the string one at a time using the index.
  • Check if the current character is a parenthesis using the comparison operator (==).
    • If yes, simply ignore the character.
    • Otherwise, append the character to a temporary string.
  • After the loop completes, copy the temporary string to the original string.

Note that you can not directly fetch the character from a Java String using the index inside the square brackets. For that purpose, you need to invoke the charAt() method by passing the index to it.

The definition of the charAt() method is given below.

The method returns the character at the respective index.

To append the character, you can directly use the concatenation operator (+).

Let us see the code.

Note that you need to reassign the string to itself after concatenation because the Java Strings are immutable and you can not make changes in place.

Also, note the length() method. It returns the number of characters in the string.

Output:

The new string is: abcd

Conclusion

The replaceAll() method to remove the occurrence of parentheses is more robust as well as simple in terms of complexities.

This is all about removing the parentheses from a string in Java.

Hope you enjoyed reading the article. Stay tuned for more such articles. Happy Learning!

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-63daab8fcc391965609550/] [crayon-63daab8fcc394384016407/] Output: 1 […]

  • 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. […]

  • Find character in String in java
    02 February

    Find Character in String in Java

    Table of ContentsHow to Find Character in String in Java1. Using indexOf() Method to Find Character in String in Java2. Using lastIndexOf() Method to Find Char in String in JavaFind Character at Index in String in Javahow Do I Find Word in String in Java? In this post, we will see how to find character […]

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.