🎓 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.replace() method in Java is used to replace occurrences of a specified character or substring with another character or substring. This guide will cover the method's usage, explain how it works, and provide examples to demonstrate its functionality.Table of Contents
- Introduction
replaceMethod Syntax- Examples
- Replacing Characters
- Replacing Substrings
- Replacing with Empty Strings
- Case Sensitivity
- Conclusion
Introduction
The String.replace() method is a member of the String class in Java. It allows you to create a new string by replacing all occurrences of a specified character or substring with another character or substring. This method is particularly useful for text processing, formatting, and data cleaning.
replace Method Syntax
The replace method has two common variations:
- Replacing all occurrences of a character:
public String replace(char oldChar, char newChar)
- Replacing all occurrences of a substring:
public String replace(CharSequence target, CharSequence replacement)
Examples
Replacing Characters
The replace method can be used to replace all occurrences of a specified character with another character.
Example
public class ReplaceExample {
public static void main(String[] args) {
String message = "Hello, World!";
String newMessage = message.replace('o', '0');
System.out.println("Original message: " + message);
System.out.println("New message: " + newMessage);
}
}
Output:
Original message: Hello, World!
New message: Hell0, W0rld!
Replacing Substrings
The replace method can be used to replace all occurrences of a specified substring with another substring.
Example
public class ReplaceExample {
public static void main(String[] args) {
String message = "Hello, World! Hello, Java!";
String newMessage = message.replace("Hello", "Hi");
System.out.println("Original message: " + message);
System.out.println("New message: " + newMessage);
}
}
Output:
Original message: Hello, World! Hello, Java!
New message: Hi, World! Hi, Java!
Replacing with Empty Strings
The replace method can also be used to remove characters or substrings by replacing them with an empty string.
Example
public class ReplaceExample {
public static void main(String[] args) {
String message = "Hello, World!";
String newMessage = message.replace("World", "");
System.out.println("Original message: " + message);
System.out.println("New message: " + newMessage);
}
}
Output:
Original message: Hello, World!
New message: Hello, !
Case Sensitivity
The replace method is case-sensitive, meaning it will distinguish between uppercase and lowercase characters and substrings.
Example
public class ReplaceExample {
public static void main(String[] args) {
String message = "Hello, World!";
String newMessage = message.replace("world", "Java");
System.out.println("Original message: " + message);
System.out.println("New message: " + newMessage);
}
}
Output:
Original message: Hello, World!
New message: Hello, World!
Conclusion
The String.replace() method in Java is a powerful and versatile tool for replacing characters and substrings within a string. By understanding how to use this method, you can efficiently process, format, and clean text in your Java applications. Whether you are replacing characters, substrings, handling case sensitivity, or removing parts of a string, the replace 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