{"id":89400,"date":"2025-08-05T18:00:59","date_gmt":"2025-08-05T12:30:59","guid":{"rendered":"https:\/\/techvidvan.com\/tutorials\/?p=89400"},"modified":"2025-08-05T18:12:33","modified_gmt":"2025-08-05T12:42:33","slug":"java-string-replaceall-method","status":"publish","type":"post","link":"https:\/\/techvidvan.com\/tutorials\/java-string-replaceall-method\/","title":{"rendered":"Java String replaceAll() Method"},"content":{"rendered":"<p>Java&#8217;s String replaceAll function creates a new string with characters that are related to a supplied string, a defined value, or a regex expression.<\/p>\n<h2>Explanation<\/h2>\n<p>The replaceAll() method of the Java String class returns a string that replaces all the characters in the regex and replacement string sequence.<\/p>\n<p>Each substring in the string that matches the regex is replaced with the supplied text using the replaceAll() method.<\/p>\n<p><strong>Syntax:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">public String replaceAll(String regex, String replace_str)<\/pre>\n<h3>Internal implementation<\/h3>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">public String replaceAll(String regex, String replacement) {  \r\n        return\r\nPattern.compile(regex).matcher(this).replaceAll(replacement);  \r\n}<\/pre>\n<p><strong>Parameter:<\/strong><\/p>\n<ul>\n<li><strong>regex:<\/strong> the regular expression that will be used to match this string.<\/li>\n<li><strong>replace_str:<\/strong> the string that will be used to replace the found expression.<\/li>\n<\/ul>\n<p><strong>Return Value:<\/strong><\/p>\n<ul>\n<li>This method gives back the finished String.<\/li>\n<li>Returns a new string that replaces every instance of the matching substring with the replacement string.<\/li>\n<\/ul>\n<p><strong>Diagram:<\/strong><\/p>\n<p><a href=\"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/sites\/2\/2023\/11\/Internal-implementation.webp\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-89673 size-full\" src=\"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/sites\/2\/2023\/11\/Internal-implementation.webp\" alt=\"Internal implementation\" width=\"400\" height=\"359\" \/><\/a><\/p>\n<h4>Example:<\/h4>\n<p><strong>For instance, to replace all occurrences of a single character:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">public class TechVidvanReplaceAll{  \r\npublic static void main(String args[]){  \r\nString s1=\"Good things takes time\";  \r\nString replaceString=s1.replaceAll(\"o\",\"e\");\r\nSystem.out.println(replaceString);  \r\n}\r\n}<\/pre>\n<p><strong>Output:<\/strong><br \/>\nGood things takes time<\/p>\n<h4>Example:<\/h4>\n<p><strong>For example, to replace all occurrences of a single word or collection of terms:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">public class TechVidvanReplaceAll{  \r\npublic static void main(String args[]){  \r\nString s1=\"My name is Jaanu, her name is Nithu.\";  \r\nString replaceString=s1.replaceAll(\"is\",\"was\");  \r\nSystem.out.println(replaceString); \r\n}\r\n}<\/pre>\n<p><strong>Output:<\/strong><br \/>\nMy name was Jaanu, her name was Nithu.<\/p>\n<h4>Example:<\/h4>\n<p><strong>Inserting spaces between characters was done by using the replaceAll() method:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">public class ReplaceAllExample5   \r\n{  \r\n\/\/ main method  \r\npublic static void main(String argvs[])  \r\n{  \r\n  \r\n\/\/ input string  \r\nString str = \"Cricket\";  \r\nSystem.out.println(str);  \r\n  \r\nString regex = \"\";  \r\n\/\/ adding a white space before and after every character of the input string.  \r\nstr = str.replaceAll(regex, \" \");  \r\n  \r\nSystem.out.println(str);  \r\n  \r\n}  \r\n}<\/pre>\n<p><strong>Output:<\/strong><br \/>\nCricket<br \/>\nC r i c k e t<\/p>\n<h3>Escaping Characters in replaceAll()<\/h3>\n<p>Whether it is a regex or a regular string, the first argument to the replaceAll() method is up to you, mainly because a regular expression is a typical string.<\/p>\n<p>Regex contains characters with special meanings.<\/p>\n<p><strong>The metacharacters are as follows:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">\\ ^ $ . | ? * + {} [] ()<\/pre>\n<p>Use either the method or the replace() method to match the substring that contains metacharacters.<\/p>\n<h3>Difference between replaceAll() and replace()<\/h3>\n<ul>\n<li>The ReplaceAll() method substitutes a new value for every occurrence of a supplied substring. On the other hand, the Replace() function is used to replace particular characters in a string with a new value. It merely swaps out the substring&#8217;s first &#8220;n&#8221; occurrences.<\/li>\n<li>While the replace() method substitutes a single character, the replaceAll() function replaces a pattern (using regex). On the other hand, the replace() method uses a String as a replacement.<\/li>\n<\/ul>\n<h3>How to replace all characters?<\/h3>\n<ul>\n<li><strong>String.prototype.replaceAll(): <\/strong>A new string is produced by the replaceAll() method of the String data type, and it contains replacements for all pattern matches.<\/li>\n<li>The replacement can be a string or a function that is called for each match, and the pattern can be either a string or a RegExp. The first string is not altered.<\/li>\n<\/ul>\n<h3>Special Characters<\/h3>\n<ul>\n<li>Be aware that the outcomes might differ from what they would be if the replacement string were treated as a literal replacement string if it contained dollar signs ($) or backslashes ().<\/li>\n<\/ul>\n<h3>Exception of String replaceAll()<\/h3>\n<p>Although the String replaceAll method in Java is quite effective, it does have some limitations.<\/p>\n<ul>\n<li><strong>Invalid regex: <\/strong>When an invalid regex is passed to the replaceAll() method, a PatternSyntaxException with the name Invalid regex is raised.<\/li>\n<li><strong>Null regular expression:<\/strong> The replaceAll() method throws a NullPointerException and does not accept null regular expressions.<\/li>\n<\/ul>\n<p><strong>The four primary ways to handle this exception:<\/strong><\/p>\n<table>\n<tbody>\n<tr>\n<td><span style=\"font-weight: 400;\">public String getMessage()<\/span><\/td>\n<td><span style=\"font-weight: 400;\">The description of the exception is included.<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400;\">public int getIndex()<\/span><\/td>\n<td><span style=\"font-weight: 400;\">It will return the error of the index.<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400;\">public String getPattern()<\/span><\/td>\n<td><span style=\"font-weight: 400;\">It will return the regular expression that contains the error.<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400;\">public String getDescription()<\/span><\/td>\n<td><span style=\"font-weight: 400;\">It will provide us with a decryption for the error.<\/span><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<h3>Usage of string replaceAll()<\/h3>\n<ul>\n<li>Substrings should be replaced with different strings wherever they appear.<\/li>\n<li>It is typically employed to eliminate characters or their pattern from a String.<\/li>\n<li>It can easily escape in particular expressions by using the replaceAll() method.<\/li>\n<\/ul>\n<h3>Advantages of String replaceAll()<\/h3>\n<ul>\n<li><strong>Replace:<\/strong> It replaces every instance of the given pattern in a string and is repeatable.<\/li>\n<li><strong>Maintain:<\/strong> It keeps the original string intact while returning a new string with replacement characters..<\/li>\n<li><strong>Wide range:<\/strong> It offers a selection of techniques for carrying out some replacements.<\/li>\n<\/ul>\n<h3>Disadvantages of String replaceAll()<\/h3>\n<ul>\n<li><strong>Complexity:<\/strong> It contributes to errors in complex expressions and makes the code challenging to understand and maintain.<\/li>\n<li><strong>Escaping:<\/strong> Because special characters make code harder to read, they should be correctly escaped.<\/li>\n<li><strong>Not Appropriate:<\/strong> It is not appropriate for single occurrences and substring replacements.<\/li>\n<\/ul>\n<h3>Conclusion<\/h3>\n<p>We have discussed the String replaceAll() in Java. In extensively learned about its uses and function of replaceAll().<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Java&#8217;s String replaceAll function creates a new string with characters that are related to a supplied string, a defined value, or a regex expression. Explanation The replaceAll() method of the Java String class returns&#46;&#46;&#46;<\/p>\n","protected":false},"author":6,"featured_media":447363,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[183],"tags":[296,262,299,263,250,5559,5558],"class_list":["post-89400","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-java","tag-java","tag-java-string-replaceall","tag-java-tutorial","tag-java-tutorial-for-beginners","tag-learn-java","tag-string-replaceall","tag-string-replaceall-in-java"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.7 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Java String replaceAll() Method - TechVidvan<\/title>\n<meta name=\"description\" content=\"The replaceAll() method of the Java String class returns a string that replaces all the characters in the regex and replacement string sequence.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/techvidvan.com\/tutorials\/java-string-replaceall-method\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Java String replaceAll() Method - TechVidvan\" \/>\n<meta property=\"og:description\" content=\"The replaceAll() method of the Java String class returns a string that replaces all the characters in the regex and replacement string sequence.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/techvidvan.com\/tutorials\/java-string-replaceall-method\/\" \/>\n<meta property=\"og:site_name\" content=\"TechVidvan\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/TechVidvan\/\" \/>\n<meta property=\"article:published_time\" content=\"2025-08-05T12:30:59+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2025-08-05T12:42:33+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2024\/05\/java-String-replaceAll.webp\" \/>\n\t<meta property=\"og:image:width\" content=\"1200\" \/>\n\t<meta property=\"og:image:height\" content=\"628\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/webp\" \/>\n<meta name=\"author\" content=\"TechVidvan Team\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@vidvantech\" \/>\n<meta name=\"twitter:site\" content=\"@vidvantech\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"TechVidvan Team\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"4 minutes\" \/>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Java String replaceAll() Method - TechVidvan","description":"The replaceAll() method of the Java String class returns a string that replaces all the characters in the regex and replacement string sequence.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/techvidvan.com\/tutorials\/java-string-replaceall-method\/","og_locale":"en_US","og_type":"article","og_title":"Java String replaceAll() Method - TechVidvan","og_description":"The replaceAll() method of the Java String class returns a string that replaces all the characters in the regex and replacement string sequence.","og_url":"https:\/\/techvidvan.com\/tutorials\/java-string-replaceall-method\/","og_site_name":"TechVidvan","article_publisher":"https:\/\/www.facebook.com\/TechVidvan\/","article_published_time":"2025-08-05T12:30:59+00:00","article_modified_time":"2025-08-05T12:42:33+00:00","og_image":[{"width":1200,"height":628,"url":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2024\/05\/java-String-replaceAll.webp","type":"image\/webp"}],"author":"TechVidvan Team","twitter_card":"summary_large_image","twitter_creator":"@vidvantech","twitter_site":"@vidvantech","twitter_misc":{"Written by":"TechVidvan Team","Est. reading time":"4 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/techvidvan.com\/tutorials\/java-string-replaceall-method\/#article","isPartOf":{"@id":"https:\/\/techvidvan.com\/tutorials\/java-string-replaceall-method\/"},"author":{"name":"TechVidvan Team","@id":"https:\/\/techvidvan.com\/tutorials\/#\/schema\/person\/dde481bb412350cde1ed6e389bc0deaf"},"headline":"Java String replaceAll() Method","datePublished":"2025-08-05T12:30:59+00:00","dateModified":"2025-08-05T12:42:33+00:00","mainEntityOfPage":{"@id":"https:\/\/techvidvan.com\/tutorials\/java-string-replaceall-method\/"},"wordCount":682,"commentCount":0,"publisher":{"@id":"https:\/\/techvidvan.com\/tutorials\/#organization"},"image":{"@id":"https:\/\/techvidvan.com\/tutorials\/java-string-replaceall-method\/#primaryimage"},"thumbnailUrl":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2024\/05\/java-String-replaceAll.webp","keywords":["java","java string replaceAll()","Java Tutorial","java tutorial for beginners","Learn Java","string replaceAll()","string replaceAll() in java"],"articleSection":["Java Tutorials"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/techvidvan.com\/tutorials\/java-string-replaceall-method\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/techvidvan.com\/tutorials\/java-string-replaceall-method\/","url":"https:\/\/techvidvan.com\/tutorials\/java-string-replaceall-method\/","name":"Java String replaceAll() Method - TechVidvan","isPartOf":{"@id":"https:\/\/techvidvan.com\/tutorials\/#website"},"primaryImageOfPage":{"@id":"https:\/\/techvidvan.com\/tutorials\/java-string-replaceall-method\/#primaryimage"},"image":{"@id":"https:\/\/techvidvan.com\/tutorials\/java-string-replaceall-method\/#primaryimage"},"thumbnailUrl":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2024\/05\/java-String-replaceAll.webp","datePublished":"2025-08-05T12:30:59+00:00","dateModified":"2025-08-05T12:42:33+00:00","description":"The replaceAll() method of the Java String class returns a string that replaces all the characters in the regex and replacement string sequence.","breadcrumb":{"@id":"https:\/\/techvidvan.com\/tutorials\/java-string-replaceall-method\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/techvidvan.com\/tutorials\/java-string-replaceall-method\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/techvidvan.com\/tutorials\/java-string-replaceall-method\/#primaryimage","url":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2024\/05\/java-String-replaceAll.webp","contentUrl":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2024\/05\/java-String-replaceAll.webp","width":1200,"height":628,"caption":"java string replaceAll()"},{"@type":"BreadcrumbList","@id":"https:\/\/techvidvan.com\/tutorials\/java-string-replaceall-method\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/techvidvan.com\/tutorials\/"},{"@type":"ListItem","position":2,"name":"Java String replaceAll() Method"}]},{"@type":"WebSite","@id":"https:\/\/techvidvan.com\/tutorials\/#website","url":"https:\/\/techvidvan.com\/tutorials\/","name":"TechVidvan Blogs","description":"","publisher":{"@id":"https:\/\/techvidvan.com\/tutorials\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/techvidvan.com\/tutorials\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/techvidvan.com\/tutorials\/#organization","name":"TechVidvan","url":"https:\/\/techvidvan.com\/tutorials\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/techvidvan.com\/tutorials\/#\/schema\/logo\/image\/","url":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2024\/03\/techvidvan-logo-200x50-1.webp","contentUrl":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2024\/03\/techvidvan-logo-200x50-1.webp","width":200,"height":50,"caption":"TechVidvan"},"image":{"@id":"https:\/\/techvidvan.com\/tutorials\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/TechVidvan\/","https:\/\/x.com\/vidvantech"]},{"@type":"Person","@id":"https:\/\/techvidvan.com\/tutorials\/#\/schema\/person\/dde481bb412350cde1ed6e389bc0deaf","name":"TechVidvan Team"}]}},"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/posts\/89400","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/users\/6"}],"replies":[{"embeddable":true,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/comments?post=89400"}],"version-history":[{"count":4,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/posts\/89400\/revisions"}],"predecessor-version":[{"id":447843,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/posts\/89400\/revisions\/447843"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/media\/447363"}],"wp:attachment":[{"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/media?parent=89400"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/categories?post=89400"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/tags?post=89400"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}