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

  • 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 JavaWas this post helpful? 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 […]

  • 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?Was this post helpful? In this post, we will see how […]

  • 13 July

    Remove substring from String in Java

    Table of ContentsUsing String’s replace method to remove substring from String in JavaUsing String’s replaceFirst method to remove substring from String in JavaUsing String’s replaceAll method to remove substring from String in JavaUsing StringBuilder’s delete() method to remove substring from String in JavaUsing StringBuilder’s replace() method to remove substring from String in JavaConclusionWas this post […]

  • Java String contains IgnoreCase
    23 June

    Java String contains Ignore Case

    Table of ContentsUsing String.toLowerCase()Using Pattern.compile()Using String’s MatchesUsing StringUtils.containsIgnoreCase()Complete program for Java String contains IgnoreCaseConclusionWas this post helpful? 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 […]

  • 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 libraryWas this post helpful? 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 […]

  • 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 characterWas this post helpful? 💡 Outline You can print double quotes in java by escape double quotes using backslash character(\). [crayon-62dfe6a7ebd6e381110151/] When you print on console using System.out.println, you use […]

Leave a Reply

Your email address will not be published.

Subscribe to our newletter

Get quality tutorials to your inbox. Subscribe now.