233 *
234 * @param original
235 * A {@code String}
236 */
237 @HotSpotIntrinsicCandidate
238 public String(String original) {
239 this.value = original.value;
240 this.coder = original.coder;
241 this.hash = original.hash;
242 }
243
244 /**
245 * Allocates a new {@code String} so that it represents the sequence of
246 * characters currently contained in the character array argument. The
247 * contents of the character array are copied; subsequent modification of
248 * the character array does not affect the newly created string.
249 *
250 * @param value
251 * The initial value of the string
252 */
253 public String(char value[]) {
254 this(value, 0, value.length, null);
255 }
256
257 /**
258 * Allocates a new {@code String} that contains characters from a subarray
259 * of the character array argument. The {@code offset} argument is the
260 * index of the first character of the subarray and the {@code count}
261 * argument specifies the length of the subarray. The contents of the
262 * subarray are copied; subsequent modification of the character array does
263 * not affect the newly created string.
264 *
265 * @param value
266 * Array that is the source of characters
267 *
268 * @param offset
269 * The initial offset
270 *
271 * @param count
272 * The length
273 *
274 * @throws IndexOutOfBoundsException
275 * If {@code offset} is negative, {@code count} is negative, or
276 * {@code offset} is greater than {@code value.length - count}
277 */
278 public String(char value[], int offset, int count) {
279 this(value, offset, count, rangeCheck(value, offset, count));
280 }
281
282 private static Void rangeCheck(char[] value, int offset, int count) {
283 checkBoundsOffCount(offset, count, value.length);
284 return null;
285 }
286
287 /**
288 * Allocates a new {@code String} that contains characters from a subarray
289 * of the <a href="Character.html#unicode">Unicode code point</a> array
290 * argument. The {@code offset} argument is the index of the first code
291 * point of the subarray and the {@code count} argument specifies the
292 * length of the subarray. The contents of the subarray are converted to
293 * {@code char}s; subsequent modification of the {@code int} array does not
294 * affect the newly created string.
295 *
296 * @param codePoints
297 * Array that is the source of Unicode code points
298 *
355 * The top 8 bits of each 16-bit Unicode code unit
356 *
357 * @param offset
358 * The initial offset
359 * @param count
360 * The length
361 *
362 * @throws IndexOutOfBoundsException
363 * If {@code offset} is negative, {@code count} is negative, or
364 * {@code offset} is greater than {@code ascii.length - count}
365 *
366 * @see #String(byte[], int)
367 * @see #String(byte[], int, int, java.lang.String)
368 * @see #String(byte[], int, int, java.nio.charset.Charset)
369 * @see #String(byte[], int, int)
370 * @see #String(byte[], java.lang.String)
371 * @see #String(byte[], java.nio.charset.Charset)
372 * @see #String(byte[])
373 */
374 @Deprecated(since="1.1")
375 public String(byte ascii[], int hibyte, int offset, int count) {
376 checkBoundsOffCount(offset, count, ascii.length);
377 if (count == 0) {
378 this.value = "".value;
379 this.coder = "".coder;
380 return;
381 }
382 if (COMPACT_STRINGS && (byte)hibyte == 0) {
383 this.value = Arrays.copyOfRange(ascii, offset, offset + count);
384 this.coder = LATIN1;
385 } else {
386 hibyte <<= 8;
387 byte[] val = StringUTF16.newBytesFor(count);
388 for (int i = 0; i < count; i++) {
389 StringUTF16.putChar(val, i, hibyte | (ascii[offset++] & 0xff));
390 }
391 this.value = val;
392 this.coder = UTF16;
393 }
394 }
395
407 * @deprecated This method does not properly convert bytes into
408 * characters. As of JDK 1.1, the preferred way to do this is via the
409 * {@code String} constructors that take a {@link
410 * java.nio.charset.Charset}, charset name, or that use the platform's
411 * default charset.
412 *
413 * @param ascii
414 * The bytes to be converted to characters
415 *
416 * @param hibyte
417 * The top 8 bits of each 16-bit Unicode code unit
418 *
419 * @see #String(byte[], int, int, java.lang.String)
420 * @see #String(byte[], int, int, java.nio.charset.Charset)
421 * @see #String(byte[], int, int)
422 * @see #String(byte[], java.lang.String)
423 * @see #String(byte[], java.nio.charset.Charset)
424 * @see #String(byte[])
425 */
426 @Deprecated(since="1.1")
427 public String(byte ascii[], int hibyte) {
428 this(ascii, hibyte, 0, ascii.length);
429 }
430
431 /**
432 * Constructs a new {@code String} by decoding the specified subarray of
433 * bytes using the specified charset. The length of the new {@code String}
434 * is a function of the charset, and hence may not be equal to the length
435 * of the subarray.
436 *
437 * <p> The behavior of this constructor when the given bytes are not valid
438 * in the given charset is unspecified. The {@link
439 * java.nio.charset.CharsetDecoder} class should be used when more control
440 * over the decoding process is required.
441 *
442 * @param bytes
443 * The bytes to be decoded into characters
444 *
445 * @param offset
446 * The index of the first byte to decode
447 *
448 * @param length
449 * The number of bytes to decode
450
451 * @param charsetName
452 * The name of a supported {@linkplain java.nio.charset.Charset
453 * charset}
454 *
455 * @throws UnsupportedEncodingException
456 * If the named charset is not supported
457 *
458 * @throws IndexOutOfBoundsException
459 * If {@code offset} is negative, {@code length} is negative, or
460 * {@code offset} is greater than {@code bytes.length - length}
461 *
462 * @since 1.1
463 */
464 public String(byte bytes[], int offset, int length, String charsetName)
465 throws UnsupportedEncodingException {
466 if (charsetName == null)
467 throw new NullPointerException("charsetName");
468 checkBoundsOffCount(offset, length, bytes.length);
469 StringCoding.Result ret =
470 StringCoding.decode(charsetName, bytes, offset, length);
471 this.value = ret.value;
472 this.coder = ret.coder;
473 }
474
475 /**
476 * Constructs a new {@code String} by decoding the specified subarray of
477 * bytes using the specified {@linkplain java.nio.charset.Charset charset}.
478 * The length of the new {@code String} is a function of the charset, and
479 * hence may not be equal to the length of the subarray.
480 *
481 * <p> This method always replaces malformed-input and unmappable-character
482 * sequences with this charset's default replacement string. The {@link
483 * java.nio.charset.CharsetDecoder} class should be used when more control
484 * over the decoding process is required.
485 *
486 * @param bytes
487 * The bytes to be decoded into characters
488 *
489 * @param offset
490 * The index of the first byte to decode
491 *
492 * @param length
493 * The number of bytes to decode
494 *
495 * @param charset
496 * The {@linkplain java.nio.charset.Charset charset} to be used to
497 * decode the {@code bytes}
498 *
499 * @throws IndexOutOfBoundsException
500 * If {@code offset} is negative, {@code length} is negative, or
501 * {@code offset} is greater than {@code bytes.length - length}
502 *
503 * @since 1.6
504 */
505 public String(byte bytes[], int offset, int length, Charset charset) {
506 if (charset == null)
507 throw new NullPointerException("charset");
508 checkBoundsOffCount(offset, length, bytes.length);
509 StringCoding.Result ret =
510 StringCoding.decode(charset, bytes, offset, length);
511 this.value = ret.value;
512 this.coder = ret.coder;
513 }
514
515 /**
516 * Constructs a new {@code String} by decoding the specified array of bytes
517 * using the specified {@linkplain java.nio.charset.Charset charset}. The
518 * length of the new {@code String} is a function of the charset, and hence
519 * may not be equal to the length of the byte array.
520 *
521 * <p> The behavior of this constructor when the given bytes are not valid
522 * in the given charset is unspecified. The {@link
523 * java.nio.charset.CharsetDecoder} class should be used when more control
524 * over the decoding process is required.
525 *
526 * @param bytes
527 * The bytes to be decoded into characters
528 *
529 * @param charsetName
530 * The name of a supported {@linkplain java.nio.charset.Charset
531 * charset}
532 *
533 * @throws UnsupportedEncodingException
534 * If the named charset is not supported
535 *
536 * @since 1.1
537 */
538 public String(byte bytes[], String charsetName)
539 throws UnsupportedEncodingException {
540 this(bytes, 0, bytes.length, charsetName);
541 }
542
543 /**
544 * Constructs a new {@code String} by decoding the specified array of
545 * bytes using the specified {@linkplain java.nio.charset.Charset charset}.
546 * The length of the new {@code String} is a function of the charset, and
547 * hence may not be equal to the length of the byte array.
548 *
549 * <p> This method always replaces malformed-input and unmappable-character
550 * sequences with this charset's default replacement string. The {@link
551 * java.nio.charset.CharsetDecoder} class should be used when more control
552 * over the decoding process is required.
553 *
554 * @param bytes
555 * The bytes to be decoded into characters
556 *
557 * @param charset
558 * The {@linkplain java.nio.charset.Charset charset} to be used to
559 * decode the {@code bytes}
560 *
561 * @since 1.6
562 */
563 public String(byte bytes[], Charset charset) {
564 this(bytes, 0, bytes.length, charset);
565 }
566
567 /**
568 * Constructs a new {@code String} by decoding the specified subarray of
569 * bytes using the platform's default charset. The length of the new
570 * {@code String} is a function of the charset, and hence may not be equal
571 * to the length of the subarray.
572 *
573 * <p> The behavior of this constructor when the given bytes are not valid
574 * in the default charset is unspecified. The {@link
575 * java.nio.charset.CharsetDecoder} class should be used when more control
576 * over the decoding process is required.
577 *
578 * @param bytes
579 * The bytes to be decoded into characters
580 *
581 * @param offset
582 * The index of the first byte to decode
583 *
584 * @param length
585 * The number of bytes to decode
586 *
587 * @throws IndexOutOfBoundsException
588 * If {@code offset} is negative, {@code length} is negative, or
589 * {@code offset} is greater than {@code bytes.length - length}
590 *
591 * @since 1.1
592 */
593 public String(byte bytes[], int offset, int length) {
594 checkBoundsOffCount(offset, length, bytes.length);
595 StringCoding.Result ret = StringCoding.decode(bytes, offset, length);
596 this.value = ret.value;
597 this.coder = ret.coder;
598 }
599
600 /**
601 * Constructs a new {@code String} by decoding the specified array of bytes
602 * using the platform's default charset. The length of the new {@code
603 * String} is a function of the charset, and hence may not be equal to the
604 * length of the byte array.
605 *
606 * <p> The behavior of this constructor when the given bytes are not valid
607 * in the default charset is unspecified. The {@link
608 * java.nio.charset.CharsetDecoder} class should be used when more control
609 * over the decoding process is required.
610 *
611 * @param bytes
612 * The bytes to be decoded into characters
613 *
836 * <blockquote><pre>
837 * dstBegin + (srcEnd-srcBegin) - 1
838 * </pre></blockquote>
839 *
840 * @param srcBegin index of the first character in the string
841 * to copy.
842 * @param srcEnd index after the last character in the string
843 * to copy.
844 * @param dst the destination array.
845 * @param dstBegin the start offset in the destination array.
846 * @exception IndexOutOfBoundsException If any of the following
847 * is true:
848 * <ul><li>{@code srcBegin} is negative.
849 * <li>{@code srcBegin} is greater than {@code srcEnd}
850 * <li>{@code srcEnd} is greater than the length of this
851 * string
852 * <li>{@code dstBegin} is negative
853 * <li>{@code dstBegin+(srcEnd-srcBegin)} is larger than
854 * {@code dst.length}</ul>
855 */
856 public void getChars(int srcBegin, int srcEnd, char dst[], int dstBegin) {
857 checkBoundsBeginEnd(srcBegin, srcEnd, length());
858 checkBoundsOffCount(dstBegin, srcEnd - srcBegin, dst.length);
859 if (isLatin1()) {
860 StringLatin1.getChars(value, srcBegin, srcEnd, dst, dstBegin);
861 } else {
862 StringUTF16.getChars(value, srcBegin, srcEnd, dst, dstBegin);
863 }
864 }
865
866 /**
867 * Copies characters from this string into the destination byte array. Each
868 * byte receives the 8 low-order bits of the corresponding character. The
869 * eight high-order bits of each character are not copied and do not
870 * participate in the transfer in any way.
871 *
872 * <p> The first character to be copied is at index {@code srcBegin}; the
873 * last character to be copied is at index {@code srcEnd-1}. The total
874 * number of characters to be copied is {@code srcEnd-srcBegin}. The
875 * characters, converted to bytes, are copied into the subarray of {@code
876 * dst} starting at index {@code dstBegin} and ending at index:
890 * Index after the last character in the string to copy
891 *
892 * @param dst
893 * The destination array
894 *
895 * @param dstBegin
896 * The start offset in the destination array
897 *
898 * @throws IndexOutOfBoundsException
899 * If any of the following is true:
900 * <ul>
901 * <li> {@code srcBegin} is negative
902 * <li> {@code srcBegin} is greater than {@code srcEnd}
903 * <li> {@code srcEnd} is greater than the length of this String
904 * <li> {@code dstBegin} is negative
905 * <li> {@code dstBegin+(srcEnd-srcBegin)} is larger than {@code
906 * dst.length}
907 * </ul>
908 */
909 @Deprecated(since="1.1")
910 public void getBytes(int srcBegin, int srcEnd, byte dst[], int dstBegin) {
911 checkBoundsBeginEnd(srcBegin, srcEnd, length());
912 Objects.requireNonNull(dst);
913 checkBoundsOffCount(dstBegin, srcEnd - srcBegin, dst.length);
914 if (isLatin1()) {
915 StringLatin1.getBytes(value, srcBegin, srcEnd, dst, dstBegin);
916 } else {
917 StringUTF16.getBytes(value, srcBegin, srcEnd, dst, dstBegin);
918 }
919 }
920
921 /**
922 * Encodes this {@code String} into a sequence of bytes using the named
923 * charset, storing the result into a new byte array.
924 *
925 * <p> The behavior of this method when this string cannot be encoded in
926 * the given charset is unspecified. The {@link
927 * java.nio.charset.CharsetEncoder} class should be used when more control
928 * over the encoding process is required.
929 *
930 * @param charsetName
1026 * {@link java.text.Collator}.
1027 *
1028 * @param sb
1029 * The {@code StringBuffer} to compare this {@code String} against
1030 *
1031 * @return {@code true} if this {@code String} represents the same
1032 * sequence of characters as the specified {@code StringBuffer},
1033 * {@code false} otherwise
1034 *
1035 * @since 1.4
1036 */
1037 public boolean contentEquals(StringBuffer sb) {
1038 return contentEquals((CharSequence)sb);
1039 }
1040
1041 private boolean nonSyncContentEquals(AbstractStringBuilder sb) {
1042 int len = length();
1043 if (len != sb.length()) {
1044 return false;
1045 }
1046 byte v1[] = value;
1047 byte v2[] = sb.getValue();
1048 if (coder() == sb.getCoder()) {
1049 int n = v1.length;
1050 for (int i = 0; i < n; i++) {
1051 if (v1[i] != v2[i]) {
1052 return false;
1053 }
1054 }
1055 } else {
1056 if (!isLatin1()) { // utf16 str and latin1 abs can never be "equal"
1057 return false;
1058 }
1059 return StringUTF16.contentEquals(v1, v2, len);
1060 }
1061 return true;
1062 }
1063
1064 /**
1065 * Compares this string to the specified {@code CharSequence}. The
1066 * result is {@code true} if and only if this {@code String} represents the
1067 * same sequence of char values as the specified sequence. Note that if the
1178 * </pre></blockquote>
1179 * If there is no index position at which they differ, then the shorter
1180 * string lexicographically precedes the longer string. In this case,
1181 * {@code compareTo} returns the difference of the lengths of the
1182 * strings -- that is, the value:
1183 * <blockquote><pre>
1184 * this.length()-anotherString.length()
1185 * </pre></blockquote>
1186 *
1187 * <p>For finer-grained String comparison, refer to
1188 * {@link java.text.Collator}.
1189 *
1190 * @param anotherString the {@code String} to be compared.
1191 * @return the value {@code 0} if the argument string is equal to
1192 * this string; a value less than {@code 0} if this string
1193 * is lexicographically less than the string argument; and a
1194 * value greater than {@code 0} if this string is
1195 * lexicographically greater than the string argument.
1196 */
1197 public int compareTo(String anotherString) {
1198 byte v1[] = value;
1199 byte v2[] = anotherString.value;
1200 if (coder() == anotherString.coder()) {
1201 return isLatin1() ? StringLatin1.compareTo(v1, v2)
1202 : StringUTF16.compareTo(v1, v2);
1203 }
1204 return isLatin1() ? StringLatin1.compareToUTF16(v1, v2)
1205 : StringUTF16.compareToLatin1(v1, v2);
1206 }
1207
1208 /**
1209 * A Comparator that orders {@code String} objects as by
1210 * {@code compareToIgnoreCase}. This comparator is serializable.
1211 * <p>
1212 * Note that this Comparator does <em>not</em> take locale into account,
1213 * and will result in an unsatisfactory ordering for certain locales.
1214 * The {@link java.text.Collator} class provides locale-sensitive comparison.
1215 *
1216 * @see java.text.Collator
1217 * @since 1.2
1218 */
1219 public static final Comparator<String> CASE_INSENSITIVE_ORDER
1220 = new CaseInsensitiveComparator();
1221 private static class CaseInsensitiveComparator
1222 implements Comparator<String>, java.io.Serializable {
1223 // use serialVersionUID from JDK 1.2.2 for interoperability
1224 private static final long serialVersionUID = 8575799808933029326L;
1225
1226 public int compare(String s1, String s2) {
1227 byte v1[] = s1.value;
1228 byte v2[] = s2.value;
1229 if (s1.coder() == s2.coder()) {
1230 return s1.isLatin1() ? StringLatin1.compareToCI(v1, v2)
1231 : StringUTF16.compareToCI(v1, v2);
1232 }
1233 return s1.isLatin1() ? StringLatin1.compareToCI_UTF16(v1, v2)
1234 : StringUTF16.compareToCI_Latin1(v1, v2);
1235 }
1236
1237 /** Replaces the de-serialized object. */
1238 private Object readResolve() { return CASE_INSENSITIVE_ORDER; }
1239 }
1240
1241 /**
1242 * Compares two strings lexicographically, ignoring case
1243 * differences. This method returns an integer whose sign is that of
1244 * calling {@code compareTo} with normalized versions of the strings
1245 * where case differences have been eliminated by calling
1246 * {@code Character.toLowerCase(Character.toUpperCase(character))} on
1247 * each character.
1248 * <p>
1280 * argument.
1281 * <li>There is some nonnegative integer <i>k</i> less than {@code len}
1282 * such that:
1283 * {@code this.charAt(toffset + }<i>k</i>{@code ) != other.charAt(ooffset + }
1284 * <i>k</i>{@code )}
1285 * </ul>
1286 *
1287 * <p>Note that this method does <em>not</em> take locale into account. The
1288 * {@link java.text.Collator} class provides locale-sensitive comparison.
1289 *
1290 * @param toffset the starting offset of the subregion in this string.
1291 * @param other the string argument.
1292 * @param ooffset the starting offset of the subregion in the string
1293 * argument.
1294 * @param len the number of characters to compare.
1295 * @return {@code true} if the specified subregion of this string
1296 * exactly matches the specified subregion of the string argument;
1297 * {@code false} otherwise.
1298 */
1299 public boolean regionMatches(int toffset, String other, int ooffset, int len) {
1300 byte tv[] = value;
1301 byte ov[] = other.value;
1302 // Note: toffset, ooffset, or len might be near -1>>>1.
1303 if ((ooffset < 0) || (toffset < 0) ||
1304 (toffset > (long)length() - len) ||
1305 (ooffset > (long)other.length() - len)) {
1306 return false;
1307 }
1308 if (coder() == other.coder()) {
1309 if (!isLatin1() && (len > 0)) {
1310 toffset = toffset << 1;
1311 ooffset = ooffset << 1;
1312 len = len << 1;
1313 }
1314 while (len-- > 0) {
1315 if (tv[toffset++] != ov[ooffset++]) {
1316 return false;
1317 }
1318 }
1319 } else {
1320 if (coder() == LATIN1) {
1321 while (len-- > 0) {
1380 * @param ooffset the starting offset of the subregion in the string
1381 * argument.
1382 * @param len the number of characters to compare.
1383 * @return {@code true} if the specified subregion of this string
1384 * matches the specified subregion of the string argument;
1385 * {@code false} otherwise. Whether the matching is exact
1386 * or case insensitive depends on the {@code ignoreCase}
1387 * argument.
1388 */
1389 public boolean regionMatches(boolean ignoreCase, int toffset,
1390 String other, int ooffset, int len) {
1391 if (!ignoreCase) {
1392 return regionMatches(toffset, other, ooffset, len);
1393 }
1394 // Note: toffset, ooffset, or len might be near -1>>>1.
1395 if ((ooffset < 0) || (toffset < 0)
1396 || (toffset > (long)length() - len)
1397 || (ooffset > (long)other.length() - len)) {
1398 return false;
1399 }
1400 byte tv[] = value;
1401 byte ov[] = other.value;
1402 if (coder() == other.coder()) {
1403 return isLatin1()
1404 ? StringLatin1.regionMatchesCI(tv, toffset, ov, ooffset, len)
1405 : StringUTF16.regionMatchesCI(tv, toffset, ov, ooffset, len);
1406 }
1407 return isLatin1()
1408 ? StringLatin1.regionMatchesCI_UTF16(tv, toffset, ov, ooffset, len)
1409 : StringUTF16.regionMatchesCI_Latin1(tv, toffset, ov, ooffset, len);
1410 }
1411
1412 /**
1413 * Tests if the substring of this string beginning at the
1414 * specified index starts with the specified prefix.
1415 *
1416 * @param prefix the prefix.
1417 * @param toffset where to begin looking in this string.
1418 * @return {@code true} if the character sequence represented by the
1419 * argument is a prefix of the substring of this object starting
1420 * at index {@code toffset}; {@code false} otherwise.
1421 * The result is {@code false} if {@code toffset} is
1422 * negative or greater than the length of this
1423 * {@code String} object; otherwise the result is the same
1424 * as the result of the expression
1425 * <pre>
1426 * this.substring(toffset).startsWith(prefix)
1427 * </pre>
1428 */
1429 public boolean startsWith(String prefix, int toffset) {
1430 // Note: toffset might be near -1>>>1.
1431 if (toffset < 0 || toffset > length() - prefix.length()) {
1432 return false;
1433 }
1434 byte ta[] = value;
1435 byte pa[] = prefix.value;
1436 int po = 0;
1437 int pc = pa.length;
1438 if (coder() == prefix.coder()) {
1439 int to = isLatin1() ? toffset : toffset << 1;
1440 while (po < pc) {
1441 if (ta[to++] != pa[po++]) {
1442 return false;
1443 }
1444 }
1445 } else {
1446 if (isLatin1()) { // && pcoder == UTF16
1447 return false;
1448 }
1449 // coder == UTF16 && pcoder == LATIN1)
1450 while (po < pc) {
1451 if (StringUTF16.getChar(ta, toffset++) != (pa[po++] & 0xff)) {
1452 return false;
1453 }
1454 }
1455 }
3117 * @param obj an {@code Object}.
3118 * @return if the argument is {@code null}, then a string equal to
3119 * {@code "null"}; otherwise, the value of
3120 * {@code obj.toString()} is returned.
3121 * @see java.lang.Object#toString()
3122 */
3123 public static String valueOf(Object obj) {
3124 return (obj == null) ? "null" : obj.toString();
3125 }
3126
3127 /**
3128 * Returns the string representation of the {@code char} array
3129 * argument. The contents of the character array are copied; subsequent
3130 * modification of the character array does not affect the returned
3131 * string.
3132 *
3133 * @param data the character array.
3134 * @return a {@code String} that contains the characters of the
3135 * character array.
3136 */
3137 public static String valueOf(char data[]) {
3138 return new String(data);
3139 }
3140
3141 /**
3142 * Returns the string representation of a specific subarray of the
3143 * {@code char} array argument.
3144 * <p>
3145 * The {@code offset} argument is the index of the first
3146 * character of the subarray. The {@code count} argument
3147 * specifies the length of the subarray. The contents of the subarray
3148 * are copied; subsequent modification of the character array does not
3149 * affect the returned string.
3150 *
3151 * @param data the character array.
3152 * @param offset initial offset of the subarray.
3153 * @param count length of the subarray.
3154 * @return a {@code String} that contains the characters of the
3155 * specified subarray of the character array.
3156 * @exception IndexOutOfBoundsException if {@code offset} is
3157 * negative, or {@code count} is negative, or
3158 * {@code offset+count} is larger than
3159 * {@code data.length}.
3160 */
3161 public static String valueOf(char data[], int offset, int count) {
3162 return new String(data, offset, count);
3163 }
3164
3165 /**
3166 * Equivalent to {@link #valueOf(char[], int, int)}.
3167 *
3168 * @param data the character array.
3169 * @param offset initial offset of the subarray.
3170 * @param count length of the subarray.
3171 * @return a {@code String} that contains the characters of the
3172 * specified subarray of the character array.
3173 * @exception IndexOutOfBoundsException if {@code offset} is
3174 * negative, or {@code count} is negative, or
3175 * {@code offset+count} is larger than
3176 * {@code data.length}.
3177 */
3178 public static String copyValueOf(char data[], int offset, int count) {
3179 return new String(data, offset, count);
3180 }
3181
3182 /**
3183 * Equivalent to {@link #valueOf(char[])}.
3184 *
3185 * @param data the character array.
3186 * @return a {@code String} that contains the characters of the
3187 * character array.
3188 */
3189 public static String copyValueOf(char data[]) {
3190 return new String(data);
3191 }
3192
3193 /**
3194 * Returns the string representation of the {@code boolean} argument.
3195 *
3196 * @param b a {@code boolean}.
3197 * @return if the argument is {@code true}, a string equal to
3198 * {@code "true"} is returned; otherwise, a string equal to
3199 * {@code "false"} is returned.
3200 */
3201 public static String valueOf(boolean b) {
3202 return b ? "true" : "false";
3203 }
3204
3205 /**
3206 * Returns the string representation of the {@code char}
3207 * argument.
3208 *
3209 * @param c a {@code char}.
3343 int copied = len;
3344 for (; copied < limit - copied; copied <<= 1) {
3345 System.arraycopy(multiple, 0, multiple, copied, copied);
3346 }
3347 System.arraycopy(multiple, 0, multiple, copied, limit - copied);
3348 return new String(multiple, coder);
3349 }
3350
3351 ////////////////////////////////////////////////////////////////
3352
3353 /**
3354 * Copy character bytes from this string into dst starting at dstBegin.
3355 * This method doesn't perform any range checking.
3356 *
3357 * Invoker guarantees: dst is in UTF16 (inflate itself for asb), if two
3358 * coders are different, and dst is big enough (range check)
3359 *
3360 * @param dstBegin the char index, not offset of byte[]
3361 * @param coder the coder of dst[]
3362 */
3363 void getBytes(byte dst[], int dstBegin, byte coder) {
3364 if (coder() == coder) {
3365 System.arraycopy(value, 0, dst, dstBegin << coder, value.length);
3366 } else { // this.coder == LATIN && coder == UTF16
3367 StringLatin1.inflate(value, 0, dst, dstBegin, value.length);
3368 }
3369 }
3370
3371 /*
3372 * Package private constructor. Trailing Void argument is there for
3373 * disambiguating it against other (public) constructors.
3374 *
3375 * Stores the char[] value into a byte[] that each byte represents
3376 * the8 low-order bits of the corresponding character, if the char[]
3377 * contains only latin1 character. Or a byte[] that stores all
3378 * characters in their byte sequences defined by the {@code StringUTF16}.
3379 */
3380 String(char[] value, int off, int len, Void sig) {
3381 if (len == 0) {
3382 this.value = "".value;
3383 this.coder = "".coder;
|
233 *
234 * @param original
235 * A {@code String}
236 */
237 @HotSpotIntrinsicCandidate
238 public String(String original) {
239 this.value = original.value;
240 this.coder = original.coder;
241 this.hash = original.hash;
242 }
243
244 /**
245 * Allocates a new {@code String} so that it represents the sequence of
246 * characters currently contained in the character array argument. The
247 * contents of the character array are copied; subsequent modification of
248 * the character array does not affect the newly created string.
249 *
250 * @param value
251 * The initial value of the string
252 */
253 public String(char[] value) {
254 this(value, 0, value.length, null);
255 }
256
257 /**
258 * Allocates a new {@code String} that contains characters from a subarray
259 * of the character array argument. The {@code offset} argument is the
260 * index of the first character of the subarray and the {@code count}
261 * argument specifies the length of the subarray. The contents of the
262 * subarray are copied; subsequent modification of the character array does
263 * not affect the newly created string.
264 *
265 * @param value
266 * Array that is the source of characters
267 *
268 * @param offset
269 * The initial offset
270 *
271 * @param count
272 * The length
273 *
274 * @throws IndexOutOfBoundsException
275 * If {@code offset} is negative, {@code count} is negative, or
276 * {@code offset} is greater than {@code value.length - count}
277 */
278 public String(char[] value, int offset, int count) {
279 this(value, offset, count, rangeCheck(value, offset, count));
280 }
281
282 private static Void rangeCheck(char[] value, int offset, int count) {
283 checkBoundsOffCount(offset, count, value.length);
284 return null;
285 }
286
287 /**
288 * Allocates a new {@code String} that contains characters from a subarray
289 * of the <a href="Character.html#unicode">Unicode code point</a> array
290 * argument. The {@code offset} argument is the index of the first code
291 * point of the subarray and the {@code count} argument specifies the
292 * length of the subarray. The contents of the subarray are converted to
293 * {@code char}s; subsequent modification of the {@code int} array does not
294 * affect the newly created string.
295 *
296 * @param codePoints
297 * Array that is the source of Unicode code points
298 *
355 * The top 8 bits of each 16-bit Unicode code unit
356 *
357 * @param offset
358 * The initial offset
359 * @param count
360 * The length
361 *
362 * @throws IndexOutOfBoundsException
363 * If {@code offset} is negative, {@code count} is negative, or
364 * {@code offset} is greater than {@code ascii.length - count}
365 *
366 * @see #String(byte[], int)
367 * @see #String(byte[], int, int, java.lang.String)
368 * @see #String(byte[], int, int, java.nio.charset.Charset)
369 * @see #String(byte[], int, int)
370 * @see #String(byte[], java.lang.String)
371 * @see #String(byte[], java.nio.charset.Charset)
372 * @see #String(byte[])
373 */
374 @Deprecated(since="1.1")
375 public String(byte[] ascii, int hibyte, int offset, int count) {
376 checkBoundsOffCount(offset, count, ascii.length);
377 if (count == 0) {
378 this.value = "".value;
379 this.coder = "".coder;
380 return;
381 }
382 if (COMPACT_STRINGS && (byte)hibyte == 0) {
383 this.value = Arrays.copyOfRange(ascii, offset, offset + count);
384 this.coder = LATIN1;
385 } else {
386 hibyte <<= 8;
387 byte[] val = StringUTF16.newBytesFor(count);
388 for (int i = 0; i < count; i++) {
389 StringUTF16.putChar(val, i, hibyte | (ascii[offset++] & 0xff));
390 }
391 this.value = val;
392 this.coder = UTF16;
393 }
394 }
395
407 * @deprecated This method does not properly convert bytes into
408 * characters. As of JDK 1.1, the preferred way to do this is via the
409 * {@code String} constructors that take a {@link
410 * java.nio.charset.Charset}, charset name, or that use the platform's
411 * default charset.
412 *
413 * @param ascii
414 * The bytes to be converted to characters
415 *
416 * @param hibyte
417 * The top 8 bits of each 16-bit Unicode code unit
418 *
419 * @see #String(byte[], int, int, java.lang.String)
420 * @see #String(byte[], int, int, java.nio.charset.Charset)
421 * @see #String(byte[], int, int)
422 * @see #String(byte[], java.lang.String)
423 * @see #String(byte[], java.nio.charset.Charset)
424 * @see #String(byte[])
425 */
426 @Deprecated(since="1.1")
427 public String(byte[] ascii, int hibyte) {
428 this(ascii, hibyte, 0, ascii.length);
429 }
430
431 /**
432 * Constructs a new {@code String} by decoding the specified subarray of
433 * bytes using the specified charset. The length of the new {@code String}
434 * is a function of the charset, and hence may not be equal to the length
435 * of the subarray.
436 *
437 * <p> The behavior of this constructor when the given bytes are not valid
438 * in the given charset is unspecified. The {@link
439 * java.nio.charset.CharsetDecoder} class should be used when more control
440 * over the decoding process is required.
441 *
442 * @param bytes
443 * The bytes to be decoded into characters
444 *
445 * @param offset
446 * The index of the first byte to decode
447 *
448 * @param length
449 * The number of bytes to decode
450
451 * @param charsetName
452 * The name of a supported {@linkplain java.nio.charset.Charset
453 * charset}
454 *
455 * @throws UnsupportedEncodingException
456 * If the named charset is not supported
457 *
458 * @throws IndexOutOfBoundsException
459 * If {@code offset} is negative, {@code length} is negative, or
460 * {@code offset} is greater than {@code bytes.length - length}
461 *
462 * @since 1.1
463 */
464 public String(byte[] bytes, int offset, int length, String charsetName)
465 throws UnsupportedEncodingException {
466 if (charsetName == null)
467 throw new NullPointerException("charsetName");
468 checkBoundsOffCount(offset, length, bytes.length);
469 StringCoding.Result ret =
470 StringCoding.decode(charsetName, bytes, offset, length);
471 this.value = ret.value;
472 this.coder = ret.coder;
473 }
474
475 /**
476 * Constructs a new {@code String} by decoding the specified subarray of
477 * bytes using the specified {@linkplain java.nio.charset.Charset charset}.
478 * The length of the new {@code String} is a function of the charset, and
479 * hence may not be equal to the length of the subarray.
480 *
481 * <p> This method always replaces malformed-input and unmappable-character
482 * sequences with this charset's default replacement string. The {@link
483 * java.nio.charset.CharsetDecoder} class should be used when more control
484 * over the decoding process is required.
485 *
486 * @param bytes
487 * The bytes to be decoded into characters
488 *
489 * @param offset
490 * The index of the first byte to decode
491 *
492 * @param length
493 * The number of bytes to decode
494 *
495 * @param charset
496 * The {@linkplain java.nio.charset.Charset charset} to be used to
497 * decode the {@code bytes}
498 *
499 * @throws IndexOutOfBoundsException
500 * If {@code offset} is negative, {@code length} is negative, or
501 * {@code offset} is greater than {@code bytes.length - length}
502 *
503 * @since 1.6
504 */
505 public String(byte[] bytes, int offset, int length, Charset charset) {
506 if (charset == null)
507 throw new NullPointerException("charset");
508 checkBoundsOffCount(offset, length, bytes.length);
509 StringCoding.Result ret =
510 StringCoding.decode(charset, bytes, offset, length);
511 this.value = ret.value;
512 this.coder = ret.coder;
513 }
514
515 /**
516 * Constructs a new {@code String} by decoding the specified array of bytes
517 * using the specified {@linkplain java.nio.charset.Charset charset}. The
518 * length of the new {@code String} is a function of the charset, and hence
519 * may not be equal to the length of the byte array.
520 *
521 * <p> The behavior of this constructor when the given bytes are not valid
522 * in the given charset is unspecified. The {@link
523 * java.nio.charset.CharsetDecoder} class should be used when more control
524 * over the decoding process is required.
525 *
526 * @param bytes
527 * The bytes to be decoded into characters
528 *
529 * @param charsetName
530 * The name of a supported {@linkplain java.nio.charset.Charset
531 * charset}
532 *
533 * @throws UnsupportedEncodingException
534 * If the named charset is not supported
535 *
536 * @since 1.1
537 */
538 public String(byte[] bytes, String charsetName)
539 throws UnsupportedEncodingException {
540 this(bytes, 0, bytes.length, charsetName);
541 }
542
543 /**
544 * Constructs a new {@code String} by decoding the specified array of
545 * bytes using the specified {@linkplain java.nio.charset.Charset charset}.
546 * The length of the new {@code String} is a function of the charset, and
547 * hence may not be equal to the length of the byte array.
548 *
549 * <p> This method always replaces malformed-input and unmappable-character
550 * sequences with this charset's default replacement string. The {@link
551 * java.nio.charset.CharsetDecoder} class should be used when more control
552 * over the decoding process is required.
553 *
554 * @param bytes
555 * The bytes to be decoded into characters
556 *
557 * @param charset
558 * The {@linkplain java.nio.charset.Charset charset} to be used to
559 * decode the {@code bytes}
560 *
561 * @since 1.6
562 */
563 public String(byte[] bytes, Charset charset) {
564 this(bytes, 0, bytes.length, charset);
565 }
566
567 /**
568 * Constructs a new {@code String} by decoding the specified subarray of
569 * bytes using the platform's default charset. The length of the new
570 * {@code String} is a function of the charset, and hence may not be equal
571 * to the length of the subarray.
572 *
573 * <p> The behavior of this constructor when the given bytes are not valid
574 * in the default charset is unspecified. The {@link
575 * java.nio.charset.CharsetDecoder} class should be used when more control
576 * over the decoding process is required.
577 *
578 * @param bytes
579 * The bytes to be decoded into characters
580 *
581 * @param offset
582 * The index of the first byte to decode
583 *
584 * @param length
585 * The number of bytes to decode
586 *
587 * @throws IndexOutOfBoundsException
588 * If {@code offset} is negative, {@code length} is negative, or
589 * {@code offset} is greater than {@code bytes.length - length}
590 *
591 * @since 1.1
592 */
593 public String(byte[] bytes, int offset, int length) {
594 checkBoundsOffCount(offset, length, bytes.length);
595 StringCoding.Result ret = StringCoding.decode(bytes, offset, length);
596 this.value = ret.value;
597 this.coder = ret.coder;
598 }
599
600 /**
601 * Constructs a new {@code String} by decoding the specified array of bytes
602 * using the platform's default charset. The length of the new {@code
603 * String} is a function of the charset, and hence may not be equal to the
604 * length of the byte array.
605 *
606 * <p> The behavior of this constructor when the given bytes are not valid
607 * in the default charset is unspecified. The {@link
608 * java.nio.charset.CharsetDecoder} class should be used when more control
609 * over the decoding process is required.
610 *
611 * @param bytes
612 * The bytes to be decoded into characters
613 *
836 * <blockquote><pre>
837 * dstBegin + (srcEnd-srcBegin) - 1
838 * </pre></blockquote>
839 *
840 * @param srcBegin index of the first character in the string
841 * to copy.
842 * @param srcEnd index after the last character in the string
843 * to copy.
844 * @param dst the destination array.
845 * @param dstBegin the start offset in the destination array.
846 * @exception IndexOutOfBoundsException If any of the following
847 * is true:
848 * <ul><li>{@code srcBegin} is negative.
849 * <li>{@code srcBegin} is greater than {@code srcEnd}
850 * <li>{@code srcEnd} is greater than the length of this
851 * string
852 * <li>{@code dstBegin} is negative
853 * <li>{@code dstBegin+(srcEnd-srcBegin)} is larger than
854 * {@code dst.length}</ul>
855 */
856 public void getChars(int srcBegin, int srcEnd, char[] dst, int dstBegin) {
857 checkBoundsBeginEnd(srcBegin, srcEnd, length());
858 checkBoundsOffCount(dstBegin, srcEnd - srcBegin, dst.length);
859 if (isLatin1()) {
860 StringLatin1.getChars(value, srcBegin, srcEnd, dst, dstBegin);
861 } else {
862 StringUTF16.getChars(value, srcBegin, srcEnd, dst, dstBegin);
863 }
864 }
865
866 /**
867 * Copies characters from this string into the destination byte array. Each
868 * byte receives the 8 low-order bits of the corresponding character. The
869 * eight high-order bits of each character are not copied and do not
870 * participate in the transfer in any way.
871 *
872 * <p> The first character to be copied is at index {@code srcBegin}; the
873 * last character to be copied is at index {@code srcEnd-1}. The total
874 * number of characters to be copied is {@code srcEnd-srcBegin}. The
875 * characters, converted to bytes, are copied into the subarray of {@code
876 * dst} starting at index {@code dstBegin} and ending at index:
890 * Index after the last character in the string to copy
891 *
892 * @param dst
893 * The destination array
894 *
895 * @param dstBegin
896 * The start offset in the destination array
897 *
898 * @throws IndexOutOfBoundsException
899 * If any of the following is true:
900 * <ul>
901 * <li> {@code srcBegin} is negative
902 * <li> {@code srcBegin} is greater than {@code srcEnd}
903 * <li> {@code srcEnd} is greater than the length of this String
904 * <li> {@code dstBegin} is negative
905 * <li> {@code dstBegin+(srcEnd-srcBegin)} is larger than {@code
906 * dst.length}
907 * </ul>
908 */
909 @Deprecated(since="1.1")
910 public void getBytes(int srcBegin, int srcEnd, byte[] dst, int dstBegin) {
911 checkBoundsBeginEnd(srcBegin, srcEnd, length());
912 Objects.requireNonNull(dst);
913 checkBoundsOffCount(dstBegin, srcEnd - srcBegin, dst.length);
914 if (isLatin1()) {
915 StringLatin1.getBytes(value, srcBegin, srcEnd, dst, dstBegin);
916 } else {
917 StringUTF16.getBytes(value, srcBegin, srcEnd, dst, dstBegin);
918 }
919 }
920
921 /**
922 * Encodes this {@code String} into a sequence of bytes using the named
923 * charset, storing the result into a new byte array.
924 *
925 * <p> The behavior of this method when this string cannot be encoded in
926 * the given charset is unspecified. The {@link
927 * java.nio.charset.CharsetEncoder} class should be used when more control
928 * over the encoding process is required.
929 *
930 * @param charsetName
1026 * {@link java.text.Collator}.
1027 *
1028 * @param sb
1029 * The {@code StringBuffer} to compare this {@code String} against
1030 *
1031 * @return {@code true} if this {@code String} represents the same
1032 * sequence of characters as the specified {@code StringBuffer},
1033 * {@code false} otherwise
1034 *
1035 * @since 1.4
1036 */
1037 public boolean contentEquals(StringBuffer sb) {
1038 return contentEquals((CharSequence)sb);
1039 }
1040
1041 private boolean nonSyncContentEquals(AbstractStringBuilder sb) {
1042 int len = length();
1043 if (len != sb.length()) {
1044 return false;
1045 }
1046 byte[] v1 = value;
1047 byte[] v2 = sb.getValue();
1048 if (coder() == sb.getCoder()) {
1049 int n = v1.length;
1050 for (int i = 0; i < n; i++) {
1051 if (v1[i] != v2[i]) {
1052 return false;
1053 }
1054 }
1055 } else {
1056 if (!isLatin1()) { // utf16 str and latin1 abs can never be "equal"
1057 return false;
1058 }
1059 return StringUTF16.contentEquals(v1, v2, len);
1060 }
1061 return true;
1062 }
1063
1064 /**
1065 * Compares this string to the specified {@code CharSequence}. The
1066 * result is {@code true} if and only if this {@code String} represents the
1067 * same sequence of char values as the specified sequence. Note that if the
1178 * </pre></blockquote>
1179 * If there is no index position at which they differ, then the shorter
1180 * string lexicographically precedes the longer string. In this case,
1181 * {@code compareTo} returns the difference of the lengths of the
1182 * strings -- that is, the value:
1183 * <blockquote><pre>
1184 * this.length()-anotherString.length()
1185 * </pre></blockquote>
1186 *
1187 * <p>For finer-grained String comparison, refer to
1188 * {@link java.text.Collator}.
1189 *
1190 * @param anotherString the {@code String} to be compared.
1191 * @return the value {@code 0} if the argument string is equal to
1192 * this string; a value less than {@code 0} if this string
1193 * is lexicographically less than the string argument; and a
1194 * value greater than {@code 0} if this string is
1195 * lexicographically greater than the string argument.
1196 */
1197 public int compareTo(String anotherString) {
1198 byte[] v1 = value;
1199 byte[] v2 = anotherString.value;
1200 if (coder() == anotherString.coder()) {
1201 return isLatin1() ? StringLatin1.compareTo(v1, v2)
1202 : StringUTF16.compareTo(v1, v2);
1203 }
1204 return isLatin1() ? StringLatin1.compareToUTF16(v1, v2)
1205 : StringUTF16.compareToLatin1(v1, v2);
1206 }
1207
1208 /**
1209 * A Comparator that orders {@code String} objects as by
1210 * {@code compareToIgnoreCase}. This comparator is serializable.
1211 * <p>
1212 * Note that this Comparator does <em>not</em> take locale into account,
1213 * and will result in an unsatisfactory ordering for certain locales.
1214 * The {@link java.text.Collator} class provides locale-sensitive comparison.
1215 *
1216 * @see java.text.Collator
1217 * @since 1.2
1218 */
1219 public static final Comparator<String> CASE_INSENSITIVE_ORDER
1220 = new CaseInsensitiveComparator();
1221 private static class CaseInsensitiveComparator
1222 implements Comparator<String>, java.io.Serializable {
1223 // use serialVersionUID from JDK 1.2.2 for interoperability
1224 private static final long serialVersionUID = 8575799808933029326L;
1225
1226 public int compare(String s1, String s2) {
1227 byte[] v1 = s1.value;
1228 byte[] v2 = s2.value;
1229 if (s1.coder() == s2.coder()) {
1230 return s1.isLatin1() ? StringLatin1.compareToCI(v1, v2)
1231 : StringUTF16.compareToCI(v1, v2);
1232 }
1233 return s1.isLatin1() ? StringLatin1.compareToCI_UTF16(v1, v2)
1234 : StringUTF16.compareToCI_Latin1(v1, v2);
1235 }
1236
1237 /** Replaces the de-serialized object. */
1238 private Object readResolve() { return CASE_INSENSITIVE_ORDER; }
1239 }
1240
1241 /**
1242 * Compares two strings lexicographically, ignoring case
1243 * differences. This method returns an integer whose sign is that of
1244 * calling {@code compareTo} with normalized versions of the strings
1245 * where case differences have been eliminated by calling
1246 * {@code Character.toLowerCase(Character.toUpperCase(character))} on
1247 * each character.
1248 * <p>
1280 * argument.
1281 * <li>There is some nonnegative integer <i>k</i> less than {@code len}
1282 * such that:
1283 * {@code this.charAt(toffset + }<i>k</i>{@code ) != other.charAt(ooffset + }
1284 * <i>k</i>{@code )}
1285 * </ul>
1286 *
1287 * <p>Note that this method does <em>not</em> take locale into account. The
1288 * {@link java.text.Collator} class provides locale-sensitive comparison.
1289 *
1290 * @param toffset the starting offset of the subregion in this string.
1291 * @param other the string argument.
1292 * @param ooffset the starting offset of the subregion in the string
1293 * argument.
1294 * @param len the number of characters to compare.
1295 * @return {@code true} if the specified subregion of this string
1296 * exactly matches the specified subregion of the string argument;
1297 * {@code false} otherwise.
1298 */
1299 public boolean regionMatches(int toffset, String other, int ooffset, int len) {
1300 byte[] tv = value;
1301 byte[] ov = other.value;
1302 // Note: toffset, ooffset, or len might be near -1>>>1.
1303 if ((ooffset < 0) || (toffset < 0) ||
1304 (toffset > (long)length() - len) ||
1305 (ooffset > (long)other.length() - len)) {
1306 return false;
1307 }
1308 if (coder() == other.coder()) {
1309 if (!isLatin1() && (len > 0)) {
1310 toffset = toffset << 1;
1311 ooffset = ooffset << 1;
1312 len = len << 1;
1313 }
1314 while (len-- > 0) {
1315 if (tv[toffset++] != ov[ooffset++]) {
1316 return false;
1317 }
1318 }
1319 } else {
1320 if (coder() == LATIN1) {
1321 while (len-- > 0) {
1380 * @param ooffset the starting offset of the subregion in the string
1381 * argument.
1382 * @param len the number of characters to compare.
1383 * @return {@code true} if the specified subregion of this string
1384 * matches the specified subregion of the string argument;
1385 * {@code false} otherwise. Whether the matching is exact
1386 * or case insensitive depends on the {@code ignoreCase}
1387 * argument.
1388 */
1389 public boolean regionMatches(boolean ignoreCase, int toffset,
1390 String other, int ooffset, int len) {
1391 if (!ignoreCase) {
1392 return regionMatches(toffset, other, ooffset, len);
1393 }
1394 // Note: toffset, ooffset, or len might be near -1>>>1.
1395 if ((ooffset < 0) || (toffset < 0)
1396 || (toffset > (long)length() - len)
1397 || (ooffset > (long)other.length() - len)) {
1398 return false;
1399 }
1400 byte[] tv = value;
1401 byte[] ov = other.value;
1402 if (coder() == other.coder()) {
1403 return isLatin1()
1404 ? StringLatin1.regionMatchesCI(tv, toffset, ov, ooffset, len)
1405 : StringUTF16.regionMatchesCI(tv, toffset, ov, ooffset, len);
1406 }
1407 return isLatin1()
1408 ? StringLatin1.regionMatchesCI_UTF16(tv, toffset, ov, ooffset, len)
1409 : StringUTF16.regionMatchesCI_Latin1(tv, toffset, ov, ooffset, len);
1410 }
1411
1412 /**
1413 * Tests if the substring of this string beginning at the
1414 * specified index starts with the specified prefix.
1415 *
1416 * @param prefix the prefix.
1417 * @param toffset where to begin looking in this string.
1418 * @return {@code true} if the character sequence represented by the
1419 * argument is a prefix of the substring of this object starting
1420 * at index {@code toffset}; {@code false} otherwise.
1421 * The result is {@code false} if {@code toffset} is
1422 * negative or greater than the length of this
1423 * {@code String} object; otherwise the result is the same
1424 * as the result of the expression
1425 * <pre>
1426 * this.substring(toffset).startsWith(prefix)
1427 * </pre>
1428 */
1429 public boolean startsWith(String prefix, int toffset) {
1430 // Note: toffset might be near -1>>>1.
1431 if (toffset < 0 || toffset > length() - prefix.length()) {
1432 return false;
1433 }
1434 byte[] ta = value;
1435 byte[] pa = prefix.value;
1436 int po = 0;
1437 int pc = pa.length;
1438 if (coder() == prefix.coder()) {
1439 int to = isLatin1() ? toffset : toffset << 1;
1440 while (po < pc) {
1441 if (ta[to++] != pa[po++]) {
1442 return false;
1443 }
1444 }
1445 } else {
1446 if (isLatin1()) { // && pcoder == UTF16
1447 return false;
1448 }
1449 // coder == UTF16 && pcoder == LATIN1)
1450 while (po < pc) {
1451 if (StringUTF16.getChar(ta, toffset++) != (pa[po++] & 0xff)) {
1452 return false;
1453 }
1454 }
1455 }
3117 * @param obj an {@code Object}.
3118 * @return if the argument is {@code null}, then a string equal to
3119 * {@code "null"}; otherwise, the value of
3120 * {@code obj.toString()} is returned.
3121 * @see java.lang.Object#toString()
3122 */
3123 public static String valueOf(Object obj) {
3124 return (obj == null) ? "null" : obj.toString();
3125 }
3126
3127 /**
3128 * Returns the string representation of the {@code char} array
3129 * argument. The contents of the character array are copied; subsequent
3130 * modification of the character array does not affect the returned
3131 * string.
3132 *
3133 * @param data the character array.
3134 * @return a {@code String} that contains the characters of the
3135 * character array.
3136 */
3137 public static String valueOf(char[] data) {
3138 return new String(data);
3139 }
3140
3141 /**
3142 * Returns the string representation of a specific subarray of the
3143 * {@code char} array argument.
3144 * <p>
3145 * The {@code offset} argument is the index of the first
3146 * character of the subarray. The {@code count} argument
3147 * specifies the length of the subarray. The contents of the subarray
3148 * are copied; subsequent modification of the character array does not
3149 * affect the returned string.
3150 *
3151 * @param data the character array.
3152 * @param offset initial offset of the subarray.
3153 * @param count length of the subarray.
3154 * @return a {@code String} that contains the characters of the
3155 * specified subarray of the character array.
3156 * @exception IndexOutOfBoundsException if {@code offset} is
3157 * negative, or {@code count} is negative, or
3158 * {@code offset+count} is larger than
3159 * {@code data.length}.
3160 */
3161 public static String valueOf(char[] data, int offset, int count) {
3162 return new String(data, offset, count);
3163 }
3164
3165 /**
3166 * Equivalent to {@link #valueOf(char[], int, int)}.
3167 *
3168 * @param data the character array.
3169 * @param offset initial offset of the subarray.
3170 * @param count length of the subarray.
3171 * @return a {@code String} that contains the characters of the
3172 * specified subarray of the character array.
3173 * @exception IndexOutOfBoundsException if {@code offset} is
3174 * negative, or {@code count} is negative, or
3175 * {@code offset+count} is larger than
3176 * {@code data.length}.
3177 */
3178 public static String copyValueOf(char[] data, int offset, int count) {
3179 return new String(data, offset, count);
3180 }
3181
3182 /**
3183 * Equivalent to {@link #valueOf(char[])}.
3184 *
3185 * @param data the character array.
3186 * @return a {@code String} that contains the characters of the
3187 * character array.
3188 */
3189 public static String copyValueOf(char[] data) {
3190 return new String(data);
3191 }
3192
3193 /**
3194 * Returns the string representation of the {@code boolean} argument.
3195 *
3196 * @param b a {@code boolean}.
3197 * @return if the argument is {@code true}, a string equal to
3198 * {@code "true"} is returned; otherwise, a string equal to
3199 * {@code "false"} is returned.
3200 */
3201 public static String valueOf(boolean b) {
3202 return b ? "true" : "false";
3203 }
3204
3205 /**
3206 * Returns the string representation of the {@code char}
3207 * argument.
3208 *
3209 * @param c a {@code char}.
3343 int copied = len;
3344 for (; copied < limit - copied; copied <<= 1) {
3345 System.arraycopy(multiple, 0, multiple, copied, copied);
3346 }
3347 System.arraycopy(multiple, 0, multiple, copied, limit - copied);
3348 return new String(multiple, coder);
3349 }
3350
3351 ////////////////////////////////////////////////////////////////
3352
3353 /**
3354 * Copy character bytes from this string into dst starting at dstBegin.
3355 * This method doesn't perform any range checking.
3356 *
3357 * Invoker guarantees: dst is in UTF16 (inflate itself for asb), if two
3358 * coders are different, and dst is big enough (range check)
3359 *
3360 * @param dstBegin the char index, not offset of byte[]
3361 * @param coder the coder of dst[]
3362 */
3363 void getBytes(byte[] dst, int dstBegin, byte coder) {
3364 if (coder() == coder) {
3365 System.arraycopy(value, 0, dst, dstBegin << coder, value.length);
3366 } else { // this.coder == LATIN && coder == UTF16
3367 StringLatin1.inflate(value, 0, dst, dstBegin, value.length);
3368 }
3369 }
3370
3371 /*
3372 * Package private constructor. Trailing Void argument is there for
3373 * disambiguating it against other (public) constructors.
3374 *
3375 * Stores the char[] value into a byte[] that each byte represents
3376 * the8 low-order bits of the corresponding character, if the char[]
3377 * contains only latin1 character. Or a byte[] that stores all
3378 * characters in their byte sequences defined by the {@code StringUTF16}.
3379 */
3380 String(char[] value, int off, int len, Void sig) {
3381 if (len == 0) {
3382 this.value = "".value;
3383 this.coder = "".coder;
|