{"id":20232,"date":"2022-06-11T16:41:57","date_gmt":"2022-06-11T11:11:57","guid":{"rendered":"https:\/\/java2blog.com\/?p=20232"},"modified":"2022-06-11T16:41:57","modified_gmt":"2022-06-11T11:11:57","slug":"remove-parentheses-from-string-java","status":"publish","type":"post","link":"https:\/\/java2blog.com\/remove-parentheses-from-string-java\/","title":{"rendered":"Remove Parentheses From String in Java"},"content":{"rendered":"<div id=\"toc_container\" class=\"toc_light_blue no_bullets\"><p class=\"toc_title\">Table of Contents<\/p><ul class=\"toc_list\"><li><a href=\"#Java_Strings\">Java Strings<\/a><\/li><li><a href=\"#Remove_Parentheses_From_a_String_Using_the_replaceAll_Method\">Remove Parentheses From a String Using the replaceAll() Method<\/a><\/li><li><a href=\"#Remove_Parentheses_From_a_String_by_Traversing\">Remove Parentheses From a String by Traversing<\/a><\/li><li><a href=\"#Conclusion\">Conclusion<\/a><\/li><\/ul><\/div>\n<p>Java uses the Strings data structure to store the text data. This article discusses methods to remove parentheses from a String in Java.<\/p>\n<h2><span id=\"Java_Strings\">Java Strings<\/span><\/h2>\n<p>Java Strings is a class that stores the text data at contiguous memory locations. The String class implements the <code>CharSequence<\/code> interface along with other <a href=\"https:\/\/java2blog.com\/interface-in-java-with-example\/\" title=\"interfaces\">interfaces<\/a>.<\/p>\n<p>Strings are a constant data structure and hence their values can not be changed once created. <\/p>\n<p>However, there are several methods implemented within the class that makes working with Java Strings easier and more robust.<\/p>\n<p>There are two ways in which you can remove the parentheses from a String in Java.<\/p>\n<ul>\n<li>You can traverse the whole string and append the characters other than parentheses to the new String.<\/li>\n<li>You can use the <code>replaceAll()<\/code> method of the String class to remove all the occurrences of parentheses.<\/li>\n<\/ul>\n<p>Let us see each one of them separately.<\/p>\n<h2><span id=\"Remove_Parentheses_From_a_String_Using_the_replaceAll_Method\">Remove Parentheses From a String Using the replaceAll() Method<\/span><\/h2>\n<p>Yet another method to remove the parentheses from the Java String is to invoke the <code>replaceAll()<\/code> method.<\/p>\n<p>The <code>replaceAll()<\/code> method is a method of the String class. You can invoke the method on an instance of the String class. <\/p>\n<p>Let us see the definition of the method.<\/p>\n<pre code=\"java\" title=\"replaceAll() method definition\">\npublic String replaceAll(String regex,\n                String replacement)\n                <\/pre>\n<ul>\n<li>The method accepts two arguments.\n<ul>\n<li>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. <\/li>\n<li>Regular expressions are a set of characters that represent a pattern in a text.<\/li>\n<li>String replacement: This argument represents the string that will be inserted in the place of the matched pattern.<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<p>For example, let us say you have a string \u201c{a}[b(c)d]\u201d. In order to replace the parentheses, you shall pass a regular expression (\u201c[\\[\\](){}]\u201d) and the empty string \u2018replacement\u2019. <\/p>\n<p>This will replace all the parentheses with a blank string. In essence, the parentheses will be removed.<\/p>\n<p>You can read more about regex <a href=\"https:\/\/docs.oracle.com\/javase\/tutorial\/essential\/regex\/\" target=\"_blank\" rel=\"noopener\">here<\/a>.<\/p>\n<p>Note that the <code>replaceAll()<\/code> 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.<\/p>\n<p>Let us see the code.<\/p>\n<pre code=\"java\" title=\"Using replaceAll() method\">\npackage java2blog;\n\npublic class parRemove \n{\n    public static void main(String [] args)\n    {\n        String str = \"{a}[b(c)d]\";\n        str = str.replaceAll(\"[\\\\[\\\\](){}]\", \"\");\n        System.out.println(\"New string is: \"+str);\n    }\n}\n<\/pre>\n<p>Output:<\/p>\n<div class=\"content-box-green\">\nNew string is: abcd\n<\/div>\n<section class=\"read-more-posts\">\n<div class=\"rm-header\">\n<h2>Further reading:<\/h2>\n<\/div>\n<div class=\"rm-wrap\">\n<div class=\"rm-item\">\n<h5><a href=\"https:\/\/java2blog.com\/remove-comma-from-string-java\/\" target=\"_blank\" rel=\"noopener\">Remove Comma from String in Java<\/a><\/h5>\n<div class=\"ex\">\n            <a href=\"https:\/\/java2blog.com\/remove-comma-from-string-java\/\" target=\"_blank\" rel=\"noopener\">Read more \u2192<\/a>\n        <\/div>\n<\/div>\n<div class=\"rm-item\">\n<h5><a href=\"https:\/\/java2blog.com\/remove-all-white-spaces-from-string-in-java\/\" target=\"_blank\" rel=\"noopener\">How to remove all white spaces from String in java<\/a><\/h5>\n<div class=\"ex\">\n            <a href=\"https:\/\/java2blog.com\/remove-all-white-spaces-from-string-in-java\/\" target=\"_blank\" rel=\"noopener\">Read more \u2192<\/a>\n        <\/div>\n<\/p><\/div>\n<\/div>\n<\/section>\n<h2><span id=\"Remove_Parentheses_From_a_String_by_Traversing\">Remove Parentheses From a String by Traversing<\/span><\/h2>\n<p>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. <\/p>\n<p>You can follow the steps given below to perform the aforementioned task.<\/p>\n<ul>\n<li>Take a temporary empty string.<\/li>\n<li>Traverse through the string using a loop.<\/li>\n<li>Extract the characters from the string one at a time using the index.<\/li>\n<li>Check if the current character is a parenthesis using the comparison operator (==).\n<ul>\n<li>If yes, simply ignore the character.<\/li>\n<li>Otherwise, append the character to a temporary string.<\/li>\n<\/ul>\n<\/li>\n<li>After the loop completes, copy the temporary string to the original string.<\/li>\n<\/ul>\n<p>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 <code>charAt()<\/code> method by passing the index to it.<\/p>\n<p>The definition of the <code>charAt()<\/code> method is given below.<\/p>\n<pre code=\"java\" title=\"chatAt() method definition\">\npublic char charAt(int index)\n<\/pre>\n<p>The method returns the character at the respective index. <\/p>\n<p>To append the character, you can directly use the concatenation operator (+).<\/p>\n<p>Let us see the code.<\/p>\n<pre code=\"java\" title=\"Traversing the string to remove parantheses\">\npackage java2blog;\n\npublic class parRemove \n{\n    public static void main(String [] args)\n    {\n        String str = \"{a}[b(c)d]\";\n        String temp=\"\";\n        for(int i=0;i<str.length();i++)\n        {\n            char ch = str.charAt(i);\n            if(ch=='(' || ch==')' || ch=='[' || ch==']' || ch=='{' || ch=='}')\n            {\n                continue;\n            }\n            else\n            {\n                temp = temp+ch;\n            }\n        }\n        str = temp;\n        System.out.println(\"The new string is: \"+str);\n    }\n}\n<\/pre>\n<blockquote>\n<p>Note that you need to reassign the string to itself after concatenation because the <a href=\"https:\/\/java2blog.com\/why-string-is-immutable-in-java\" title=\"Java Strings are immutable\">Java Strings are immutable<\/a> and you can not make changes in place.<\/p>\n<\/blockquote>\n<p>Also, note the <code>length()<\/code> method. It returns the number of characters in the string. <\/p>\n<p>Output:<\/p>\n<div class=\"content-box-green\">\nThe new string is: abcd\n<\/div>\n<h2><span id=\"Conclusion\">Conclusion<\/span><\/h2>\n<p>The <code>replaceAll()<\/code> method to remove the occurrence of parentheses is more robust as well as simple in terms of complexities.<\/p>\n<p>This is all about removing the parentheses from a string in Java.<\/p>\n<p>Hope you enjoyed reading the article. Stay tuned for more such articles. Happy Learning!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>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 [&hellip;]<\/p>\n","protected":false},"author":36,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"inline_featured_image":false,"_mi_skip_tracking":false},"categories":[27,5],"tags":[],"_links":{"self":[{"href":"https:\/\/java2blog.com\/wp-json\/wp\/v2\/posts\/20232"}],"collection":[{"href":"https:\/\/java2blog.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/java2blog.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/java2blog.com\/wp-json\/wp\/v2\/users\/36"}],"replies":[{"embeddable":true,"href":"https:\/\/java2blog.com\/wp-json\/wp\/v2\/comments?post=20232"}],"version-history":[{"count":0,"href":"https:\/\/java2blog.com\/wp-json\/wp\/v2\/posts\/20232\/revisions"}],"wp:attachment":[{"href":"https:\/\/java2blog.com\/wp-json\/wp\/v2\/media?parent=20232"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/java2blog.com\/wp-json\/wp\/v2\/categories?post=20232"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/java2blog.com\/wp-json\/wp\/v2\/tags?post=20232"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}