/** The value is used for character storage. */ private final char value[]; /** Cache the hash code for the string */ private int hash; // Default to 0 /** use serialVersionUID from JDK 1.0.2 for interoperability */ private static final long serialVersionUID = -6849794470754667710L; public static final Comparator CASE_INSENSITIVE_ORDER = new CaseInsensitiveComparator();
/** * 这是一个雷同于equals的方法,比较的是字符串的片段,也便是部分地域的比较 * toffset是当前字符串的比较起始位置(偏移量),other是要比较的String对象参数,ooffset是要参数String的比较片段起始位置,len是两个字符串要比较的片段的长度大小 * * 例子:String str1 = "0123456",Str2 = "0123456789"; * str1.regionMatchs(0,str2,0,6);意思是str1从0位置开始于str2的0位置开始比较6个长度的字符串片段 * 相等则返回 true,不等返回false */ public boolean regionMatches(int toffset, String other, int ooffset, int len) { char ta[] = value; //当前对象的值 int to = toffset; //当前对象的比较片段的起始位置,既偏移量 char pa[] = other.value; //参数,既比较字符串的值 int po = ooffset; //比较字符串的起始位置 // Note: toffset, ooffset, or len might be near -1>>>1. if ((ooffset < 0) || (toffset < 0) //起始位置不小于0或起始位置不大于字符串长度 - 片段长度,大于就截取不到这么长的片段了 || (toffset > (long)value.length - len) || (ooffset > (long)other.value.length - len)) { return false; //惊讶脸,居然不是抛异常,而是返回false } while (len-- > 0) { //使用while循环,固然也可以使for循环 if (ta[to++] != pa[po++]) { //片段地域的字符元素逐个比较 return false; } } return true; } /** * 这个跟上面的方法一样,只不过多了一个参数,既ignoreCase,既是否为区分大小写。 * 是equalsIgnoreCase()方法的片段比较版本,实际上equalsIgnoreCase()也是调用regionMatches函数 */ public boolean regionMatches(boolean ignoreCase, int toffset, String other, int ooffset, int len) { char ta[] = value; int to = toffset; char pa[] = other.value; int po = ooffset; // Note: toffset, ooffset, or len might be near -1>>>1. if ((ooffset < 0) || (toffset < 0) || (toffset > (long)value.length - len) || (ooffset > (long)other.value.length - len)) { return false; } //上面的表明同上 while (len-- > 0) { char c1 = ta[to++]; char c2 = pa[po++]; if (c1 == c2) { continue; } if (ignoreCase) { //当ignoreCase为true时,既忽视大小写时 // If characters don't match but case may be ignored, // try converting both characters to uppercase. // If the results match, then the comparison scan should // continue. char u1 = Character.toUpperCase(c1); //片段中每个字符转换为大写 char u2 = Character.toUpperCase(c2); if (u1 == u2) { //大写比较一次,假如相等则不执行下面的语句,进入下一个循环 continue; } // Unfortunately, conversion to uppercase does not work properly // for the Georgian alphabet, which has strange rules about case // conversion. So we need to make one last check before // exiting. if (Character.toLowerCase(u1) == Character.toLowerCase(u2)) { //每个字符换行成小写比较一次 continue; } } return false; } return true; }
public String[] split(String regex, int limit) { /* fastpath if the regex is a (1)one-char String and this character is not one of the RegEx's meta characters ".$|()[{^?*+\\", or (2)two-char String and the first char is the backslash and the second is not the ascii digit or ascii letter. */ char ch = 0; if (((regex.value.length == 1 && ".$|()[{^?*+\\".indexOf(ch = regex.charAt(0)) == -1) || (regex.length() == 2 && regex.charAt(0) == '\\' && (((ch = regex.charAt(1))-'0')|('9'-ch)) < 0 && ((ch-'a')|('z'-ch)) < 0 && ((ch-'A')|('Z'-ch)) < 0)) && (ch < Character.MIN_HIGH_SURROGATE || ch > Character.MAX_LOW_SURROGATE)) { int off = 0; int next = 0; boolean limited = limit > 0; ArrayList list = new ArrayList(); while ((next = indexOf(ch, off)) != -1) { if (!limited || list.size() < limit - 1) { list.add(substring(off, next)); off = next + 1; } else { // last one //assert (list.size() == limit - 1); list.add(substring(off, value.length)); off = value.length; break; } } // If no match was found, return this if (off == 0) return new String[]{this}; // Add remaining segment if (!limited || list.size() < limit) list.add(substring(off, value.length)); // Construct result int resultSize = list.size(); if (limit == 0) { while (resultSize > 0 && list.get(resultSize - 1).length() == 0) { resultSize--; } } String[] result = new String[resultSize]; return list.subList(0, resultSize).toArray(result); } return Pattern.compile(regex).split(this, limit); } public String[] split(String regex) { return split(regex, 0); }