String Replaceall Java

String Replaceall Java | In this section, we code to replace a string with a given substring to do this we can use the replaceAll() method available in Java. Let us understand it through some examples.

Example-1:-
string= “Java the programming the”
replaceAll(“the”, “XYZ”)
string after replacement:- “Java XYZ programming XYZ”
In the original string, every “the” is replaced with the string “XYZ”.

Example-2:-
string = “Know………Program”
replaceAll(“………”, ” “)
string after replacement:- “Know Program”
In the original string, the “………” is replaced with a space (” “).

The repaceAll() method replaces all the occurrences of the substring with the given substring.

The signature of the replaceAll() method is as follows:- public String replaceAll(String regex, String replacement)

In the replaceAll() method,
regex:- is the regular expression that needs to be replaced.
Replacement:- the substring that replaces the given string.
Returns:- The replaced string.
Throws:- PaternSyntaxException, if the syntax is not valid

The internal implementation of the code goes as follows:-

public String replaceAll(String regex, String replacement){
   return Patter.compile(regex)
                .matcher(this)
                .replaceAll(replacement);
}

Internally replaceAll() method of String calls replaceAll() method of the Matcher class defined in java.util.regex package.

Java Replaceall String Demonstration

Let us see a simple Java program to demonstrate how replaceall() method works. In the below program we took a string and we are replacing the “Language” substring with the “lang” substring.

Example-1:- String Replaceall Java

public class Main {
   public static void main(String args[]) {
      String str1 = "Java Language, Python Language, 
                     Go Language";
      String replace = str1.replaceAll("Language", "lang");
      System.out.println(replace);
   }
}

Output:-

Java lang, Python lang, Go lang

String.replaceall in Java Example

Example-2:- Let us see another program to replace a character with some other character or string.

public class Main {
   public static void main(String args[]) {
      String str1 = "JAvA ProgrAmming LAnguAge";
      String replace = str1.replaceAll("A", "a");
      System.out.println(replace);
   }
}

Output:-

Java Programming Language

In the above program, the character “A” is replaced with the new character “a”. Hence the original string “JAvA ProgrAmming LAnguAge” becomes “Java Programming Language”.

Java Replaceall String Whitespace

Now, we will try to replace all the whitespace in the given string by using the replaceAll() method. The space, and tab are belongs to the whitespace.

Example-3:- String Replaceall Java

public class Main {
   public static void main(String args[]) {
      String str1 = "Learn Java Programming   Language"+
                    " - Know \t Program \n  Website";
      String replace = str1.replaceAll("\\s", "");
      System.out.println(replace);
   }
}

Output:-

LearnJavaProgrammingLanguage-KnowProgramWebsite

Java String.replaceall

In this section, we add the whitespaces to the string by using the replaceAll() method.

Example-4:- String Replaceall Java demonstration

public class Main {

   public static void main(String argvs[]) {
      String string = "KnowProgram";
      System.out.println(string);

      String regex = "";
      string = string.replaceAll(regex, " ");
      System.out.println(string);
   }
}

Output:-

KnowProgram
K n o w P r o g r a m

If you enjoyed this post, share it with your friends. Do you want to share more information about the topic discussed above or do you find anything incorrect? Let us know in the comments. Thank you!

Leave a Comment

Your email address will not be published.