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

The String 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 named ReplaceExample.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.geeks
New string1 is: java_code_geeks
New string2 is: java.jcg.geeks
New string3 is: j*v*.cod*.g**ks
New string4 is: j*va.code.geeks

3. Download the source code

This was an example of string replace in Java. You can download the source code from here: StringReplaceExample.zip

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.
Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Back to top button