Java String replace() Method

The String.replace() method in Java is used to replace occurrences of a specified character or substring with another character or substring. This guide will cover the method's usage, explain how it works, and provide examples to demonstrate its functionality.

Table of Contents

  1. Introduction
  2. replace Method Syntax
  3. Examples
    • Replacing Characters
    • Replacing Substrings
    • Replacing with Empty Strings
    • Case Sensitivity
  4. Conclusion

Introduction

The String.replace() method is a member of the String class in Java. It allows you to create a new string by replacing all occurrences of a specified character or substring with another character or substring. This method is particularly useful for text processing, formatting, and data cleaning.

replace Method Syntax

The replace method has two common variations:

  1. Replacing all occurrences of a character:
public String replace(char oldChar, char newChar)
  1. Replacing all occurrences of a substring:
public String replace(CharSequence target, CharSequence replacement)

Examples

Replacing Characters

The replace method can be used to replace all occurrences of a specified character with another character.

Example

public class ReplaceExample {
    public static void main(String[] args) {
        String message = "Hello, World!";

        String newMessage = message.replace('o', '0');

        System.out.println("Original message: " + message);
        System.out.println("New message: " + newMessage);
    }
}

Output:

Original message: Hello, World!
New message: Hell0, W0rld!

Replacing Substrings

The replace method can be used to replace all occurrences of a specified substring with another substring.

Example

public class ReplaceExample {
    public static void main(String[] args) {
        String message = "Hello, World! Hello, Java!";

        String newMessage = message.replace("Hello", "Hi");

        System.out.println("Original message: " + message);
        System.out.println("New message: " + newMessage);
    }
}

Output:

Original message: Hello, World! Hello, Java!
New message: Hi, World! Hi, Java!

Replacing with Empty Strings

The replace method can also be used to remove characters or substrings by replacing them with an empty string.

Example

public class ReplaceExample {
    public static void main(String[] args) {
        String message = "Hello, World!";

        String newMessage = message.replace("World", "");

        System.out.println("Original message: " + message);
        System.out.println("New message: " + newMessage);
    }
}

Output:

Original message: Hello, World!
New message: Hello, !

Case Sensitivity

The replace method is case-sensitive, meaning it will distinguish between uppercase and lowercase characters and substrings.

Example

public class ReplaceExample {
    public static void main(String[] args) {
        String message = "Hello, World!";

        String newMessage = message.replace("world", "Java");

        System.out.println("Original message: " + message);
        System.out.println("New message: " + newMessage);
    }
}

Output:

Original message: Hello, World!
New message: Hello, World!

Conclusion

The String.replace() method in Java is a powerful and versatile tool for replacing characters and substrings within a string. By understanding how to use this method, you can efficiently process, format, and clean text in your Java applications. Whether you are replacing characters, substrings, handling case sensitivity, or removing parts of a string, the replace method provides a reliable solution for these tasks.

Comments