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?

Leave a Reply

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