Title: Java String Replace and ReplaceAll - Examples Java Code Geeks - 2026
Open Graph Title: Java String Replace and ReplaceAll - Examples Java Code Geeks - 2026
Description: In this example, we are going to see how to use the String Replace and ReplaceAll String class API Methods in Java. With replaceAll, you can replace all
Open Graph Description: In this example, we are going to see how to use the String Replace and ReplaceAll String class API Methods in Java. With replaceAll, you can replace all
Mail addresses
support@javacodegeeks.com
Opengraph URL: https://examples.javacodegeeks.com/java-string-replace-and-replaceall-string-methods/
Generator: WordPress 6.9.4
Domain: examples.javacodegeeks.com
{"@context":"https://schema.org","@graph":[{"@type":"Article","@id":"https://examples.javacodegeeks.com/java-string-replace-and-replaceall-string-methods/#article","isPartOf":{"@id":"https://examples.javacodegeeks.com/java-string-replace-and-replaceall-string-methods/"},"author":{"name":"Nikos Maravitsas","@id":"https://examples.javacodegeeks.com/#/schema/person/686a494fbb577a8d1231862b139cda6b"},"headline":"Java String Replace and ReplaceAll String Methods","datePublished":"2014-04-01T15:01:48+00:00","dateModified":"2020-06-15T11:04:26+00:00","mainEntityOfPage":{"@id":"https://examples.javacodegeeks.com/java-string-replace-and-replaceall-string-methods/"},"wordCount":347,"commentCount":1,"publisher":{"@id":"https://examples.javacodegeeks.com/#organization"},"image":{"@id":"https://examples.javacodegeeks.com/java-string-replace-and-replaceall-string-methods/#primaryimage"},"thumbnailUrl":"https://examples.javacodegeeks.com/wp-content/uploads/2012/12/java-logo.jpg","articleSection":["String"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https://examples.javacodegeeks.com/java-string-replace-and-replaceall-string-methods/#respond"]}]},{"@type":"WebPage","@id":"https://examples.javacodegeeks.com/java-string-replace-and-replaceall-string-methods/","url":"https://examples.javacodegeeks.com/java-string-replace-and-replaceall-string-methods/","name":"Java String Replace and ReplaceAll - Examples Java Code Geeks - 2026","isPartOf":{"@id":"https://examples.javacodegeeks.com/#website"},"primaryImageOfPage":{"@id":"https://examples.javacodegeeks.com/java-string-replace-and-replaceall-string-methods/#primaryimage"},"image":{"@id":"https://examples.javacodegeeks.com/java-string-replace-and-replaceall-string-methods/#primaryimage"},"thumbnailUrl":"https://examples.javacodegeeks.com/wp-content/uploads/2012/12/java-logo.jpg","datePublished":"2014-04-01T15:01:48+00:00","dateModified":"2020-06-15T11:04:26+00:00","description":"In this example, we are going to see how to use the String Replace and ReplaceAll String class API Methods in Java. With replaceAll, you can replace all","breadcrumb":{"@id":"https://examples.javacodegeeks.com/java-string-replace-and-replaceall-string-methods/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https://examples.javacodegeeks.com/java-string-replace-and-replaceall-string-methods/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https://examples.javacodegeeks.com/java-string-replace-and-replaceall-string-methods/#primaryimage","url":"https://examples.javacodegeeks.com/wp-content/uploads/2012/12/java-logo.jpg","contentUrl":"https://examples.javacodegeeks.com/wp-content/uploads/2012/12/java-logo.jpg","width":150,"height":150,"caption":"Bipartite Graph"},{"@type":"BreadcrumbList","@id":"https://examples.javacodegeeks.com/java-string-replace-and-replaceall-string-methods/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https://examples.javacodegeeks.com/"},{"@type":"ListItem","position":2,"name":"Java Development","item":"https://examples.javacodegeeks.com/category/java-development/"},{"@type":"ListItem","position":3,"name":"Core Java","item":"https://examples.javacodegeeks.com/category/java-development/core-java/"},{"@type":"ListItem","position":4,"name":"lang","item":"https://examples.javacodegeeks.com/category/java-development/core-java/lang/"},{"@type":"ListItem","position":5,"name":"String","item":"https://examples.javacodegeeks.com/category/java-development/core-java/lang/string/"},{"@type":"ListItem","position":6,"name":"Java String Replace and ReplaceAll String Methods"}]},{"@type":"WebSite","@id":"https://examples.javacodegeeks.com/#website","url":"https://examples.javacodegeeks.com/","name":"Java Code Geeks","description":"Java Examples and Code Snippets","publisher":{"@id":"https://examples.javacodegeeks.com/#organization"},"alternateName":"JCG","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https://examples.javacodegeeks.com/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https://examples.javacodegeeks.com/#organization","name":"Exelixis Media P.C.","url":"https://examples.javacodegeeks.com/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https://examples.javacodegeeks.com/#/schema/logo/image/","url":"https://examples.javacodegeeks.com/wp-content/uploads/2022/06/exelixis-logo.png","contentUrl":"https://examples.javacodegeeks.com/wp-content/uploads/2022/06/exelixis-logo.png","width":864,"height":246,"caption":"Exelixis Media P.C."},"image":{"@id":"https://examples.javacodegeeks.com/#/schema/logo/image/"},"sameAs":["https://www.facebook.com/javacodegeeks","https://x.com/javacodegeeks"]},{"@type":"Person","@id":"https://examples.javacodegeeks.com/#/schema/person/686a494fbb577a8d1231862b139cda6b","name":"Nikos Maravitsas","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https://examples.javacodegeeks.com/#/schema/person/image/","url":"https://examples.javacodegeeks.com/wp-content/uploads/2013/10/Nikos-Maravitsas-80x96.jpg","contentUrl":"https://examples.javacodegeeks.com/wp-content/uploads/2013/10/Nikos-Maravitsas-80x96.jpg","caption":"Nikos Maravitsas"},"description":"Nikos has graduated from the Department of Informatics and Telecommunications of The National and Kapodistrian University of Athens. During his studies he discovered his interests about software development and he has successfully completed numerous assignments in a variety of fields. Currently, his main interests are system’s security, parallel systems, artificial intelligence, operating systems, system programming, telecommunications, web applications, human – machine interaction and mobile development.","sameAs":["http://www.javacodegeeks.com/"],"url":"https://examples.javacodegeeks.com/author/nikos-maravitsas/"}]}
{"@context":"http:\/\/schema.org","@type":"BreadcrumbList","@id":"#Breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"item":{"name":"Home","@id":"https:\/\/examples.javacodegeeks.com\/"}},{"@type":"ListItem","position":2,"item":{"name":"Java Development","@id":"https:\/\/examples.javacodegeeks.com\/category\/java-development\/"}},{"@type":"ListItem","position":3,"item":{"name":"Core Java","@id":"https:\/\/examples.javacodegeeks.com\/category\/java-development\/core-java\/"}},{"@type":"ListItem","position":4,"item":{"name":"lang","@id":"https:\/\/examples.javacodegeeks.com\/category\/java-development\/core-java\/lang\/"}},{"@type":"ListItem","position":5,"item":{"name":"String","@id":"https:\/\/examples.javacodegeeks.com\/category\/java-development\/core-java\/lang\/string\/"}}]}
{"@context":"http:\/\/schema.org","@type":"Article","dateCreated":"2014-04-01T18:01:48+03:00","datePublished":"2014-04-01T18:01:48+03:00","dateModified":"2020-06-15T14:04:26+03:00","headline":"Java String Replace and ReplaceAll String Methods","name":"Java String Replace and ReplaceAll String Methods","keywords":[],"url":"https:\/\/examples.javacodegeeks.com\/java-string-replace-and-replaceall-string-methods\/","description":"In this example, we are going to see how to use the String Replace and ReplaceAll String class API Methods in Java. With replaceAll, you can replace all character sequences and single characters from","copyrightYear":"2014","articleSection":"String","articleBody":"\nIn this example, we are going to see how to use the String Replace and ReplaceAll String class API Methods in Java. With replaceAll, you can replace all character sequences and single characters from a String instance.\n\n\n\nAs you know, String objects are immutable. This means, that every time you try to change its value, a new String object is created, holding the new value. So replaceAll will create a new String object and return a reference pointing to it.\n\n\n\n\n\n\n\nLet's see some examples on Java String Replace and ReplaceAll String Methods.\n\n\n\n1. Replacing single characters\n\n\n\nHere is how you can replace all instances of a single character inside a String.\n\n\n\n\n\n\n\npublic class StringReplaceAllExample {\n \n public static void main(String[] args) {\n \n String str = "This is a String to use as an example to present raplaceAll";\n \n \/\/ replace all occurrences of 'a' with '@'\n String newStr = str.replaceAll("a", "@");\n System.out.println(newStr);\n \n \/\/ replace all occurrences of 'e' with '3'\n newStr = newStr.replaceAll("e", "3");\n System.out.println(newStr);\n \n \/\/ replace all occurrences of 't' with 'T'\n newStr = newStr.replaceAll("t", "T");\n System.out.println(newStr);\n \n \/\/ remove all occurrences of 'o'\n newStr = newStr.replaceAll("o", "");\n System.out.println(newStr);\n \n \/\/ replace all occurrences of 't' with 'That'\n newStr = newStr.replaceAll("T", "That");\n System.out.println(newStr);\n }\n}\n\n\n\nThis will output:\n\n\n\nThis is @ String to use @s @n ex@mple to present r@pl@ceAll\nThis is @ String to us3 @s @n 3x@mpl3 to pr3s3nt r@pl@c3All\nThis is @ STring To us3 @s @n 3x@mpl3 To pr3s3nT r@pl@c3All\nThis is @ STring T us3 @s @n 3x@mpl3 T pr3s3nT r@pl@c3All\nThathis is @ SThatring That us3 @s @n 3x@mpl3 That pr3s3nThat r@pl@c3All\n\n\n\nAs you can see from the above example, replacement is a case sensitive operation, For example, when we replaced all occurrences of 'a', we didn't replace 'A' in the original String. Additionally, you can use replaceAll to completely remove a character from the String by replacing it with \"\". It's also worth noting that if no occurrences of the targeted character are found, as a result replaceAll will return the exact same String, no new String gets created.\n\n\n\n The difference between replace and replaceAll is that the replace method either takes a pair of char or a pair CharSequence's . The replace method will replace all occurrences of a char or CharSequence's . Using the wrong function can lead to subtle bugs.\n\n\n\nHere we can see a replace method example.\n\n\n\npublic class Replace {\n\tpublic static void main(String[] args) {\n\t\t String Str = new String("The dog plays with the ball.");\n\n\t System.out.println(Str.replace('a', 'T'));\n\n\t}\n}\n\n\n\n\nThe output is:\n\n\n\nThe dog plTys with the bTll.\n\n\n\n2. Replacing char sequences character\n\n\n\nHere is how you can replace all char sequences inside a String with another char sequence or with a single character.\n\n\n\npublic class StringReplaceAllExample {\n \n public static void main(String[] args) {\n \n String str = "This is a String to use as an example to present raplaceAll";\n \n \/\/ replace all occurrences of 'This' with 'That'\n String newStr = str.replaceAll("This", "That");\n System.out.println(newStr);\n \n \/\/ replace all occurrences of 'String' with 'big String'\n newStr = str.replaceAll("String", "big String");\n System.out.println(newStr);\n \n \/\/ remove all occurrences of 'is'\n newStr = str.replaceAll("is", "");\n System.out.println(newStr);\n \n \/\/ remove all occurrences of 'replaceAll'\n newStr = str.replaceAll("raplaceAll", "");\n System.out.println(newStr);\n }\n}\n\n\n\nThis will output:\n\n\n\nThat is a String to use as an example to present raplaceAll\nThis is a big String to use as an example to present raplaceAll\nTh a String to use as an example to present raplaceAll\nThis is a String to use as an example to present \n\n\n\n\n\n\n\n3. Using regular expressions\n\n\n\nThis is a great feature of replaceAll. You can use a regular expression to replace all occurrences of matching char sequences inside a String.\n\n\n\n\n\n\n\npublic class StringReplaceAllExample {\n \n public static void main(String[] args) {\n \n String str = "This 1231 is 124 a String 1243 to 34563 use 5455";\n \n \/\/ remove all numbers\n String newStr = str.replaceAll("[0-9]+", "");\n System.out.println(newStr);\n \n \/\/ remove all words with 'Java'\n newStr = str.replaceAll("[a-zA-Z]+", "Java");\n System.out.println(newStr);\n }\n}\n\n\n\nThis will output:\n\n\n\nThis is a String to use \nJava 1231 Java 124 Java Java 1243 Java 34563 Java 5455\n\n\n\nAnother example is:\n\n\n\npublic class ReplaceAllex {\n\n\t\t public static void main(String[] args) {\n\t\t\t String s="That is a String to use as an example to present raplaceAll" ;\n\t\t\t System.out.println(s);\n\t\t\t String str = s.replaceAll(" {2,}", " ");\n\t\t\t System.out.println(str);\n\t\t }\n}\n\n\n\nThe output is:\n\n\n\nThat is a String to use as an example to present raplaceAll\nThat is a String to use as an example to present raplaceAll\n\n\n\n\nThe last example:\n\n\n\npublic class ReplaceAllDollar {\n\t public static void main(String[] args) {\n\t\t String s="The difference between dollars and euros is 0.09 dollars.";\n\t\t System.out.println(s);\n\t\tString str = s.replaceAll("dollars", "\\\\$");\n\t\tSystem.out.println(str);\n\t }\n}\n\n\n\n\nThe output is:\n\n\n\nThe difference between dollars and euros is 0.09 dollars.\nThe difference between $ and euros is 0.09 $.\n \n\n\n\n4. Download the Source Code\n\n\n\nDownload\nYou can download the full source code of this example here: Java String Replace and ReplaceAll String Methods\n\n\n\nLast updated on May 12th, 2020\n","publisher":{"@id":"#Publisher","@type":"Organization","name":"Examples Java Code Geeks","logo":{"@type":"ImageObject","url":"\/wp-content\/uploads\/2013\/05\/JavaCodeGeeks-logo.png"},"sameAs":["https:\/\/feeds.feedburner.com\/examplesjavacodegeeks","https:\/\/www.facebook.com\/javacodegeeks","https:\/\/twitter.com\/javacodegeeks","https:\/\/www.linkedin.com\/groups\/3810709\/","https:\/\/www.youtube.com\/channel\/UCxoUc7Rar2q90Gu0nT2ffuQ","https:\/\/github.com\/javacodegeeks\/"]},"sourceOrganization":{"@id":"#Publisher"},"copyrightHolder":{"@id":"#Publisher"},"mainEntityOfPage":{"@type":"WebPage","@id":"https:\/\/examples.javacodegeeks.com\/java-string-replace-and-replaceall-string-methods\/","breadcrumb":{"@id":"#Breadcrumb"}},"author":{"@type":"Person","name":"Nikos Maravitsas","url":"https:\/\/examples.javacodegeeks.com\/author\/nikos-maravitsas\/"},"image":{"@type":"ImageObject","url":"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2012\/12\/java-logo.jpg","width":1200,"height":150}}
| og:locale | en_US |
| og:type | article |
| og:site_name | Examples Java Code Geeks |
| article:publisher | https://www.facebook.com/javacodegeeks |
| article:published_time | 2014-04-01T15:01:48+00:00 |
| article:modified_time | 2020-06-15T11:04:26+00:00 |
| og:image | https://examples.javacodegeeks.com/wp-content/uploads/2012/12/java-logo.jpg |
| og:image:width | 150 |
| og:image:height | 150 |
| og:image:type | image/jpeg |
| author | Nikos Maravitsas |
| twitter:card | summary_large_image |
| twitter:creator | @javacodegeeks |
| twitter:label1 | Written by |
| twitter:data1 | Nikos Maravitsas |
| twitter:label2 | Est. reading time |
| twitter:data2 | 5 minutes |
| None | IE=edge |
| msapplication-TileImage | https://examples.javacodegeeks.com/wp-content/uploads/2013/05/JavaCodeGeeks-favIcon.png |
Links:
Viewport: width=device-width, initial-scale=1.0
Robots: index, follow, max-image-preview:large, max-snippet:-1, max-video-preview:-1