Java String replace() method example

The Java String replace() method replaces each substring of this string that matches the literal target substring. The substring matching process start from beginning of the string (index 0).

1. String replace() method

String replace() method is overloaded method in Java. It has two variant.

  1. public String replace(char oldChar, char newChar) – returns a string resulting from replacing all occurrences of oldChar in this string with newChar.
  2. public String replace(CharSequence target, CharSequence replacement) – returns a string resulting from replacing all occurrences of target substring in this string with replacement substring.

2. Java String replace(char oldChar, char newChar) example

Java program to replace all occurrences of a given character in string with a new character. In given example, I am replacing all occurrences of letter ‘o’ (lower case) with letter ‘O’ (upper case).

public class StringExample 
{
    public static void main(String[] args) 
    {
        String originalString = "Hello world !!";
        
        String newString = originalString.replace('o', 'O');	//HellO wOrld !!
        
        System.out.println(originalString);
        System.out.println(newString);
    }
}

Program output.

Hello world !!
HellO wOrld !!

2. Java String replace(CharSequence target, CharSequence replacement) example

Java program to replace all occurrences of a given substring 'target' in string with a new substring code>’replacement’.

In given example, I am replacing all occurrences of substring “java” with upper case “JAVA” string.

public class StringExample 
{
    public static void main(String[] args) 
    {
        String originalString = "how to do in java - java tutotials";
        
        String newString = originalString.replace("java", "JAVA");
        
        System.out.println(originalString);
        System.out.println(newString);
    }
}

Program output.

how to do in java - java tutotials
how to do in JAVA - JAVA tutotials

Note that regular expressions are not allowed as method arguments. If you want to use regex, then use String replaceAll() method.

3. ‘null’ is not allowed

A 'null' is not allowed as both method arguments. It will throw NullPointerException.

public class StringExample 
{
    public static void main(String[] args) 
    {
        String newString = "hello world".replace("world", null);

        //or

        //String newString = "hello world".replace(null, "world");
    }
}

Program output.

Exception in thread "main" java.lang.NullPointerException
	at java.lang.String.replace(String.java:2227)
	at com.StringExample.main(StringExample.java:7)

Happy Learning !!

References:

Java String methods and examples
Java String Doc

Was this post helpful?

Join 7000+ Fellow Programmers

Subscribe to get new post notifications, industry updates, best practices, and much more. Directly into your inbox, for free.

Leave a Comment

HowToDoInJava

A blog about Java and its related technologies, the best practices, algorithms, interview questions, scripting languages, and Python.