Java String replace() example

📘 Premium Read: Access my best content on Medium member-only articles — deep dives into Java, Spring Boot, Microservices, backend architecture, interview preparation, career advice, and industry-standard best practices.

✅ Some premium posts are free to read — no account needed. Follow me on Medium to stay updated and support my writing.

🎓 Top 10 Udemy Courses (Huge Discount): Explore My Udemy Courses — Learn through real-time, project-based development.

▶️ Subscribe to My YouTube Channel (172K+ subscribers): Java Guides on YouTube

In this guide, you will learn about the String replace() method in Java programming and how to use it with an example.

1. String replace() Method Overview

Definition:

The replace() method of Java's String class is utilized to replace all the occurrences of a specified character or substring with another character or substring within the invoking string.

Syntax:

1. str.replace(char oldChar, char newChar)
2. str.replace(CharSequence target, CharSequence replacement)

Parameters:

- oldChar: the old character.

- newChar: the new character.

- target: the sequence of char values to be replaced.

- replacement: the replacement sequence of char values.

Key Points:

- Returns a new string resulting from replacing all occurrences of oldChar in this string with newChar.

- The replace() method is case-sensitive.

- If the old character or substring is not found, the original string is returned.

- The method does not alter the original string but returns a new one.

2. String replace() Method Example

public class ReplaceExample {
    public static void main(String[] args) {
        String sample = "Java Programming Java";

        // Replacing a character
        String replaced1 = sample.replace('a', 'A');
        System.out.println("Replace 'a' with 'A': " + replaced1);

        // Replacing a substring
        String replaced2 = sample.replace("Java", "JAVA");
        System.out.println("Replace 'Java' with 'JAVA': " + replaced2);

        // Attempting to replace a non-existent character
        String replaced3 = sample.replace('z', 'Z');
        System.out.println("Replace 'z' with 'Z': " + replaced3);
    }
}

Output:

Replace 'a' with 'A': JAvA ProgrAmming JAvA
Replace 'Java' with 'JAVA': JAVA Programming JAVA
Replace 'z' with 'Z': Java Programming Java

Explanation:

In the example:

1. We begin by replacing all occurrences of the character 'a' with 'A'. The replace() method alters every 'a' found in the string.

2. We then replace the substring "Java" with "JAVA". The method replaces each occurrence of the specified substring in the provided string.

3. Lastly, we attempt to replace a character that doesn't exist in the string ('z'). The method returns the original string unaltered.

Related Java String Class method examples

Comments

Spring Boot 3 Paid Course Published for Free
on my Java Guides YouTube Channel

Subscribe to my YouTube Channel (165K+ subscribers):
Java Guides Channel

Top 10 My Udemy Courses with Huge Discount:
Udemy Courses - Ramesh Fadatare