🎓 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.regionMatches() method in Java is used to compare a specific region of one string with a region of another string. This guide will cover the method's usage, explain how it works, and provide examples to demonstrate its functionality.Table of Contents
- Introduction
regionMatchesMethod Syntax- Examples
- Basic Region Comparison
- Case-Insensitive Region Comparison
- Handling Edge Cases
- Conclusion
Introduction
The String.regionMatches() method is a member of the String class in Java. It allows you to compare a substring of one string to a substring of another string. This method is particularly useful when you need to compare parts of strings without creating new substrings.
regionMatches Method Syntax
The regionMatches method has two common variations:
- Case-sensitive comparison:
public boolean regionMatches(int toffset, String other, int ooffset, int len)
- Case-insensitive comparison:
public boolean regionMatches(boolean ignoreCase, int toffset, String other, int ooffset, int len)
- ignoreCase: If true, ignore case differences when comparing characters.
- toffset: The starting offset in the first string.
- other: The second string to be compared.
- ooffset: The starting offset in the second string.
- len: The number of characters to compare.
Examples
Basic Region Comparison
The regionMatches method can be used to compare regions of two strings in a case-sensitive manner.
Example
public class RegionMatchesExample {
public static void main(String[] args) {
String str1 = "Hello, World!";
String str2 = "World";
boolean result = str1.regionMatches(7, str2, 0, 5);
System.out.println("Region matches: " + result);
}
}
Output:
Region matches: true
Case-Insensitive Region Comparison
The regionMatches method can also be used to compare regions of two strings in a case-insensitive manner.
Example
public class RegionMatchesExample {
public static void main(String[] args) {
String str1 = "Hello, World!";
String str2 = "world";
boolean result = str1.regionMatches(true, 7, str2, 0, 5);
System.out.println("Region matches (ignore case): " + result);
}
}
Output:
Region matches (ignore case): true
Handling Edge Cases
The regionMatches method returns false if the specified regions do not match.
Example
public class RegionMatchesExample {
public static void main(String[] args) {
String str1 = "Hello, World!";
String str2 = "Java";
boolean result = str1.regionMatches(7, str2, 0, 4);
System.out.println("Region matches: " + result);
}
}
Output:
Region matches: false
Additionally, if the length of the region to be compared exceeds the length of either string, the method will also return false.
Example
public class RegionMatchesExample {
public static void main(String[] args) {
String str1 = "Hello";
String str2 = "Hello, World!";
boolean result = str1.regionMatches(0, str2, 0, 10);
System.out.println("Region matches: " + result);
}
}
Output:
Region matches: false
Conclusion
The String.regionMatches() method in Java is used for comparing specific regions of two strings. By understanding how to use this method, you can efficiently compare parts of strings in your Java applications. Whether you are performing case-sensitive or case-insensitive comparisons, the regionMatches 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