Home >Java >javaTutorial >What is the use of regionMatches method in java
regionMatches method:
Note: The regionMatches() method is used to detect whether two strings are equal within a region.
public class RegionMatches{ public static void main(String[] args) { int number = 0; String s = "student;entropy;ENgage,English,client,eye"; String q="en"; //需要查找的字符串 for (int k=0;k<s.length();k++){ //true为不区分大小写,k为所有字符串,q为需要查找的字符串,0为从字符串1的位置开始,2为需要查找的字符串长度为2 if(s.regionMatches(true, k, q, 0, 2)){ number++; System.out.println("en字符在字符串的第"+k+"个位置"); } } System.out.println("含有‘en'子串的字符串的总数有:"+number); } }
Running result:
en character is in the 4th position of the string
en character is in the 8th position of the string
en character is in the string The 16th position
en character is at the 23rd position of the string
en character is at the 34th position of the string
The total number of strings containing the 'en' substring is: 5
The above is the detailed content of What is the use of regionMatches method in java. For more information, please follow other related articles on the PHP Chinese website!