Java String replaceAll() Method
Why do we need string in java?
In Java, an string class is a series of characters that are interpreted literally by a script. Character strings are represented by the String class. Java programs implement “abc” and other string literals as instances of this class.
- Numerous methods, including compare(), concat(), equals(), split(), length(), replace(), compareTo(), intern(), and substring(), are available in the Java String class.
To know more about String Class in java read the complete article.
String replaceAll() Method:
The Java String replaceAll() method replaces each substring of this string that matches the given regular expression with the given replacement. Each substring that matches the provided regular expression is replaced with the replacement provided before the Java String replaceAll() function returns a string.
- The java.lang.String class provides a lot of built-in methods that are used to manipulate string in Java.These methods help us to perform operations on String objects such as trimming, concatenating, converting, comparing, replacing strings etc.
To replace every instance of a substring (that matches the argument regex) with the replacement string, use the String.replaceAll(String regex, String replacement) method.
Syntax:
public String replaceAll(String regex, String replacement)
Parameters:
regex - the regular expression to which this string is to be matched replacement - the string to be substituted for each match
Let’s look at a string-related Java program where the Java String replaceall() method is used to perform an operation on the given string.
Example : Java string replaceall Method
public class Main { public static void main(String[] args) { String str1 = "PrepInsta123or456PrepInsta Prime"; // regex for sequence of digits String regex = "\\d+"; // replace all occurrences of numeric // digits by a space System.out.println(str1.replaceAll(regex, " ")); } }
Output
PrepInsta or PrepInsta Prime
Example 2 : Taking two strings str1 and str2 and we are checking whether the strings are ending with the specified suffixes.
public class Main { public static void main(String[] args) { String str1 = "+a-+b"; // replace "+" with "#" using replaceAll() // need to escape "+" System.out.println(str1.replaceAll("\\+", "#")); // #a-#b // replace "+" with "#" using replace() System.out.println(str1.replace("+", "#")); // #a-#b } }
Output
#a-#b #a-#b
Examples 3: Taking two strings str1 and str2 and we are checking whether the strings are ending with the specified suffixes.
public class Main{ public static void main(String[] args) { String str = "java is best Platform for PrepInsta"; String newStr = str.replaceAll("java", "scala"); System.out.println(newStr); } }
Output
scala is best Platform for PrepInsta
Prime Course Trailer
Related Banners
Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription
Get over 200+ course One Subscription
Courses like AI/ML, Cloud Computing, Ethical Hacking, C, C++, Java, Python, DSA (All Languages), Competitive Coding (All Languages), TCS, Infosys, Wipro, Amazon, DBMS, SQL and others
Login/Signup to comment