- Java Programming Examples
- Example - Home
- Example - Environment
- Example - Strings
- Example - Arrays
- Example - Date & Time
- Example - Methods
- Example - Files
- Example - Directories
- Example - Exceptions
- Example - Data Structure
- Example - Collections
- Example - Networking
- Example - Threading
- Example - Applets
- Example - Simple GUI
- Example - JDBC
- Example - Regular Exp
- Example - Apache PDF Box
- Example - Apache POI PPT
- Example - Apache POI Excel
- Example - Apache POI Word
- Example - OpenCV
- Example - Apache Tika
- Example - iText
- Java Tutorial
- Java - Tutorial
- Java Useful Resources
- Java - Quick Guide
- Java - Useful Resources
How to match regions in strings using Java
Problem Description
How to match regions in strings?
Solution
Following example determines region matchs in two strings by using regionMatches() method.
public class StringRegionMatch { public static void main(String[] args) { String first_str = "Welcome to Microsoft"; String second_str = "I work with Microsoft"; boolean match = first_str.regionMatches(11, second_str, 12, 9); System.out.println("first_str[11 -19] == " + "second_str[12 - 21]:-"+ match); } }
11 is the index number in the source string from where comparison starts
Second_str is the destination string
12 is the index number from where comparison should start in destination string
9 is the number of characters to compare
Result
The above code sample will produce the following result.
first_str[11 -19] == second_str[12 - 21]:-true
java_strings.htm
Advertisements