Industrial manufacturing
Industrial Internet of Things | Industrial materials | Equipment Maintenance and Repair | Industrial programming |
home  MfgRobots >> Industrial manufacturing >  >> Industrial programming >> Java

Mastering Java String Replacement: replace(), replaceAll(), and replaceFirst() Explained

The Java String class offers three powerful replacement methods that let you modify text efficiently: replace(), replaceAll(), and replaceFirst(). Each serves a distinct purpose and is optimized for different use‑cases.

  1. replace()
  2. replaceAll()
  3. replaceFirst()

Java String replace()

The replace() method performs a straightforward character or string substitution. It scans the entire string and replaces every occurrence of the target with the replacement, returning a new String instance. This method is ideal when you need a direct, non‑regex substitution.

Syntax

public String replace(CharSequence target, CharSequence replacement)

Parameters

Example

public class ReplaceExample {
    public static void main(String[] args) {
        String original = "the quick fox jumped";
        System.out.println("Original: " + original);
        System.out.println("After replace(\"fox\", \"dog\"): " + original.replace("fox", "dog"));
        System.out.println("After replace('t', 'a'): " + original.replace('t', 'a'));
    }
}

Output

Original: the quick fox jumped
After replace("fox", "dog"): the quick dog jumped
After replace('t', 'a'): ahe quick fox jumped

Java String replaceAll()

The replaceAll() method uses a regular expression (regex) to identify matches across the entire string. It then substitutes each match with the specified replacement, returning a new string. This method is perfect when you need pattern‑based replacements.

Signature

public String replaceAll(String regex, String replacement)

Parameters

Example

public class ReplaceAllExample {
    public static void main(String[] args) {
        String text = "Guru99 is a site providing free tutorials";
        String withoutSpaces = text.replaceAll("\\s", "");
        System.out.println(withoutSpaces);
    }
}

Output

Guru99isasiteprovidingfreetutorials

Java String replaceFirst()

The replaceFirst() method behaves like replaceAll() but only affects the first match found. It scans from the start of the string and stops after the first replacement, making it useful when only a single occurrence should be altered.

Signature

public String replaceFirst(String regex, String replacement)

Parameters

Example

public class ReplaceFirstExample {
    public static void main(String[] args) {
        String sentence = "This website providing free tutorials";
        String updated = sentence.replaceFirst("s", "9");
        System.out.println(updated);
    }
}

Output

Thi9 website providing free tutorials

Choosing the Right Method

These methods are immutable; they never alter the original string but return a new instance, ensuring thread safety and predictable behavior in concurrent environments.

Java

  1. Java Methods: How to Define, Call, and Use Them Effectively
  2. Mastering Java Strings: Creation, Methods, and Best Practices
  3. Mastering Java Polymorphism: Concepts, Examples, and Best Practices
  4. Master Java String Manipulation: Essential Functions, Methods, and Practical Examples
  5. Java String length() Method: How to Get a String’s Size (Example)
  6. Mastering Java String.indexOf(): Locating Substrings & Practical Examples
  7. Understanding Java String.charAt(): Syntax, Return Type, Exceptions, and a Practical Example
  8. Mastering Java’s String.endsWith(): How to Check String Suffixes with Examples
  9. Java String toLowerCase() & toUpperCase(): Convert Text Case with Locale Awareness
  10. Mastering Java's split() Method: A Practical Guide with Code Examples