All TalkersCode Topics

Follow TalkersCode On Social Media

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

Java Replace Characters In String

Last Updated : Mar 11, 2024

Java Replace Characters In String

In this article we will show you the solution of java replace characters in string, Java offers a variety of techniques that are flexible and simple to use for replacing characters in a string.

One such technique is the replace() function, which substitutes a different character or string for every instance of a specified character or string of characters.

The replace() method modifies the original string and outputs a replacement-filled new string.

Java also has the replaceAll() method, which enables character substitution using regular expressions in addition to the replace() method.

Developers may do intricate pattern matching & substitution within strings thanks to this powerful capability. We'll talk about the concept of replacing characters in a string in Java.

Step By Step Guide On Java Replace Characters In String :-

import java.lang.String;
public class StringReplacement {
    public static void main(String[] args) {
        String originalString = "Hello talkerscode!";
        // Replacing 'o' with 'talkerscode'
        String replacedString1 = originalString.replace('o', ' ');
        replacedString1 = replacedString1 + "talkerscode";
        // Replacing 'c' with 'talkerstech'
        String replacedString2 = originalString.replace('c', ' ');
        replacedString2 = replacedString2 + "talkerstech";
        // Replacing 'e' with 'talkersmoney'
        String replacedString3 = originalString.replace('e', ' ');
        replacedString3 = replacedString3 + "talkersmoney";
        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. As you can see, we have written some Java code that replaces the characters in a string.
  2. The String class is imported from the java.lang package. It is not essential to use this import statement because String is a component of the java.lang package.
  3. The main class of the program is StringReplacement, which we define.
  4. We define the program's main method as its entry point. As a parameter, it accepts a string array (args).
  5. We declare a string variable called originalString in the main method, and we give it the value Hello talkerscode!.
  6. The originalString's characters are swapped out for new ones using the replace technique. We carry out the following three replacements:
  7. The character o is swapped out for the space character in the first substitution. In the replacedString1 variable, the outcome is kept.
  8. The character "c" is changed to the space character "'" in the second substitution. In the replacedString2 variable, the outcome is kept.
  9. The character "'" is used in place of the letter "e" in the third substitution. The replacedString3 variable holds the outcome.
  10. We use the + operator to join the matching replacement word and the respective changed string together after each replacement.
  11. The System.out.println commands are then used to print the original text along with the three strings that were replaced. The strings and the messages are concatenated using the + operator.

Conclusion :-

As a result, we were able to understand how to replace characters in a string using Java.

We also discovered that one such technique is the replace() method, which is illustrated in the above code and replaces every instance of a specified character with another character or string.

It makes changes to the original string and then outputs a replacement string.

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