String
Java String Replace Example
In this example we will show how to modify a string by replacing either characters or sequences of characters included in the string with different ones.
1. String Replace methods in Java
TheString provides 4 methods for replacing strings.
String replace(char oldChar, char newChar)
This method replaces every occurrence of the oldChar in the string with the newChar and returns the updated string.String replace(CharSequence target, CharSequence replacement)
This method replaces every occurrence of the target sequence of characters in the string with the replacement sequence of characters.String replaceAll(String regularExpr, String replacement)
This method replaces every substring in the given string that matches the regular expression, with the replacemet substring.String replaceFirst(String regularExpr, String replacement)
This method replaces the first occurrence of the substring that matches the regular expression, with the replacement string.
2. String replace example
Create a java class namedReplaceExample.java with the following code:
ReplaceExample.java
package com.javacodegeeks.javabasics.replace;public class ReplaceExample {public static void main(String[] args) {String oldstr = "java.code.geeks";System.out.println("Original string is: " + oldstr);// this method replaces every occurrence of the character . with the// character _String newstr1 = oldstr.replace('.', '_');System.out.println("New string1 is: " + newstr1);// this method replaces every occurrence of the character sequence// "code" with the// character sequence "jcg"String newstr2 = oldstr.replace("code", "jcg");System.out.println("New string2 is: " + newstr2);String regex = "[a,e]";// this method replaces every occurrence of the characters in the// regular expression// with the character *String newstr3 = oldstr.replaceAll(regex, "*");System.out.println("New string3 is: " + newstr3);// this method replaces the first occurrence of any of the characters in// the regular expression// with the character *String newstr4 = oldstr.replaceFirst(regex, "*");System.out.println("New string4 is: " + newstr4);}}If we run the above code, we will have the following results:
- Output:
Original string is: java.code.geeksNew string1 is: java_code_geeksNew string2 is: java.jcg.geeksNew string3 is: j*v*.cod*.g**ksNew string4 is: j*va.code.geeks3. Download the source code
This was an example of string replace in Java. You can download the source code from here:StringReplaceExample.zip
Do you want to know how to develop your skillset to become aJava Rockstar?
Subscribe to our newsletter to start Rockingright now!
To get you started we give you our best selling eBooks forFREE!
1. JPA Mini Book
2. JVM Troubleshooting Guide
3. JUnit Tutorial for Unit Testing
4. Java Annotations Tutorial
5. Java Interview Questions
6. Spring Interview Questions
7. Android UI Design
and many more ....
I agree to theTerms andPrivacy Policy

Thank you!
We will contact you soon.
Konstantina Dimtsa
Konstantina has graduated from the Department of Informatics and Telecommunications in National and Kapodistrian University of Athens (NKUA) and she is currently pursuing M.Sc studies in Advanced Information Systems at the same department. She is also working as a research associate for NKUA in the field of telecommunications. Her main interests lie in software engineering, web applications, databases and telecommunications.



