🎓 Top 15 Udemy Courses (80-90% Discount): My Udemy Courses - Ramesh Fadatare — All my Udemy courses are real-time and project oriented courses.
▶️ Subscribe to My YouTube Channel (178K+ subscribers): Java Guides on YouTube
▶️ For AI, ChatGPT, Web, Tech, and Generative AI, subscribe to another channel: Ramesh Fadatare on YouTube
String.replaceAll() method in Java is used to replace each substring of a string that matches a given regular expression with a specified replacement string. This guide will cover the method's usage, explain how it works, and provide examples to demonstrate its functionality.Table of Contents
- Introduction
replaceAllMethod Syntax- Examples
- Replacing Substrings Using a Regular Expression
- Replacing Digits with a String
- Handling Special Characters
- Conclusion
Introduction
The String.replaceAll() method is a member of the String class in Java. It allows you to replace all substrings of a string that match a given regular expression with a specified replacement string. This method is particularly useful for text processing and data cleaning tasks that require pattern-based replacements.
replaceAll Method Syntax
The syntax for the replaceAll method is as follows:
public String replaceAll(String regex, String replacement)
- regex: The regular expression to match substrings to be replaced.
- replacement: The string to replace each matched substring.
Examples
Replacing Substrings Using a Regular Expression
The replaceAll method can be used to replace all occurrences of a pattern in a string with a specified replacement string.
Example
public class ReplaceAllExample {
public static void main(String[] args) {
String message = "The rain in Spain stays mainly in the plain.";
String newMessage = message.replaceAll("ain", "ane");
System.out.println("Original message: " + message);
System.out.println("New message: " + newMessage);
}
}
Output:
Original message: The rain in Spain stays mainly in the plain.
New message: The rane in Spane stays manely in the plane.
Replacing Digits with a String
The replaceAll method can be used to replace all digits in a string with a specified replacement string.
Example
public class ReplaceAllExample {
public static void main(String[] args) {
String message = "Order number: 12345";
String newMessage = message.replaceAll("\\d", "#");
System.out.println("Original message: " + message);
System.out.println("New message: " + newMessage);
}
}
Output:
Original message: Order number: 12345
New message: Order number: #####
Handling Special Characters
The replaceAll method can handle special characters in regular expressions by using escape sequences.
Example
public class ReplaceAllExample {
public static void main(String[] args) {
String message = "Price: $10.00";
String newMessage = message.replaceAll("\\$", "USD ");
System.out.println("Original message: " + message);
System.out.println("New message: " + newMessage);
}
}
Output:
Original message: Price: $10.00
New message: Price: USD 10.00
Removing Whitespace
The replaceAll method can be used to remove all whitespace from a string.
Example
public class ReplaceAllExample {
public static void main(String[] args) {
String message = "Java is fun";
String newMessage = message.replaceAll("\\s", "");
System.out.println("Original message: " + message);
System.out.println("New message: " + newMessage);
}
}
Output:
Original message: Java is fun
New message: Javaisfun
Conclusion
The String.replaceAll() method in Java is used for replacing substrings based on regular expressions. By understanding how to use this method, you can efficiently perform pattern-based replacements in your Java applications. Whether you are replacing specific substrings, digits, special characters, or whitespace, the replaceAll method provides a reliable solution for these tasks.
My Top and Bestseller Udemy Courses. The sale is going on with a 70 - 80% discount. The discount coupon has been added to each course below:
Build REST APIs with Spring Boot 4, Spring Security 7, and JWT
[NEW] Learn Apache Maven with IntelliJ IDEA and Java 25
ChatGPT + Generative AI + Prompt Engineering for Beginners
Spring 7 and Spring Boot 4 for Beginners (Includes 8 Projects)
Available in Udemy for Business
Building Real-Time REST APIs with Spring Boot - Blog App
Available in Udemy for Business
Building Microservices with Spring Boot and Spring Cloud
Available in Udemy for Business
Java Full-Stack Developer Course with Spring Boot and React JS
Available in Udemy for Business
Build 5 Spring Boot Projects with Java: Line-by-Line Coding
Testing Spring Boot Application with JUnit and Mockito
Available in Udemy for Business
Spring Boot Thymeleaf Real-Time Web Application - Blog App
Available in Udemy for Business
Master Spring Data JPA with Hibernate
Available in Udemy for Business
Spring Boot + Apache Kafka Course - The Practical Guide
Available in Udemy for Business
Comments
Post a Comment
Leave Comment