All TalkersCode Topics

Follow TalkersCode On Social Media

devloprr.com - A Social Media Network for developers Join Now ➔

String Replace In Java

Last Updated : Mar 11, 2024

String Replace In Java

In this article we will show you the solution of string replace in java, a string's characters or substrings can be changed using Java's powerful replace() method, which is provided by the language's String class.

However, we can make use of other string manipulation methods offered by Java if we wish to investigate an alternative strategy without using the replace() method.

Using the StringBuilder class is one option. To replace characters, we can turn the original string into a StringBuilder object and utilize its replace() method.

Since we can provide the start and final indices to just replace certain sections of the string, this approach offers more flexibility.

The Pattern & Matcher classes are obtained from the java.util.regex package can also be used, as can regular expressions.

We can define patterns and carry out intricate string matching & substitution operations using regular expressions.

We'll talk about the Java concept of string replace.

Step By Step Guide On String Replace In Java :-

import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class StringReplacement {
    public static void main(String[] args) {
        String originalString = "Hello talkerscode! talkerstech is great. talkersmoney can bring financial success.";
        // Creating patterns for replacement
        Pattern pattern1 = Pattern.compile("talkerscode");
        Pattern pattern2 = Pattern.compile("talkerstech");
        Pattern pattern3 = Pattern.compile("talkersmoney");
        // Replacing words using Matcher
        Matcher matcher1 = pattern1.matcher(originalString);
        String replacedString1 = matcher1.replaceAll("REPLACED1");
        Matcher matcher2 = pattern2.matcher(replacedString1);
        String replacedString2 = matcher2.replaceAll("REPLACED2");
        Matcher matcher3 = pattern3.matcher(replacedString2);
        String replacedString3 = matcher3.replaceAll("REPLACED3");
        System.out.println("Original string: " + originalString);
        System.out.println("Replaced string 1: " + replacedString1);
        System.out.println("Replaced string 2: " + replacedString2);
        System.out.println("Replaced string 3: " + replacedString3);
    }
}
  1. You can see that we wrote Java code to demonstrate string substitution in this location.
  2. We start by importing the Matcher and Pattern classes from the java.util.regex package.
  3. These classes are what we utilize to work with regular expressions.
  4. The primary method, from which the program runs, is defined in the StringReplacement class.
  5. We define a unique string inside the main method: "Hello talkerscode! Great talkerstech. Talkersmoney can help you succeed financially.
  6. To specify the patterns we wish to replace in the original string, we build three Pattern objects. The words "talkerscode," "talkerstech," and "talkersmoney" are all patterns that match each other.
  7. Each Pattern object's matcher method is invoked, and the original string is sent as a parameter. This results in Matcher objects.
  8. To carry out the actual replacement, we'll make use of these Matcher objects.
  9. In order to replace every instance of the relevant pattern with the provided replacement string, we call the replaceAll function on each Matcher object.
  10. The replaced strings are kept in separate variables: replacedString1 holds the original string with "talkerscode" replaced by "REPLACED1," replacedString2 holds the original string with "talkerstech" replaced by "REPLACED2," and replacedString3 holds the original string with "talkersmoney" replaced by "REPLACED3."
  11. Finally, we use the System.out.println method to print the original string together with the three modified strings.

Conclusion :-

As a result, we were able to understand the Java concept of string replace.

We also discovered how to replace every instance of a pattern in a string using the replaceAll function of the Matcher class.

We may carry out intricate matching and replacement operations within strings by using the Pattern & Matcher classes from the java.util.regex package.

I hope this article on string replace in java helps you and the steps and method mentioned above are easy to follow and implement.

Author Image About Amruta

Amruta is an Experienced web developer with 4 years for experience she completed her master's with MCA and passionate about programming Languages for creating technical contents like HTML, CSS, JavaScript, Java, Python, PHP, jQuery.

Follow Amruta On Linkedin 🡪