Java replace() Method
Tags: java, beginners, computerscience, javascript
Published: 2021-08-29 12:23:01

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 :-**
```Java
stringName.replace(oldString, newString);
``` 
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'.

```Java
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);
    }
}
``` 
***You can run your code online [here](https://www.tutorialspoint.com/compile_java_online.php)***

![java-replace().png](https://cdn.hashnode.com/res/hashnode/image/upload/v1630235665359/loz67vhX2.png)

### 2. replace(String oldString, String newString)
In this example we have replaced all the occurrence of String "lo" with String "ping".

```Java
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);
    }
}
``` 
***You can run your code online [here](https://www.tutorialspoint.com/compile_java_online.php)***

![java-replace()-1.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1630236294000/wrYerxtCf.png)

### 3. replace(String emptyString, String newString)
In this example we have replaced all the occurrence of String "" (empty String) with String "A".

```Java
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);
    }
}
``` 
***You can run your code online [here](https://www.tutorialspoint.com/compile_java_online.php)***

![java-replace()-2.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1630236930284/Y5--6qN0m.png)

### 4. replace(String whitespace, String newString)

```Java
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);
    }
}
``` 
***You can run your code online [here](https://www.tutorialspoint.com/compile_java_online.php)***

![java-replace()-3.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1630237244680/PwONG1vRq.png)

### 5. replaceFirst(String oldString, String newString)

```Java
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);
    }
}
``` 
***You can run your code online [here](https://www.tutorialspoint.com/compile_java_online.php)***

![java-replaceFirst().png](https://cdn.hashnode.com/res/hashnode/image/upload/v1630238073181/DNPxpjQzI.png)

### 6. replaceAll(String regex, String newString)

```Java
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);
    }
}
``` 
***You can run your code online [here](https://www.tutorialspoint.com/compile_java_online.php)***

![java-replaceAll().png](https://cdn.hashnode.com/res/hashnode/image/upload/v1630239161535/V5YNK4HDf.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](https://twitter.com/rajatgour20)***
#### *Connect with me on [Twitter](https://twitter.com/ritvikdubey27?s=09) or [LinkedIn](https://www.linkedin.com/in/ritvik-dubey-3bbb22169)*
#### *My personal blog [blog.ritvikdubey.com](https://blog.ritvikdubey.com/)*