1 7 8 package java.lang; 9 10 import java.io.ObjectStreamClass ; 11 import java.io.ObjectStreamField ; 12 import java.io.UnsupportedEncodingException ; 13 import java.util.ArrayList ; 14 import java.util.Comparator ; 15 import java.util.Formatter ; 16 import java.util.Locale ; 17 import java.util.regex.Matcher ; 18 import java.util.regex.Pattern ; 19 import java.util.regex.PatternSyntaxException ; 20 21 22 89 90 public final class String 91 implements java.io.Serializable , Comparable <String >, CharSequence 92 { 93 94 private final char value[]; 95 96 97 private final int offset; 98 99 100 private final int count; 101 102 103 private int hash; 105 106 private static final long serialVersionUID = -6849794470754667710L; 107 108 120 private static final ObjectStreamField [] serialPersistentFields = 121 new ObjectStreamField [0]; 122 123 128 public String() { 129 this.offset = 0; 130 this.count = 0; 131 this.value = new char[0]; 132 } 133 134 143 public String(String original) { 144 int size = original.count; 145 char[] originalValue = original.value; 146 char[] v; 147 if (originalValue.length > size) { 148 v = new char[size]; 152 System.arraycopy(originalValue, original.offset, v, 0, size); 153 } else { 154 v = originalValue; 157 } 158 this.offset = 0; 159 this.count = size; 160 this.value = v; 161 } 162 163 172 public String(char value[]) { 173 int size = value.length; 174 char[] v = new char[size]; 175 System.arraycopy(value, 0, v, 0, size); 176 this.offset = 0; 177 this.count = size; 178 this.value = v; 179 } 180 181 197 public String(char value[], int offset, int count) { 198 if (offset < 0) { 199 throw new StringIndexOutOfBoundsException (offset); 200 } 201 if (count < 0) { 202 throw new StringIndexOutOfBoundsException (count); 203 } 204 if (offset > value.length - count) { 206 throw new StringIndexOutOfBoundsException (offset + count); 207 } 208 char[] v = new char[count]; 209 System.arraycopy(value, offset, v, 0, count); 210 this.offset = 0; 211 this.count = count; 212 this.value = v; 213 } 214 215 235 public String(int[] codePoints, int offset, int count) { 236 if (offset < 0) { 237 throw new StringIndexOutOfBoundsException (offset); 238 } 239 if (count < 0) { 240 throw new StringIndexOutOfBoundsException (count); 241 } 242 if (offset > codePoints.length - count) { 244 throw new StringIndexOutOfBoundsException (offset + count); 245 } 246 247 int expansion = 0; 248 int margin = 1; 249 char[] v = new char[count + margin]; 250 int x = offset; 251 int j = 0; 252 for (int i = 0; i < count; i++) { 253 int c = codePoints[x++]; 254 if (c < 0) { 255 throw new IllegalArgumentException (); 256 } 257 if (margin <= 0 && (j+1) >= v.length) { 258 if (expansion == 0) { 259 expansion = (((-margin + 1) * count) << 10) / i; 260 expansion >>= 10; 261 if (expansion <= 0) { 262 expansion = 1; 263 } 264 } else { 265 expansion *= 2; 266 } 267 char[] tmp = new char[Math.min(v.length+expansion, count*2)]; 268 margin = (tmp.length - v.length) - (count - i); 269 System.arraycopy(v, 0, tmp, 0, j); 270 v = tmp; 271 } 272 if (c < Character.MIN_SUPPLEMENTARY_CODE_POINT) { 273 v[j++] = (char) c; 274 } else if (c <= Character.MAX_CODE_POINT) { 275 Character.toSurrogates(c, v, j); 276 j += 2; 277 margin--; 278 } else { 279 throw new IllegalArgumentException (); 280 } 281 } 282 this.offset = 0; 283 this.value = v; 284 this.count = j; 285 } 286 287 315 @Deprecated 316 public String(byte ascii[], int hibyte, int offset, int count) { 317 checkBounds(ascii, offset, count); 318 char value[] = new char[count]; 319 320 if (hibyte == 0) { 321 for (int i = count ; i-- > 0 ;) { 322 value[i] = (char) (ascii[i + offset] & 0xff); 323 } 324 } else { 325 hibyte <<= 8; 326 for (int i = count ; i-- > 0 ;) { 327 value[i] = (char) (hibyte | (ascii[i + offset] & 0xff)); 328 } 329 } 330 this.offset = 0; 331 this.count = count; 332 this.value = value; 333 } 334 335 357 @Deprecated 358 public String(byte ascii[], int hibyte) { 359 this(ascii, hibyte, 0, ascii.length); 360 } 361 362 366 private static void checkBounds(byte[] bytes, int offset, int length) { 367 if (length < 0) 368 throw new StringIndexOutOfBoundsException (length); 369 if (offset < 0) 370 throw new StringIndexOutOfBoundsException (offset); 371 if (offset > bytes.length - length) 372 throw new StringIndexOutOfBoundsException (offset + length); 373 } 374 375 399 public String(byte bytes[], int offset, int length, String charsetName) 400 throws UnsupportedEncodingException 401 { 402 if (charsetName == null) 403 throw new NullPointerException ("charsetName"); 404 checkBounds(bytes, offset, length); 405 char[] v = StringCoding.decode(charsetName, bytes, offset, length); 406 this.offset = 0; 407 this.count = v.length; 408 this.value = v; 409 } 410 411 430 public String(byte bytes[], String charsetName) 431 throws UnsupportedEncodingException 432 { 433 this(bytes, 0, bytes.length, charsetName); 434 } 435 436 456 public String(byte bytes[], int offset, int length) { 457 checkBounds(bytes, offset, length); 458 char[] v = StringCoding.decode(bytes, offset, length); 459 this.offset = 0; 460 this.count = v.length; 461 this.value = v; 462 } 463 464 478 public String(byte bytes[]) { 479 this(bytes, 0, bytes.length); 480 } 481 482 490 public String(StringBuffer buffer) { 491 String result = buffer.toString(); 492 this.value = result.value; 493 this.count = result.count; 494 this.offset = result.offset; 495 } 496 497 511 public String(StringBuilder builder) { 512 String result = builder.toString(); 513 this.value = result.value; 514 this.count = result.count; 515 this.offset = result.offset; 516 } 517 518 519 String(int offset, int count, char value[]) { 521 this.value = value; 522 this.offset = offset; 523 this.count = count; 524 } 525 526 534 public int length() { 535 return count; 536 } 537 538 556 public char charAt(int index) { 557 if ((index < 0) || (index >= count)) { 558 throw new StringIndexOutOfBoundsException (index); 559 } 560 return value[index + offset]; 561 } 562 563 585 public int codePointAt(int index) { 586 if ((index < 0) || (index >= count)) { 587 throw new StringIndexOutOfBoundsException (index); 588 } 589 return Character.codePointAtImpl(value, offset + index, offset + count); 590 } 591 592 614 public int codePointBefore(int index) { 615 int i = index - 1; 616 if ((i < 0) || (i >= count)) { 617 throw new StringIndexOutOfBoundsException (index); 618 } 619 return Character.codePointBeforeImpl(value, offset + index, offset); 620 } 621 622 643 public int codePointCount(int beginIndex, int endIndex) { 644 if (beginIndex < 0 || endIndex > count || beginIndex > endIndex) { 645 throw new IndexOutOfBoundsException (); 646 } 647 return Character.codePointCountImpl(value, offset+beginIndex, endIndex-beginIndex); 648 } 649 650 670 public int offsetByCodePoints(int index, int codePointOffset) { 671 if (index < 0 || index > count) { 672 throw new IndexOutOfBoundsException (); 673 } 674 return Character.offsetByCodePointsImpl(value, offset, count, 675 offset+index, codePointOffset); 676 } 677 678 682 void getChars(char dst[], int dstBegin) { 683 System.arraycopy(value, offset, dst, dstBegin, count); 684 } 685 686 716 public void getChars(int srcBegin, int srcEnd, char dst[], int dstBegin) { 717 if (srcBegin < 0) { 718 throw new StringIndexOutOfBoundsException (srcBegin); 719 } 720 if (srcEnd > count) { 721 throw new StringIndexOutOfBoundsException (srcEnd); 722 } 723 if (srcBegin > srcEnd) { 724 throw new StringIndexOutOfBoundsException (srcEnd - srcBegin); 725 } 726 System.arraycopy(value, offset + srcBegin, dst, dstBegin, 727 srcEnd - srcBegin); 728 } 729 730 767 @Deprecated 768 public void getBytes(int srcBegin, int srcEnd, byte dst[], int dstBegin) { 769 if (srcBegin < 0) { 770 throw new StringIndexOutOfBoundsException (srcBegin); 771 } 772 if (srcEnd > count) { 773 throw new StringIndexOutOfBoundsException (srcEnd); 774 } 775 if (srcBegin > srcEnd) { 776 throw new StringIndexOutOfBoundsException (srcEnd - srcBegin); 777 } 778 int j = dstBegin; 779 int n = offset + srcEnd; 780 int i = offset + srcBegin; 781 char[] val = value; 782 783 while (i < n) { 784 dst[j++] = (byte)val[i++]; 785 } 786 } 787 788 808 public byte[] getBytes(String charsetName) 809 throws UnsupportedEncodingException 810 { 811 if (charsetName == null) throw new NullPointerException (); 812 return StringCoding.encode(charsetName, value, offset, count); 813 } 814 815 828 public byte[] getBytes() { 829 return StringCoding.encode(value, offset, count); 830 } 831 832 845 public boolean equals(Object anObject) { 846 if (this == anObject) { 847 return true; 848 } 849 if (anObject instanceof String ) { 850 String anotherString = (String )anObject; 851 int n = count; 852 if (n == anotherString.count) { 853 char v1[] = value; 854 char v2[] = anotherString.value; 855 int i = offset; 856 int j = anotherString.offset; 857 while (n-- != 0) { 858 if (v1[i++] != v2[j++]) 859 return false; 860 } 861 return true; 862 } 863 } 864 return false; 865 } 866 867 878 public boolean contentEquals(StringBuffer sb) { 879 synchronized(sb) { 880 return contentEquals((CharSequence )sb); 881 } 882 } 883 884 895 public boolean contentEquals(CharSequence cs) { 896 if (count != cs.length()) 897 return false; 898 if (cs instanceof AbstractStringBuilder ) { 900 char v1[] = value; 901 char v2[] = ((AbstractStringBuilder )cs).getValue(); 902 int i = offset; 903 int j = 0; 904 int n = count; 905 while (n-- != 0) { 906 if (v1[i++] != v2[j++]) 907 return false; 908 } 909 } 910 if (cs.equals(this)) 912 return true; 913 char v1[] = value; 915 int i = offset; 916 int j = 0; 917 int n = count; 918 while (n-- != 0) { 919 if (v1[i++] != cs.charAt(j++)) 920 return false; 921 } 922 return true; 923 } 924 925 949 public boolean equalsIgnoreCase(String anotherString) { 950 return (this == anotherString) ? true : 951 (anotherString != null) && (anotherString.count == count) && 952 regionMatches(true, 0, anotherString, 0, count); 953 } 954 955 996 public int compareTo(String anotherString) { 997 int len1 = count; 998 int len2 = anotherString.count; 999 int n = Math.min(len1, len2); 1000 char v1[] = value; 1001 char v2[] = anotherString.value; 1002 int i = offset; 1003 int j = anotherString.offset; 1004 1005 if (i == j) { 1006 int k = i; 1007 int lim = n + i; 1008 while (k < lim) { 1009 char c1 = v1[k]; 1010 char c2 = v2[k]; 1011 if (c1 != c2) { 1012 return c1 - c2; 1013 } 1014 k++; 1015 } 1016 } else { 1017 while (n-- != 0) { 1018 char c1 = v1[i++]; 1019 char c2 = v2[j++]; 1020 if (c1 != c2) { 1021 return c1 - c2; 1022 } 1023 } 1024 } 1025 return len1 - len2; 1026 } 1027 1028 1040 public static final Comparator <String > CASE_INSENSITIVE_ORDER 1041 = new CaseInsensitiveComparator(); 1042 private static class CaseInsensitiveComparator 1043 implements Comparator <String >, java.io.Serializable { 1044 private static final long serialVersionUID = 8575799808933029326L; 1046 1047 public int compare(String s1, String s2) { 1048 int n1=s1.length(), n2=s2.length(); 1049 for (int i1=0, i2=0; i1<n1 && i2<n2; i1++, i2++) { 1050 char c1 = s1.charAt(i1); 1051 char c2 = s2.charAt(i2); 1052 if (c1 != c2) { 1053 c1 = Character.toUpperCase(c1); 1054 c2 = Character.toUpperCase(c2); 1055 if (c1 != c2) { 1056 c1 = Character.toLowerCase(c1); 1057 c2 = Character.toLowerCase(c2); 1058 if (c1 != c2) { 1059 return c1 - c2; 1060 } 1061 } 1062 } 1063 } 1064 return n1 - n2; 1065 } 1066 } 1067 1068 1088 public int compareToIgnoreCase(String str) { 1089 return CASE_INSENSITIVE_ORDER.compare(this, str); 1090 } 1091 1092 1123 public boolean regionMatches(int toffset, String other, int ooffset, 1124 int len) { 1125 char ta[] = value; 1126 int to = offset + toffset; 1127 char pa[] = other.value; 1128 int po = other.offset + ooffset; 1129 if ((ooffset < 0) || (toffset < 0) || (toffset > (long)count - len) 1131 || (ooffset > (long)other.count - len)) { 1132 return false; 1133 } 1134 while (len-- > 0) { 1135 if (ta[to++] != pa[po++]) { 1136 return false; 1137 } 1138 } 1139 return true; 1140 } 1141 1142 1192 public boolean regionMatches(boolean ignoreCase, int toffset, 1193 String other, int ooffset, int len) { 1194 char ta[] = value; 1195 int to = offset + toffset; 1196 char pa[] = other.value; 1197 int po = other.offset + ooffset; 1198 if ((ooffset < 0) || (toffset < 0) || (toffset > (long)count - len) || 1200 (ooffset > (long)other.count - len)) { 1201 return false; 1202 } 1203 while (len-- > 0) { 1204 char c1 = ta[to++]; 1205 char c2 = pa[po++]; 1206 if (c1 == c2) { 1207 continue; 1208 } 1209 if (ignoreCase) { 1210 char u1 = Character.toUpperCase(c1); 1215 char u2 = Character.toUpperCase(c2); 1216 if (u1 == u2) { 1217 continue; 1218 } 1219 if (Character.toLowerCase(u1) == Character.toLowerCase(u2)) { 1224 continue; 1225 } 1226 } 1227 return false; 1228 } 1229 return true; 1230 } 1231 1232 1249 public boolean startsWith(String prefix, int toffset) { 1250 char ta[] = value; 1251 int to = offset + toffset; 1252 char pa[] = prefix.value; 1253 int po = prefix.offset; 1254 int pc = prefix.count; 1255 if ((toffset < 0) || (toffset > count - pc)) { 1257 return false; 1258 } 1259 while (--pc >= 0) { 1260 if (ta[to++] != pa[po++]) { 1261 return false; 1262 } 1263 } 1264 return true; 1265 } 1266 1267 1280 public boolean startsWith(String prefix) { 1281 return startsWith(prefix, 0); 1282 } 1283 1284 1295 public boolean endsWith(String suffix) { 1296 return startsWith(suffix, count - suffix.count); 1297 } 1298 1299 1312 public int hashCode() { 1313 int h = hash; 1314 if (h == 0) { 1315 int off = offset; 1316 char val[] = value; 1317 int len = count; 1318 1319 for (int i = 0; i < len; i++) { 1320 h = 31*h + val[off++]; 1321 } 1322 hash = h; 1323 } 1324 return h; 1325 } 1326 1327 1351 public int indexOf(int ch) { 1352 return indexOf(ch, 0); 1353 } 1354 1355 1394 public int indexOf(int ch, int fromIndex) { 1395 int max = offset + count; 1396 char v[] = value; 1397 1398 if (fromIndex < 0) { 1399 fromIndex = 0; 1400 } else if (fromIndex >= count) { 1401 return -1; 1403 } 1404 1405 int i = offset + fromIndex; 1406 if (ch < Character.MIN_SUPPLEMENTARY_CODE_POINT) { 1407 for (; i < max ; i++) { 1410 if (v[i] == ch) { 1411 return i - offset; 1412 } 1413 } 1414 return -1; 1415 } 1416 1417 if (ch <= Character.MAX_CODE_POINT) { 1418 char[] surrogates = Character.toChars(ch); 1420 for (; i < max; i++) { 1421 if (v[i] == surrogates[0]) { 1422 if (i + 1 == max) { 1423 break; 1424 } 1425 if (v[i+1] == surrogates[1]) { 1426 return i - offset; 1427 } 1428 } 1429 } 1430 } 1431 return -1; 1432 } 1433 1434 1457 public int lastIndexOf(int ch) { 1458 return lastIndexOf(ch, count - 1); 1459 } 1460 1461 1495 public int lastIndexOf(int ch, int fromIndex) { 1496 int min = offset; 1497 char v[] = value; 1498 1499 int i = offset + ((fromIndex >= count) ? count - 1 : fromIndex); 1500 1501 if (ch < Character.MIN_SUPPLEMENTARY_CODE_POINT) { 1502 for (; i >= min ; i--) { 1505 if (v[i] == ch) { 1506 return i - offset; 1507 } 1508 } 1509 return -1; 1510 } 1511 1512 int max = offset + count; 1513 if (ch <= Character.MAX_CODE_POINT) { 1514 char[] surrogates = Character.toChars(ch); 1516 for (; i >= min; i--) { 1517 if (v[i] == surrogates[0]) { 1518 if (i + 1 == max) { 1519 break; 1520 } 1521 if (v[i+1] == surrogates[1]) { 1522 return i - offset; 1523 } 1524 } 1525 } 1526 } 1527 return -1; 1528 } 1529 1530 1545 public int indexOf(String str) { 1546 return indexOf(str, 0); 1547 } 1548 1549 1563 public int indexOf(String str, int fromIndex) { 1564 return indexOf(value, offset, count, 1565 str.value, str.offset, str.count, fromIndex); 1566 } 1567 1568 1581 static int indexOf(char[] source, int sourceOffset, int sourceCount, 1582 char[] target, int targetOffset, int targetCount, 1583 int fromIndex) { 1584 if (fromIndex >= sourceCount) { 1585 return (targetCount == 0 ? sourceCount : -1); 1586 } 1587 if (fromIndex < 0) { 1588 fromIndex = 0; 1589 } 1590 if (targetCount == 0) { 1591 return fromIndex; 1592 } 1593 1594 char first = target[targetOffset]; 1595 int max = sourceOffset + (sourceCount - targetCount); 1596 1597 for (int i = sourceOffset + fromIndex; i <= max; i++) { 1598 1599 if (source[i] != first) { 1600 while (++i <= max && source[i] != first); 1601 } 1602 1603 1604 if (i <= max) { 1605 int j = i + 1; 1606 int end = j + targetCount - 1; 1607 for (int k = targetOffset + 1; j < end && source[j] == 1608 target[k]; j++, k++); 1609 1610 if (j == end) { 1611 1612 return i - sourceOffset; 1613 } 1614 } 1615 } 1616 return -1; 1617 } 1618 1619 1635 public int lastIndexOf(String str) { 1636 return lastIndexOf(str, count); 1637 } 1638 1639 1653 public int lastIndexOf(String str, int fromIndex) { 1654 return lastIndexOf(value, offset, count, 1655 str.value, str.offset, str.count, fromIndex); 1656 } 1657 1658 1671 static int lastIndexOf(char[] source, int sourceOffset, int sourceCount, 1672 char[] target, int targetOffset, int targetCount, 1673 int fromIndex) { 1674 1678 int rightIndex = sourceCount - targetCount; 1679 if (fromIndex < 0) { 1680 return -1; 1681 } 1682 if (fromIndex > rightIndex) { 1683 fromIndex = rightIndex; 1684 } 1685 1686 if (targetCount == 0) { 1687 return fromIndex; 1688 } 1689 1690 int strLastIndex = targetOffset + targetCount - 1; 1691 char strLastChar = target[strLastIndex]; 1692 int min = sourceOffset + targetCount - 1; 1693 int i = min + fromIndex; 1694 1695 startSearchForLastChar: 1696 while (true) { 1697 while (i >= min && source[i] != strLastChar) { 1698 i--; 1699 } 1700 if (i < min) { 1701 return -1; 1702 } 1703 int j = i - 1; 1704 int start = j - (targetCount - 1); 1705 int k = strLastIndex - 1; 1706 1707 while (j > start) { 1708 if (source[j--] != target[k--]) { 1709 i--; 1710 continue startSearchForLastChar; 1711 } 1712 } 1713 return start - sourceOffset + 1; 1714 } 1715 } 1716 1717 1734 public String substring(int beginIndex) { 1735 return substring(beginIndex, count); 1736 } 1737 1738 1760 public String substring(int beginIndex, int endIndex) { 1761 if (beginIndex < 0) { 1762 throw new StringIndexOutOfBoundsException (beginIndex); 1763 } 1764 if (endIndex > count) { 1765 throw new StringIndexOutOfBoundsException (endIndex); 1766 } 1767 if (beginIndex > endIndex) { 1768 throw new StringIndexOutOfBoundsException (endIndex - beginIndex); 1769 } 1770 return ((beginIndex == 0) && (endIndex == count)) ? this : 1771 new String (offset + beginIndex, endIndex - beginIndex, value); 1772 } 1773 1774 1802 public CharSequence subSequence(int beginIndex, int endIndex) { 1803 return this.substring(beginIndex, endIndex); 1804 } 1805 1806 1826 public String concat(String str) { 1827 int otherLen = str.length(); 1828 if (otherLen == 0) { 1829 return this; 1830 } 1831 char buf[] = new char[count + otherLen]; 1832 getChars(0, count, buf, 0); 1833 str.getChars(0, otherLen, buf, count); 1834 return new String (0, count + otherLen, buf); 1835 } 1836 1837 1866 public String replace(char oldChar, char newChar) { 1867 if (oldChar != newChar) { 1868 int len = count; 1869 int i = -1; 1870 char[] val = value; 1871 int off = offset; 1872 1873 while (++i < len) { 1874 if (val[off + i] == oldChar) { 1875 break; 1876 } 1877 } 1878 if (i < len) { 1879 char buf[] = new char[len]; 1880 for (int j = 0 ; j < i ; j++) { 1881 buf[j] = val[off+j]; 1882 } 1883 while (i < len) { 1884 char c = val[off + i]; 1885 buf[i] = (c == oldChar) ? newChar : c; 1886 i++; 1887 } 1888 return new String (0, len, buf); 1889 } 1890 } 1891 return this; 1892 } 1893 1894 1920 public boolean matches(String regex) { 1921 return Pattern.matches(regex, this); 1922 } 1923 1924 1933 public boolean contains(CharSequence s) { 1934 return indexOf(s.toString()) > -1; 1935 } 1936 1937 1966 public String replaceFirst(String regex, String replacement) { 1967 return Pattern.compile(regex).matcher(this).replaceFirst(replacement); 1968 } 1969 1970 1999 public String replaceAll(String regex, String replacement) { 2000 return Pattern.compile(regex).matcher(this).replaceAll(replacement); 2001 } 2002 2003 2017 public String replace(CharSequence target, CharSequence replacement) { 2018 return Pattern.compile(target.toString(), Pattern.LITERAL).matcher( 2019 this).replaceAll(Matcher.quoteReplacement(replacement.toString())); 2020 } 2021 2022 2102 public String [] split(String regex, int limit) { 2103 return Pattern.compile(regex).split(this, limit); 2104 } 2105 2106 2144 public String [] split(String regex) { 2145 return split(regex, 0); 2146 } 2147 2148 2200 public String toLowerCase(Locale locale) { 2201 if (locale == null) { 2202 throw new NullPointerException (); 2203 } 2204 2205 int firstUpper; 2206 2207 2208 scan: { 2209 for (firstUpper = 0 ; firstUpper < count; ) { 2210 char c = value[offset+firstUpper]; 2211 if ((c >= Character.MIN_HIGH_SURROGATE) && 2212 (c <= Character.MAX_HIGH_SURROGATE)) { 2213 int supplChar = codePointAt(firstUpper); 2214 if (supplChar != Character.toLowerCase(supplChar)) { 2215 break scan; 2216 } 2217 firstUpper += Character.charCount(supplChar); 2218 } else { 2219 if (c != Character.toLowerCase(c)) { 2220 break scan; 2221 } 2222 firstUpper++; 2223 } 2224 } 2225 return this; 2226 } 2227 2228 char[] result = new char[count]; 2229 int resultOffset = 0; 2231 2232 2233 System.arraycopy(value, offset, result, 0, firstUpper); 2234 2235 String lang = locale.getLanguage(); 2236 boolean localeDependent = 2237 (lang == "tr" || lang == "az" || lang == "lt"); 2238 char[] lowerCharArray; 2239 int lowerChar; 2240 int srcChar; 2241 int srcCount; 2242 for (int i = firstUpper; i < count; i += srcCount) { 2243 srcChar = (int)value[offset+i]; 2244 if ((char)srcChar >= Character.MIN_HIGH_SURROGATE && 2245 (char)srcChar <= Character.MAX_HIGH_SURROGATE) { 2246 srcChar = codePointAt(i); 2247 srcCount = Character.charCount(srcChar); 2248 } else { 2249 srcCount = 1; 2250 } 2251 if (localeDependent || srcChar == '\u03A3') { lowerChar = ConditionalSpecialCasing.toLowerCaseEx(this, i, locale); 2253 } else { 2254 lowerChar = Character.toLowerCase(srcChar); 2255 } 2256 if ((lowerChar == Character.ERROR) || 2257 (lowerChar >= Character.MIN_SUPPLEMENTARY_CODE_POINT)) { 2258 if (lowerChar == Character.ERROR) { 2259 lowerCharArray = 2260 ConditionalSpecialCasing.toLowerCaseCharArray(this, i, locale); 2261 } else if (srcCount == 2) { 2262 resultOffset += Character.toChars(lowerChar, result, i + resultOffset) - srcCount; 2263 continue; 2264 } else { 2265 lowerCharArray = Character.toChars(lowerChar); 2266 } 2267 2268 2269 int mapLen = lowerCharArray.length; 2270 if (mapLen > srcCount) { 2271 char[] result2 = new char[result.length + mapLen - srcCount]; 2272 System.arraycopy(result, 0, result2, 0, 2273 i + resultOffset); 2274 result = result2; 2275 } 2276 for (int x=0; x<mapLen; ++x) { 2277 result[i+resultOffset+x] = lowerCharArray[x]; 2278 } 2279 resultOffset += (mapLen - srcCount); 2280 } else { 2281 result[i+resultOffset] = (char)lowerChar; 2282 } 2283 } 2284 return new String (0, count+resultOffset, result); 2285 } 2286 2287 2295 public String toLowerCase() { 2296 return toLowerCase(Locale.getDefault()); 2297 } 2298 2299 2347 public String toUpperCase(Locale locale) { 2348 if (locale == null) { 2349 throw new NullPointerException (); 2350 } 2351 2352 int firstLower; 2353 2354 2355 scan: { 2356 for (firstLower = 0 ; firstLower < count; ) { 2357 int c = (int)value[offset+firstLower]; 2358 int srcCount; 2359 if ((c >= Character.MIN_HIGH_SURROGATE) && 2360 (c <= Character.MAX_HIGH_SURROGATE)) { 2361 c = codePointAt(firstLower); 2362 srcCount = Character.charCount(c); 2363 } else { 2364 srcCount = 1; 2365 } 2366 int upperCaseChar = Character.toUpperCaseEx(c); 2367 if ((upperCaseChar == Character.ERROR) || 2368 (c != upperCaseChar)) { 2369 break scan; 2370 } 2371 firstLower += srcCount; 2372 } 2373 return this; 2374 } 2375 2376 char[] result = new char[count]; 2377 int resultOffset = 0; 2379 2380 2381 System.arraycopy(value, offset, result, 0, firstLower); 2382 2383 String lang = locale.getLanguage(); 2384 boolean localeDependent = 2385 (lang == "tr" || lang == "az" || lang == "lt"); 2386 char[] upperCharArray; 2387 int upperChar; 2388 int srcChar; 2389 int srcCount; 2390 for (int i = firstLower; i < count; i += srcCount) { 2391 srcChar = (int)value[offset+i]; 2392 if ((char)srcChar >= Character.MIN_HIGH_SURROGATE && 2393 (char)srcChar <= Character.MAX_HIGH_SURROGATE) { 2394 srcChar = codePointAt(i); 2395 srcCount = Character.charCount(srcChar); 2396 } else { 2397 srcCount = 1; 2398 } 2399 if (localeDependent) { 2400 upperChar = ConditionalSpecialCasing.toUpperCaseEx(this, i, locale); 2401 } else { 2402 upperChar = Character.toUpperCaseEx(srcChar); 2403 } 2404 if ((upperChar == Character.ERROR) || 2405 (upperChar >= Character.MIN_SUPPLEMENTARY_CODE_POINT)) { 2406 if (upperChar == Character.ERROR) { 2407 if (localeDependent) { 2408 upperCharArray = 2409 ConditionalSpecialCasing.toUpperCaseCharArray(this, i, locale); 2410 } else { 2411 upperCharArray = Character.toUpperCaseCharArray(srcChar); 2412 } 2413 } else if (srcCount == 2) { 2414 resultOffset += Character.toChars(upperChar, result, i + resultOffset) - srcCount; 2415 continue; 2416 } else { 2417 upperCharArray = Character.toChars(upperChar); 2418 } 2419 2420 2421 int mapLen = upperCharArray.length; 2422 if (mapLen > srcCount) { 2423 char[] result2 = new char[result.length + mapLen - srcCount]; 2424 System.arraycopy(result, 0, result2, 0, 2425 i + resultOffset); 2426 result = result2; 2427 } 2428 for (int x=0; x<mapLen; ++x) { 2429 result[i+resultOffset+x] = upperCharArray[x]; 2430 } 2431 resultOffset += (mapLen - srcCount); 2432 } else { 2433 result[i+resultOffset] = (char)upperChar; 2434 } 2435 } 2436 return new String (0, count+resultOffset, result); 2437 } 2438 2439 2447 public String toUpperCase() { 2448 return toUpperCase(Locale.getDefault()); 2449 } 2450 2451 2482 public String trim() { 2483 int len = count; 2484 int st = 0; 2485 int off = offset; 2486 char[] val = value; 2487 2488 while ((st < len) && (val[off + st] <= ' ')) { 2489 st++; 2490 } 2491 while ((st < len) && (val[off + len - 1] <= ' ')) { 2492 len--; 2493 } 2494 return ((st > 0) || (len < count)) ? substring(st, len) : this; 2495 } 2496 2497 2502 public String toString() { 2503 return this; 2504 } 2505 2506 2513 public char[] toCharArray() { 2514 char result[] = new char[count]; 2515 getChars(0, count, result, 0); 2516 return result; 2517 } 2518 2519 2557 public static String format(String format, Object ... args) { 2558 return new Formatter ().format(format, args).toString(); 2559 } 2560 2561 2601 public static String format(Locale l, String format, Object ... args) { 2602 return new Formatter (l).format(format, args).toString(); 2603 } 2604 2605 2614 public static String valueOf(Object obj) { 2615 return (obj == null) ? "null" : obj.toString(); 2616 } 2617 2618 2628 public static String valueOf(char data[]) { 2629 return new String (data); 2630 } 2631 2632 2653 public static String valueOf(char data[], int offset, int count) { 2654 return new String (data, offset, count); 2655 } 2656 2657 2667 public static String copyValueOf(char data[], int offset, int count) { 2668 return new String (data, offset, count); 2670 } 2671 2672 2680 public static String copyValueOf(char data[]) { 2681 return copyValueOf(data, 0, data.length); 2682 } 2683 2684 2692 public static String valueOf(boolean b) { 2693 return b ? "true" : "false"; 2694 } 2695 2696 2704 public static String valueOf(char c) { 2705 char data[] = {c}; 2706 return new String (0, 1, data); 2707 } 2708 2709 2719 public static String valueOf(int i) { 2720 return Integer.toString(i, 10); 2721 } 2722 2723 2733 public static String valueOf(long l) { 2734 return Long.toString(l, 10); 2735 } 2736 2737 2747 public static String valueOf(float f) { 2748 return Float.toString(f); 2749 } 2750 2751 2761 public static String valueOf(double d) { 2762 return Double.toString(d); 2763 } 2764 2765 2789 public native String intern(); 2790 2791} 2792 | Popular Tags |