by Dinesh Thakur Category: array

String replaceAll (String regeX, String replace): This method replaces all substrings in the invoking string that matches the given regular expression pattern regeX with the specified replacement string replace. For example,


It will replace all vowels in the invoking string with # symbol. Here [aeiou] is a regular expression which means any of the characters between the square brackets will match


This statement will replace the substring is with the substring was in the given string. You can get the details of the regular expression from the Sun's website.

public class StringReplaceAll

     public static void main(String[] args)
            String strl = "Welcome";
            String str3 = "Where there is a Will , there is a way";
            System.out.println("str3.replaceAll(\"is\",\"was\") = " +str3.replaceAll("is" ,"was"));
            System.out.println("strl.replaceAll(\"[aeiou]\" ,\"#\") = " +strl.replaceAll("[aeiou] " ,"#"));

String replaceAll() in Java Example

About Dinesh Thakur

Dinesh ThakurDinesh Thakur holds an B.C.A, MCSE, MCDBA, CCNA, CCNP, A+, SCJP certifications. Dinesh authors the hugely popular blog. Where he writes how-to guides around Computer fundamental , computer software, Computer programming, and web apps. For any type of query or something that you think is missing, please feel free to Contact us.

Related Articles