DEV Community

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

Posted on

3

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

Heroku

Deliver your unique apps, your own way.

Heroku tackles the toil — patching and upgrading, 24/7 ops and security, build systems, failovers, and more. Stay focused on building great data-driven applications.

Learn More

Top comments (0)

Neon image

Next.js applications: Set up a Neon project in seconds

If you're starting a new project, Neon has got your databases covered. No credit cards. No trials. No getting in your way.

Get started →

👋 Kindness is contagious

Explore a trove of insights in this engaging article, celebrated within our welcoming DEV Community. Developers from every background are invited to join and enhance our shared wisdom.

A genuine "thank you" can truly uplift someone’s day. Feel free to express your gratitude in the comments below!

On DEV, our collective exchange of knowledge lightens the road ahead and strengthens our community bonds. Found something valuable here? A small thank you to the author can make a big difference.

Okay