Searching and replacing text

Java:
Searching and replacing text

How to:

Searching and replacing in Java is a breeze thanks to the String class and its replace() method. Here’s how you do it:

public class ReplaceDemo {
    public static void main(String[] args) {
        String originalText = "The quick brown fox jumps over the lazy dog";
        String modifiedText = originalText.replace("lazy", "energetic");
        
        System.out.println("Before: " + originalText);
        System.out.println("After: " + modifiedText);
    }
}

Output:

Before: The quick brown fox jumps over the lazy dog
After: The quick brown fox jumps over the energetic dog

Now, for patterns or wilder replacements, Pattern and Matcher come into play:

import java.util.regex.Pattern;
import java.util.regex.Matcher;

public class RegexReplaceDemo {
    public static void main(String[] args) {
        String originalText = "There are 31,536,000 seconds in 365 days.";
        Pattern pattern = Pattern.compile("\\d+");
        Matcher matcher = pattern.matcher(originalText);
        String modifiedText = matcher.replaceAll("#");
        
        System.out.println("Before: " + originalText);
        System.out.println("After: " + modifiedText);        
    }
}

Output:

Before: There are 31,536,000 seconds in 365 days.
After: There are # seconds in # days.

Deep Dive:

The replace() method traces its origins to the earliest days of Java. It’s part of the immutable String class, which means every time you use it, you’re creating a new string. Very eco-friendly, no waste of the old stuff.

But what’s the deal with Pattern and Matcher, you ask? These classes are part of Java’s regular expression (regex) API, introduced in Java 1.4. They add teeth to search and replace, allowing you to detect complex patterns and modify text dynamically. It’s like using a scalpel instead of a sledgehammer.

Plus, there’s replaceAll() and replaceFirst(), two methods of the Matcher class that fine-tune your text transformations, replacing all occurrences or just the first match.

Another alternative is using the StringBuffer or StringBuilder classes when you’re dealing with tons of modifications because unlike String, these buffers are mutable.

See Also:

For more hands-on practice, check out RegexOne (https://regexone.com), it’s a great resource to level up your regex skills.