Substring Replacement in Java

JavaJavaBeginner
Practice Now

Introduction

In this lab, we will learn how to replace substrings in a String using Java programming language. We will be using different methods available in Java to replace single/multiple characters or substrings. In the end, we will have a clear understanding of how to implement these methods in Java code.

Create a Java file

Let's create a Java file to write and execute the code. Open the terminal and navigate to the project directory. Create a new file named SubstringReplace.java using the following command:

touch SubstringReplace.java

Open the file in your preferred text editor.

Replacing single characters

We can use the replace() method to replace a single occurrence of a character in the string. Here's an example:

public class SubstringReplace {
    public static void main(String[] args) {
        String originalString = "hello world!";
        System.out.println("Original String: " + originalString);
        // Replacing a single character
        String newString = originalString.replace('l', 'L');
        System.out.println("New String: " + newString);
    }
}

In the above example, we are replacing the lowercase l character with an uppercase L character in the original string.

To compile and run the code, use the following commands:

javac SubstringReplace.java
java SubstringReplace

This should produce the following output:

Original String: hello world!
New String: heLLo worLd!

Replacing substrings

We can use the replace() method to replace all occurrences of a substring with a new substring. Here's an example:

public class SubstringReplace {
    public static void main(String[] args) {
        String originalString = "the quick brown fox jumps over the lazy dog";
        System.out.println("Original String: " + originalString);
        // Replacing a substring
        String newString = originalString.replace("the", "a");
        System.out.println("New String: " + newString);
    }
}

In the above example, we are replacing all occurrences of the substring "the" with the substring "a".

To compile and run the code, use the same commands as in step 2.

This should produce the following output:

Original String: the quick brown fox jumps over the lazy dog
New String: a quick brown fox jumps over a lazy dog

Replacing the first occurrence of a substring

We can use the replaceFirst() method to replace the first occurrence of a substring with a new substring. Here's an example:

public class SubstringReplace {
    public static void main(String[] args) {
        String originalString = "the quick brown fox jumps over the lazy dog";
        System.out.println("Original String: " + originalString);
        // Replacing the first occurrence of a substring
        String newString = originalString.replaceFirst("the", "a");
        System.out.println("New String: " + newString);
    }
}

In the above example, we are replacing the first occurrence of the substring "the" with the substring "a".

To compile and run the code, use the same commands as in step 2.

This should produce the following output:

Original String: the quick brown fox jumps over the lazy dog
New String: a quick brown fox jumps over the lazy dog

Replacing characters with regular expressions

We can use regular expressions to replace a character with a pattern. Here's an example:

public class SubstringReplace {
    public static void main(String[] args) {
        String originalString = "the quick brown fox jumps over the lazy dog";
        System.out.println("Original String: " + originalString);
        // Replacing characters with regular expressions
        String newString = originalString.replaceAll("[aeiou]", "");
        System.out.println("New String: " + newString);
    }
}

In the above example, we are removing all vowels from the original string using a regular expression.

To compile and run the code, use the same commands as in step 2.

This should produce the following output:

Original String: the quick brown fox jumps over the lazy dog
New String: th qck brwn fx jmps vr th lzy dg

Replacing multiple characters with regular expressions

We can use regular expressions to replace multiple characters with a pattern. Here's an example:

public class SubstringReplace {
    public static void main(String[] args) {
        String originalString = "the quick brown fox jumps over the lazy dog";
        System.out.println("Original String: " + originalString);
        // Replacing multiple characters with regular expressions
        String newString = originalString.replaceAll("[aeiouAEIOU]", "");
        System.out.println("New String: " + newString);
    }
}

In the above example, we are removing all vowels (lowercase and uppercase) from the original string using a regular expression.

To compile and run the code, use the same commands as in step 2.

This should produce the following output:

Original String: the quick brown fox jumps over the lazy dog
New String: th qck brwn fx jmps vr th lzy dg

Replacing substrings with regular expressions

We can use regular expressions to replace a substring with a pattern. Here's an example:

public class SubstringReplace {
    public static void main(String[] args) {
        String originalString = "the quick brown fox jumps over the lazy dog";
        System.out.println("Original String: " + originalString);
        // Replacing substrings with regular expressions
        String newString = originalString.replaceAll("\\w+", "a");
        System.out.println("New String: " + newString);
    }
}

In the above example, we are replacing all words in the original string with the letter a using a regular expression.

To compile and run the code, use the same commands as in step 2.

This should produce the following output:

Original String: the quick brown fox jumps over the lazy dog
New String: a a a a a a a a a

Replacing multiple occurrences of a substring with regular expressions

We can use regular expressions to replace all occurrences of a substring with a pattern. Here's an example:

public class SubstringReplace {
    public static void main(String[] args) {
        String originalString = "the quick brown fox jumps over the lazy dog";
        System.out.println("Original String: " + originalString);
        // Replacing all occurrences of a substring with regular expressions
        String newString = originalString.replaceAll("the", "a");
        System.out.println("New String: " + newString);
    }
}

In the above example, we are replacing all occurrences of the substring "the" with the letter a using a regular expression.

To compile and run the code, use the same commands as in step 2.

This should produce the following output:

Original String: the quick brown fox jumps over the lazy dog
New String: a quick brown fox jumps over a lazy dog

Using StringBuilder to replace substrings

We can use StringBuilder class to replace substrings in a more efficient way. Here's an example:

public class SubstringReplace {
    public static void main(String[] args) {
        String originalString = "the quick brown fox jumps over the lazy dog";
        System.out.println("Original String: " + originalString);
        // Using StringBuilder to replace substrings
        StringBuilder stringBuilder = new StringBuilder(originalString);
        stringBuilder.replace(0, 3, "a");
        String newString = stringBuilder.toString();
        System.out.println("New String: " + newString);
    }
}

In the above example, we are using StringBuilder class to replace the first 3 characters in the string with the letter a.

To compile and run the code, use the same commands as in step 2.

This should produce the following output:

Original String: the quick brown fox jumps over the lazy dog
New String: a quick brown fox jumps over the lazy dog

Using StringBuffer to replace substrings

Similar to StringBuilder, we can use StringBuffer class to replace substrings in a more efficient way. The only difference is that StringBuffer is thread-safe. Here's an example:

public class SubstringReplace {
    public static void main(String[] args) {
        String originalString = "the quick brown fox jumps over the lazy dog";
        System.out.println("Original String: " + originalString);
        // Using StringBuffer to replace substrings
        StringBuffer stringBuffer = new StringBuffer(originalString);
        stringBuffer.replace(0, 3, "a");
        String newString = stringBuffer.toString();
        System.out.println("New String: " + newString);
    }
}

In the above example, we are using StringBuffer class to replace the first 3 characters in the string with the letter a.

To compile and run the code, use the same commands as in step 2.

This should produce the following output:

Original String: the quick brown fox jumps over the lazy dog
New String: a quick brown fox jumps over the lazy dog

Summary

In this lab, we learned how to replace substrings in a String using Java programming language. We discussed several methods available in Java to replace single/multiple characters or substrings. We also learned how to use regular expressions and StringBuilder/StringBuffer classes for more efficient substring replacements. By using this knowledge, you can easily manipulate strings in your Java programs.

Other Java Tutorials you may like