DEV Community

Cover image for Java replace() Method
Ritvik Dubey
Ritvik Dubey

Posted on

Java replace() Method

Hello all👋 I hope you are doing well. This is going to be a very short and very useful article. In this article I'll be writing about replace() method in Java. This is a one of the most useful methods.

Let's begin...

replace()

The replace() method will replace a character or substring with another character or string. This is a method of Java String class. It returns a string derived from the original string by replacing every occurrence of old-string or old-character with new-string or new-character. When working with a string in Java, you may encounter a situation where you want to replace a specific character or substring in that string with another character or another substring. In such situations replace() method comes in.

Syntax :-

stringName.replace(oldString, newString);
Enter fullscreen mode Exit fullscreen mode

There are many variations in which replace() can be used.

1. replace(char oldChar, char newChar)

In this example we have replaced all the occurrences of char ‘l’ with char ‘c'.

public class Demo {
    public static void main(String[] args) {
        String strOld = "Hello world";
        System.out.println("Original string : " + strOld);
        String strNew = strOld.replace('l','c');
        System.out.println("Replaced string : " + strNew);
    }
}
Enter fullscreen mode Exit fullscreen mode

You can run your code online here

java-replace().png

2. replace(String oldString, String newString)

In this example we have replaced all the occurrence of String "lo" with String "ping".

public class Demo {
    public static void main(String[] args) {
        String strOld = "Hello world";
        System.out.println("Original string : " + strOld);
        String strNew = strOld.replace("lo","ping");
        System.out.println("Replaced string : " + strNew);
    }
}
Enter fullscreen mode Exit fullscreen mode

You can run your code online here

java-replace()-1.png

3. replace(String emptyString, String newString)

In this example we have replaced all the occurrence of String "" (empty String) with String "A".

public class Demo {
    public static void main(String[] args) {
        String strOld = "Hello world";
        System.out.println("Original string : " + strOld);
        String strNew = strOld.replace("", "A");
        System.out.println("Replaced string : " + strNew);
    }
}
Enter fullscreen mode Exit fullscreen mode

You can run your code online here

java-replace()-2.png

4. replace(String whitespace, String newString)

public class Demo {
    public static void main(String[] args) {
        String strOld = "Hello world";
        System.out.println("Original string : " + strOld);
        String strNew = strOld.replace(" ", "WHITESPACE");
        System.out.println("Replaced string : " + strNew);
    }
}
Enter fullscreen mode Exit fullscreen mode

You can run your code online here

java-replace()-3.png

5. replaceFirst(String oldString, String newString)

public class Demo {
    public static void main(String[] args) {
        String strOld = "Hello world";
        System.out.println("Original string : " + strOld);
        String strNew = strOld.replaceFirst("l", "c");
        System.out.println("Replaced string : " + strNew);
    }
}
Enter fullscreen mode Exit fullscreen mode

You can run your code online here

java-replaceFirst().png

6. replaceAll(String regex, String newString)

public class Demo {
    public static void main(String[] args) {
        String strOld = "Hello world";
        System.out.println("Original string : " + strOld);
        String strNew = strOld.replaceFirst("\\s", "");
        System.out.println("Replaced string : " + strNew);
    }
}
Enter fullscreen mode Exit fullscreen mode

You can run your code online here

java-replaceAll().png

Okay so that's enough for now.

Thank you for reading.

Please share your thoughts about it and correct me if I'm wrong.

I hope you liked it and found it helpful.

Cover:- Rajat Gour

Connect with me on Twitter or LinkedIn

My personal blog blog.ritvikdubey.com

Top comments (0)