1 /* 2 * Copyright (C) 2014 The Android Open Source Project 3 * Copyright (c) 1994, 2010, Oracle and/or its affiliates. All rights reserved. 4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 5 * 6 * This code is free software; you can redistribute it and/or modify it 7 * under the terms of the GNU General Public License version 2 only, as 8 * published by the Free Software Foundation. Oracle designates this 9 * particular file as subject to the "Classpath" exception as provided 10 * by Oracle in the LICENSE file that accompanied this code. 11 * 12 * This code is distributed in the hope that it will be useful, but WITHOUT 13 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 14 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 15 * version 2 for more details (a copy is included in the LICENSE file that 16 * accompanied this code). 17 * 18 * You should have received a copy of the GNU General Public License version 19 * 2 along with this work; if not, write to the Free Software Foundation, 20 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 21 * 22 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 23 * or visit www.oracle.com if you need additional information or have any 24 * questions. 25 */ 26 package java.lang; 27 28 import java.io.ObjectStreamField; 29 import java.io.UnsupportedEncodingException; 30 import java.lang.ArrayIndexOutOfBoundsException; 31 import java.nio.charset.Charset; 32 import java.util.ArrayList; 33 import java.util.Arrays; 34 import java.util.Comparator; 35 import java.util.Formatter; 36 import java.util.Locale; 37 import java.util.regex.Matcher; 38 import java.util.regex.Pattern; 39 import java.util.regex.PatternSyntaxException; 40 41 import libcore.util.CharsetUtils; 42 import libcore.util.EmptyArray; 43 44 /** 45 * The <code>String</code> class represents character strings. All 46 * string literals in Java programs, such as <code>"abc"</code>, are 47 * implemented as instances of this class. 48 * <p> 49 * Strings are constant; their values cannot be changed after they 50 * are created. String buffers support mutable strings. 51 * Because String objects are immutable they can be shared. For example: 52 * <p><blockquote><pre> 53 * String str = "abc"; 54 * </pre></blockquote><p> 55 * is equivalent to: 56 * <p><blockquote><pre> 57 * char data[] = {'a', 'b', 'c'}; 58 * String str = new String(data); 59 * </pre></blockquote><p> 60 * Here are some more examples of how strings can be used: 61 * <p><blockquote><pre> 62 * System.out.println("abc"); 63 * String cde = "cde"; 64 * System.out.println("abc" + cde); 65 * String c = "abc".substring(2,3); 66 * String d = cde.substring(1, 2); 67 * </pre></blockquote> 68 * <p> 69 * The class <code>String</code> includes methods for examining 70 * individual characters of the sequence, for comparing strings, for 71 * searching strings, for extracting substrings, and for creating a 72 * copy of a string with all characters translated to uppercase or to 73 * lowercase. Case mapping is based on the Unicode Standard version 74 * specified by the {@link java.lang.Character Character} class. 75 * <p> 76 * The Java language provides special support for the string 77 * concatenation operator ( + ), and for conversion of 78 * other objects to strings. String concatenation is implemented 79 * through the <code>StringBuilder</code>(or <code>StringBuffer</code>) 80 * class and its <code>append</code> method. 81 * String conversions are implemented through the method 82 * <code>toString</code>, defined by <code>Object</code> and 83 * inherited by all classes in Java. For additional information on 84 * string concatenation and conversion, see Gosling, Joy, and Steele, 85 * <i>The Java Language Specification</i>. 86 * 87 * <p> Unless otherwise noted, passing a <tt>null</tt> argument to a constructor 88 * or method in this class will cause a {@link NullPointerException} to be 89 * thrown. 90 * 91 * <p>A <code>String</code> represents a string in the UTF-16 format 92 * in which <em>supplementary characters</em> are represented by <em>surrogate 93 * pairs</em> (see the section <a href="Character.html#unicode">Unicode 94 * Character Representations</a> in the <code>Character</code> class for 95 * more information). 96 * Index values refer to <code>char</code> code units, so a supplementary 97 * character uses two positions in a <code>String</code>. 98 * <p>The <code>String</code> class provides methods for dealing with 99 * Unicode code points (i.e., characters), in addition to those for 100 * dealing with Unicode code units (i.e., <code>char</code> values). 101 * 102 * @author Lee Boynton 103 * @author Arthur van Hoff 104 * @author Martin Buchholz 105 * @author Ulf Zibis 106 * @see java.lang.Object#toString() 107 * @see java.lang.StringBuffer 108 * @see java.lang.StringBuilder 109 * @see java.nio.charset.Charset 110 * @since JDK1.0 111 */ 112 113 public final class String 114 implements java.io.Serializable, Comparable<String>, CharSequence { 115 116 // The associated character storage is managed by the runtime. We only 117 // keep track of the length here. 118 // 119 // private final char value[]; 120 private final int count; 121 122 /** Cache the hash code for the string */ 123 private int hash; // Default to 0 124 125 /** use serialVersionUID from JDK 1.0.2 for interoperability */ 126 private static final long serialVersionUID = -6849794470754667710L; 127 128 /** 129 * Class String is special cased within the Serialization Stream Protocol. 130 * 131 * A String instance is written initially into an ObjectOutputStream in the 132 * following format: 133 * <pre> 134 * <code>TC_STRING</code> (utf String) 135 * </pre> 136 * The String is written by method <code>DataOutput.writeUTF</code>. 137 * A new handle is generated to refer to all future references to the 138 * string instance within the stream. 139 */ 140 private static final ObjectStreamField[] serialPersistentFields = 141 new ObjectStreamField[0]; 142 143 /** 144 * Initializes a newly created {@code String} object so that it represents 145 * an empty character sequence. Note that use of this constructor is 146 * unnecessary since Strings are immutable. 147 */ String()148 public String() { 149 throw new UnsupportedOperationException("Use StringFactory instead."); 150 } 151 152 /** 153 * Initializes a newly created {@code String} object so that it represents 154 * the same sequence of characters as the argument; in other words, the 155 * newly created string is a copy of the argument string. Unless an 156 * explicit copy of {@code original} is needed, use of this constructor is 157 * unnecessary since Strings are immutable. 158 * 159 * @param original 160 * A {@code String} 161 */ String(String original)162 public String(String original) { 163 throw new UnsupportedOperationException("Use StringFactory instead."); 164 } 165 166 /** 167 * Allocates a new {@code String} so that it represents the sequence of 168 * characters currently contained in the character array argument. The 169 * contents of the character array are copied; subsequent modification of 170 * the character array does not affect the newly created string. 171 * 172 * @param value 173 * The initial value of the string 174 */ String(char value[])175 public String(char value[]) { 176 throw new UnsupportedOperationException("Use StringFactory instead."); 177 } 178 179 /** 180 * Allocates a new {@code String} that contains characters from a subarray 181 * of the character array argument. The {@code offset} argument is the 182 * index of the first character of the subarray and the {@code count} 183 * argument specifies the length of the subarray. The contents of the 184 * subarray are copied; subsequent modification of the character array does 185 * not affect the newly created string. 186 * 187 * @param value 188 * Array that is the source of characters 189 * 190 * @param offset 191 * The initial offset 192 * 193 * @param count 194 * The length 195 * 196 * @throws IndexOutOfBoundsException 197 * If the {@code offset} and {@code count} arguments index 198 * characters outside the bounds of the {@code value} array 199 */ String(char value[], int offset, int count)200 public String(char value[], int offset, int count) { 201 throw new UnsupportedOperationException("Use StringFactory instead."); 202 } 203 204 /** 205 * Allocates a new {@code String} that contains characters from a subarray 206 * of the <a href="Character.html#unicode">Unicode code point</a> array 207 * argument. The {@code offset} argument is the index of the first code 208 * point of the subarray and the {@code count} argument specifies the 209 * length of the subarray. The contents of the subarray are converted to 210 * {@code char}s; subsequent modification of the {@code int} array does not 211 * affect the newly created string. 212 * 213 * @param codePoints 214 * Array that is the source of Unicode code points 215 * 216 * @param offset 217 * The initial offset 218 * 219 * @param count 220 * The length 221 * 222 * @throws IllegalArgumentException 223 * If any invalid Unicode code point is found in {@code 224 * codePoints} 225 * 226 * @throws IndexOutOfBoundsException 227 * If the {@code offset} and {@code count} arguments index 228 * characters outside the bounds of the {@code codePoints} array 229 * 230 * @since 1.5 231 */ String(int[] codePoints, int offset, int count)232 public String(int[] codePoints, int offset, int count) { 233 throw new UnsupportedOperationException("Use StringFactory instead."); 234 } 235 236 /** 237 * Allocates a new {@code String} constructed from a subarray of an array 238 * of 8-bit integer values. 239 * 240 * <p> The {@code offset} argument is the index of the first byte of the 241 * subarray, and the {@code count} argument specifies the length of the 242 * subarray. 243 * 244 * <p> Each {@code byte} in the subarray is converted to a {@code char} as 245 * specified in the method above. 246 * 247 * @deprecated This method does not properly convert bytes into characters. 248 * As of JDK 1.1, the preferred way to do this is via the 249 * {@code String} constructors that take a {@link 250 * java.nio.charset.Charset}, charset name, or that use the platform's 251 * default charset. 252 * 253 * @param ascii 254 * The bytes to be converted to characters 255 * 256 * @param hibyte 257 * The top 8 bits of each 16-bit Unicode code unit 258 * 259 * @param offset 260 * The initial offset 261 * @param count 262 * The length 263 * 264 * @throws IndexOutOfBoundsException 265 * If the {@code offset} or {@code count} argument is invalid 266 * 267 * @see #String(byte[], int) 268 * @see #String(byte[], int, int, java.lang.String) 269 * @see #String(byte[], int, int, java.nio.charset.Charset) 270 * @see #String(byte[], int, int) 271 * @see #String(byte[], java.lang.String) 272 * @see #String(byte[], java.nio.charset.Charset) 273 * @see #String(byte[]) 274 */ 275 @Deprecated String(byte ascii[], int hibyte, int offset, int count)276 public String(byte ascii[], int hibyte, int offset, int count) { 277 throw new UnsupportedOperationException("Use StringFactory instead."); 278 } 279 280 /** 281 * Allocates a new {@code String} containing characters constructed from 282 * an array of 8-bit integer values. Each character <i>c</i>in the 283 * resulting string is constructed from the corresponding component 284 * <i>b</i> in the byte array such that: 285 * 286 * <blockquote><pre> 287 * <b><i>c</i></b> == (char)(((hibyte & 0xff) << 8) 288 * | (<b><i>b</i></b> & 0xff)) 289 * </pre></blockquote> 290 * 291 * @deprecated This method does not properly convert bytes into 292 * characters. As of JDK 1.1, the preferred way to do this is via the 293 * {@code String} constructors that take a {@link 294 * java.nio.charset.Charset}, charset name, or that use the platform's 295 * default charset. 296 * 297 * @param ascii 298 * The bytes to be converted to characters 299 * 300 * @param hibyte 301 * The top 8 bits of each 16-bit Unicode code unit 302 * 303 * @see #String(byte[], int, int, java.lang.String) 304 * @see #String(byte[], int, int, java.nio.charset.Charset) 305 * @see #String(byte[], int, int) 306 * @see #String(byte[], java.lang.String) 307 * @see #String(byte[], java.nio.charset.Charset) 308 * @see #String(byte[]) 309 */ 310 @Deprecated String(byte ascii[], int hibyte)311 public String(byte ascii[], int hibyte) { 312 throw new UnsupportedOperationException("Use StringFactory instead."); 313 } 314 315 /** 316 * Constructs a new {@code String} by decoding the specified subarray of 317 * bytes using the specified charset. The length of the new {@code String} 318 * is a function of the charset, and hence may not be equal to the length 319 * of the subarray. 320 * 321 * <p> The behavior of this constructor when the given bytes are not valid 322 * in the given charset is unspecified. The {@link 323 * java.nio.charset.CharsetDecoder} class should be used when more control 324 * over the decoding process is required. 325 * 326 * @param bytes 327 * The bytes to be decoded into characters 328 * 329 * @param offset 330 * The index of the first byte to decode 331 * 332 * @param length 333 * The number of bytes to decode 334 335 * @param charsetName 336 * The name of a supported {@linkplain java.nio.charset.Charset 337 * charset} 338 * 339 * @throws UnsupportedEncodingException 340 * If the named charset is not supported 341 * 342 * @throws IndexOutOfBoundsException 343 * If the {@code offset} and {@code length} arguments index 344 * characters outside the bounds of the {@code bytes} array 345 * 346 * @since JDK1.1 347 */ String(byte bytes[], int offset, int length, String charsetName)348 public String(byte bytes[], int offset, int length, String charsetName) 349 throws UnsupportedEncodingException { 350 throw new UnsupportedOperationException("Use StringFactory instead."); 351 } 352 353 /** 354 * Constructs a new {@code String} by decoding the specified subarray of 355 * bytes using the specified {@linkplain java.nio.charset.Charset charset}. 356 * The length of the new {@code String} is a function of the charset, and 357 * hence may not be equal to the length of the subarray. 358 * 359 * <p> This method always replaces malformed-input and unmappable-character 360 * sequences with this charset's default replacement string. The {@link 361 * java.nio.charset.CharsetDecoder} class should be used when more control 362 * over the decoding process is required. 363 * 364 * @param bytes 365 * The bytes to be decoded into characters 366 * 367 * @param offset 368 * The index of the first byte to decode 369 * 370 * @param length 371 * The number of bytes to decode 372 * 373 * @param charset 374 * The {@linkplain java.nio.charset.Charset charset} to be used to 375 * decode the {@code bytes} 376 * 377 * @throws IndexOutOfBoundsException 378 * If the {@code offset} and {@code length} arguments index 379 * characters outside the bounds of the {@code bytes} array 380 * 381 * @since 1.6 382 */ String(byte bytes[], int offset, int length, Charset charset)383 public String(byte bytes[], int offset, int length, Charset charset) { 384 throw new UnsupportedOperationException("Use StringFactory instead."); 385 } 386 387 /** 388 * Constructs a new {@code String} by decoding the specified array of bytes 389 * using the specified {@linkplain java.nio.charset.Charset charset}. The 390 * length of the new {@code String} is a function of the charset, and hence 391 * may not be equal to the length of the byte array. 392 * 393 * <p> The behavior of this constructor when the given bytes are not valid 394 * in the given charset is unspecified. The {@link 395 * java.nio.charset.CharsetDecoder} class should be used when more control 396 * over the decoding process is required. 397 * 398 * @param bytes 399 * The bytes to be decoded into characters 400 * 401 * @param charsetName 402 * The name of a supported {@linkplain java.nio.charset.Charset 403 * charset} 404 * 405 * @throws UnsupportedEncodingException 406 * If the named charset is not supported 407 * 408 * @since JDK1.1 409 */ String(byte bytes[], String charsetName)410 public String(byte bytes[], String charsetName) 411 throws UnsupportedEncodingException { 412 throw new UnsupportedOperationException("Use StringFactory instead."); 413 } 414 415 /** 416 * Constructs a new {@code String} by decoding the specified array of 417 * bytes using the specified {@linkplain java.nio.charset.Charset charset}. 418 * The length of the new {@code String} is a function of the charset, and 419 * hence may not be equal to the length of the byte array. 420 * 421 * <p> This method always replaces malformed-input and unmappable-character 422 * sequences with this charset's default replacement string. The {@link 423 * java.nio.charset.CharsetDecoder} class should be used when more control 424 * over the decoding process is required. 425 * 426 * @param bytes 427 * The bytes to be decoded into characters 428 * 429 * @param charset 430 * The {@linkplain java.nio.charset.Charset charset} to be used to 431 * decode the {@code bytes} 432 * 433 * @since 1.6 434 */ String(byte bytes[], Charset charset)435 public String(byte bytes[], Charset charset) { 436 throw new UnsupportedOperationException("Use StringFactory instead."); 437 } 438 439 /** 440 * Constructs a new {@code String} by decoding the specified subarray of 441 * bytes using the platform's default charset. The length of the new 442 * {@code String} is a function of the charset, and hence may not be equal 443 * to the length of the subarray. 444 * 445 * <p> The behavior of this constructor when the given bytes are not valid 446 * in the default charset is unspecified. The {@link 447 * java.nio.charset.CharsetDecoder} class should be used when more control 448 * over the decoding process is required. 449 * 450 * @param bytes 451 * The bytes to be decoded into characters 452 * 453 * @param offset 454 * The index of the first byte to decode 455 * 456 * @param length 457 * The number of bytes to decode 458 * 459 * @throws IndexOutOfBoundsException 460 * If the {@code offset} and the {@code length} arguments index 461 * characters outside the bounds of the {@code bytes} array 462 * 463 * @since JDK1.1 464 */ String(byte bytes[], int offset, int length)465 public String(byte bytes[], int offset, int length) { 466 throw new UnsupportedOperationException("Use StringFactory instead."); 467 } 468 469 /** 470 * Constructs a new {@code String} by decoding the specified array of bytes 471 * using the platform's default charset. The length of the new {@code 472 * String} is a function of the charset, and hence may not be equal to the 473 * length of the byte array. 474 * 475 * <p> The behavior of this constructor when the given bytes are not valid 476 * in the default charset is unspecified. The {@link 477 * java.nio.charset.CharsetDecoder} class should be used when more control 478 * over the decoding process is required. 479 * 480 * @param bytes 481 * The bytes to be decoded into characters 482 * 483 * @since JDK1.1 484 */ String(byte bytes[])485 public String(byte bytes[]) { 486 throw new UnsupportedOperationException("Use StringFactory instead."); 487 } 488 489 /** 490 * Allocates a new string that contains the sequence of characters 491 * currently contained in the string buffer argument. The contents of the 492 * string buffer are copied; subsequent modification of the string buffer 493 * does not affect the newly created string. 494 * 495 * @param buffer 496 * A {@code StringBuffer} 497 */ String(StringBuffer buffer)498 public String(StringBuffer buffer) { 499 throw new UnsupportedOperationException("Use StringFactory instead."); 500 } 501 502 /** 503 * Allocates a new string that contains the sequence of characters 504 * currently contained in the string builder argument. The contents of the 505 * string builder are copied; subsequent modification of the string builder 506 * does not affect the newly created string. 507 * 508 * <p> This constructor is provided to ease migration to {@code 509 * StringBuilder}. Obtaining a string from a string builder via the {@code 510 * toString} method is likely to run faster and is generally preferred. 511 * 512 * @param builder 513 * A {@code StringBuilder} 514 * 515 * @since 1.5 516 */ String(StringBuilder builder)517 public String(StringBuilder builder) { 518 throw new UnsupportedOperationException("Use StringFactory instead."); 519 } 520 521 522 /** 523 * Package private constructor 524 * 525 * @deprecated Use {@link #String(char[],int,int)} instead. 526 */ 527 @Deprecated String(int offset, int count, char[] value)528 String(int offset, int count, char[] value) { 529 throw new UnsupportedOperationException("Use StringFactory instead."); 530 } 531 532 /** 533 * Returns the length of this string. 534 * The length is equal to the number of <a href="Character.html#unicode">Unicode 535 * code units</a> in the string. 536 * 537 * @return the length of the sequence of characters represented by this 538 * object. 539 */ length()540 public int length() { 541 return count; 542 } 543 544 /** 545 * Returns <tt>true</tt> if, and only if, {@link #length()} is <tt>0</tt>. 546 * 547 * @return <tt>true</tt> if {@link #length()} is <tt>0</tt>, otherwise 548 * <tt>false</tt> 549 * 550 * @since 1.6 551 */ isEmpty()552 public boolean isEmpty() { 553 return count == 0; 554 } 555 556 /** 557 * Returns the <code>char</code> value at the 558 * specified index. An index ranges from <code>0</code> to 559 * <code>length() - 1</code>. The first <code>char</code> value of the sequence 560 * is at index <code>0</code>, the next at index <code>1</code>, 561 * and so on, as for array indexing. 562 * 563 * <p>If the <code>char</code> value specified by the index is a 564 * <a href="Character.html#unicode">surrogate</a>, the surrogate 565 * value is returned. 566 * 567 * @param index the index of the <code>char</code> value. 568 * @return the <code>char</code> value at the specified index of this string. 569 * The first <code>char</code> value is at index <code>0</code>. 570 * @exception IndexOutOfBoundsException if the <code>index</code> 571 * argument is negative or not less than the length of this 572 * string. 573 */ charAt(int index)574 public native char charAt(int index); 575 setCharAt(int index, char c)576 native void setCharAt(int index, char c); 577 578 /** 579 * Returns the character (Unicode code point) at the specified 580 * index. The index refers to <code>char</code> values 581 * (Unicode code units) and ranges from <code>0</code> to 582 * {@link #length()}<code> - 1</code>. 583 * 584 * <p> If the <code>char</code> value specified at the given index 585 * is in the high-surrogate range, the following index is less 586 * than the length of this <code>String</code>, and the 587 * <code>char</code> value at the following index is in the 588 * low-surrogate range, then the supplementary code point 589 * corresponding to this surrogate pair is returned. Otherwise, 590 * the <code>char</code> value at the given index is returned. 591 * 592 * @param index the index to the <code>char</code> values 593 * @return the code point value of the character at the 594 * <code>index</code> 595 * @exception IndexOutOfBoundsException if the <code>index</code> 596 * argument is negative or not less than the length of this 597 * string. 598 * @since 1.5 599 */ codePointAt(int index)600 public int codePointAt(int index) { 601 if ((index < 0) || (index >= count)) { 602 throw new StringIndexOutOfBoundsException(index); 603 } 604 return Character.codePointAt(this, index); 605 } 606 607 /** 608 * Returns the character (Unicode code point) before the specified 609 * index. The index refers to <code>char</code> values 610 * (Unicode code units) and ranges from <code>1</code> to {@link 611 * CharSequence#length() length}. 612 * 613 * <p> If the <code>char</code> value at <code>(index - 1)</code> 614 * is in the low-surrogate range, <code>(index - 2)</code> is not 615 * negative, and the <code>char</code> value at <code>(index - 616 * 2)</code> is in the high-surrogate range, then the 617 * supplementary code point value of the surrogate pair is 618 * returned. If the <code>char</code> value at <code>index - 619 * 1</code> is an unpaired low-surrogate or a high-surrogate, the 620 * surrogate value is returned. 621 * 622 * @param index the index following the code point that should be returned 623 * @return the Unicode code point value before the given index. 624 * @exception IndexOutOfBoundsException if the <code>index</code> 625 * argument is less than 1 or greater than the length 626 * of this string. 627 * @since 1.5 628 */ codePointBefore(int index)629 public int codePointBefore(int index) { 630 int i = index - 1; 631 if ((i < 0) || (i >= count)) { 632 throw new StringIndexOutOfBoundsException(index); 633 } 634 return Character.codePointBefore(this, index); 635 } 636 637 /** 638 * Returns the number of Unicode code points in the specified text 639 * range of this <code>String</code>. The text range begins at the 640 * specified <code>beginIndex</code> and extends to the 641 * <code>char</code> at index <code>endIndex - 1</code>. Thus the 642 * length (in <code>char</code>s) of the text range is 643 * <code>endIndex-beginIndex</code>. Unpaired surrogates within 644 * the text range count as one code point each. 645 * 646 * @param beginIndex the index to the first <code>char</code> of 647 * the text range. 648 * @param endIndex the index after the last <code>char</code> of 649 * the text range. 650 * @return the number of Unicode code points in the specified text 651 * range 652 * @exception IndexOutOfBoundsException if the 653 * <code>beginIndex</code> is negative, or <code>endIndex</code> 654 * is larger than the length of this <code>String</code>, or 655 * <code>beginIndex</code> is larger than <code>endIndex</code>. 656 * @since 1.5 657 */ codePointCount(int beginIndex, int endIndex)658 public int codePointCount(int beginIndex, int endIndex) { 659 if (beginIndex < 0 || endIndex > count || beginIndex > endIndex) { 660 throw new IndexOutOfBoundsException(); 661 } 662 return Character.codePointCount(this, beginIndex, endIndex); 663 } 664 665 /** 666 * Returns the index within this <code>String</code> that is 667 * offset from the given <code>index</code> by 668 * <code>codePointOffset</code> code points. Unpaired surrogates 669 * within the text range given by <code>index</code> and 670 * <code>codePointOffset</code> count as one code point each. 671 * 672 * @param index the index to be offset 673 * @param codePointOffset the offset in code points 674 * @return the index within this <code>String</code> 675 * @exception IndexOutOfBoundsException if <code>index</code> 676 * is negative or larger then the length of this 677 * <code>String</code>, or if <code>codePointOffset</code> is positive 678 * and the substring starting with <code>index</code> has fewer 679 * than <code>codePointOffset</code> code points, 680 * or if <code>codePointOffset</code> is negative and the substring 681 * before <code>index</code> has fewer than the absolute value 682 * of <code>codePointOffset</code> code points. 683 * @since 1.5 684 */ offsetByCodePoints(int index, int codePointOffset)685 public int offsetByCodePoints(int index, int codePointOffset) { 686 if (index < 0 || index > count) { 687 throw new IndexOutOfBoundsException(); 688 } 689 return Character.offsetByCodePoints(this, index, codePointOffset); 690 } 691 692 /** 693 * Copies characters from this string into the destination character 694 * array. 695 * <p> 696 * The first character to be copied is at index <code>srcBegin</code>; 697 * the last character to be copied is at index <code>srcEnd-1</code> 698 * (thus the total number of characters to be copied is 699 * <code>srcEnd-srcBegin</code>). The characters are copied into the 700 * subarray of <code>dst</code> starting at index <code>dstBegin</code> 701 * and ending at index: 702 * <p><blockquote><pre> 703 * dstbegin + (srcEnd-srcBegin) - 1 704 * </pre></blockquote> 705 * 706 * @param srcBegin index of the first character in the string 707 * to copy. 708 * @param srcEnd index after the last character in the string 709 * to copy. 710 * @param dst the destination array. 711 * @param dstBegin the start offset in the destination array. 712 * @exception IndexOutOfBoundsException If any of the following 713 * is true: 714 * <ul><li><code>srcBegin</code> is negative. 715 * <li><code>srcBegin</code> is greater than <code>srcEnd</code> 716 * <li><code>srcEnd</code> is greater than the length of this 717 * string 718 * <li><code>dstBegin</code> is negative 719 * <li><code>dstBegin+(srcEnd-srcBegin)</code> is larger than 720 * <code>dst.length</code></ul> 721 */ getChars(int srcBegin, int srcEnd, char dst[], int dstBegin)722 public void getChars(int srcBegin, int srcEnd, char dst[], int dstBegin) { 723 if (dst == null) { 724 throw new NullPointerException("dst == null"); 725 } 726 727 if (srcBegin < 0) { 728 throw new StringIndexOutOfBoundsException(this, srcBegin); 729 } 730 if (srcEnd > count) { 731 throw new StringIndexOutOfBoundsException(this, srcEnd); 732 } 733 734 int n = srcEnd - srcBegin; 735 if (srcEnd < srcBegin) { 736 throw new StringIndexOutOfBoundsException(this, srcBegin, n); 737 } 738 739 if (dstBegin < 0) { 740 throw new ArrayIndexOutOfBoundsException("dstBegin < 0. dstBegin=" + dstBegin); 741 } 742 // dstBegin can be equal to dst.length, but only in the case where zero chars are to be 743 // copied. 744 if (dstBegin > dst.length) { 745 throw new ArrayIndexOutOfBoundsException( 746 "dstBegin > dst.length. dstBegin=" + dstBegin + ", dst.length=" + dst.length); 747 } 748 if (n > dst.length - dstBegin) { 749 throw new ArrayIndexOutOfBoundsException( 750 "n > dst.length - dstBegin. n=" + n + ", dst.length=" + dst.length 751 + "dstBegin=" + dstBegin); 752 } 753 754 getCharsNoCheck(srcBegin, srcEnd, dst, dstBegin); 755 } 756 757 /** 758 * getChars without bounds checks, for use by other classes 759 * within the java.lang package only. The caller is responsible for 760 * ensuring that start >= 0 && start <= end && end <= count. 761 */ getCharsNoCheck(int start, int end, char[] buffer, int index)762 native void getCharsNoCheck(int start, int end, char[] buffer, int index); 763 764 765 /** 766 * Copies characters from this string into the destination byte array. Each 767 * byte receives the 8 low-order bits of the corresponding character. The 768 * eight high-order bits of each character are not copied and do not 769 * participate in the transfer in any way. 770 * 771 * <p> The first character to be copied is at index {@code srcBegin}; the 772 * last character to be copied is at index {@code srcEnd-1}. The total 773 * number of characters to be copied is {@code srcEnd-srcBegin}. The 774 * characters, converted to bytes, are copied into the subarray of {@code 775 * dst} starting at index {@code dstBegin} and ending at index: 776 * 777 * <blockquote><pre> 778 * dstbegin + (srcEnd-srcBegin) - 1 779 * </pre></blockquote> 780 * 781 * @deprecated This method does not properly convert characters into 782 * bytes. As of JDK 1.1, the preferred way to do this is via the 783 * {@link #getBytes()} method, which uses the platform's default charset. 784 * 785 * @param srcBegin 786 * Index of the first character in the string to copy 787 * 788 * @param srcEnd 789 * Index after the last character in the string to copy 790 * 791 * @param dst 792 * The destination array 793 * 794 * @param dstBegin 795 * The start offset in the destination array 796 * 797 * @throws IndexOutOfBoundsException 798 * If any of the following is true: 799 * <ul> 800 * <li> {@code srcBegin} is negative 801 * <li> {@code srcBegin} is greater than {@code srcEnd} 802 * <li> {@code srcEnd} is greater than the length of this String 803 * <li> {@code dstBegin} is negative 804 * <li> {@code dstBegin+(srcEnd-srcBegin)} is larger than {@code 805 * dst.length} 806 * </ul> 807 */ 808 @Deprecated getBytes(int srcBegin, int srcEnd, byte dst[], int dstBegin)809 public void getBytes(int srcBegin, int srcEnd, byte dst[], int dstBegin) { 810 if (srcBegin < 0) { 811 throw new StringIndexOutOfBoundsException(this, srcBegin); 812 } 813 if (srcEnd > count) { 814 throw new StringIndexOutOfBoundsException(this, srcEnd); 815 } 816 if (srcBegin > srcEnd) { 817 throw new StringIndexOutOfBoundsException(this, (srcEnd - srcBegin)); 818 } 819 820 int j = dstBegin; 821 int n = srcEnd; 822 int i = srcBegin; 823 824 while (i < n) { 825 dst[j++] = (byte)charAt(i++); 826 } 827 } 828 829 /** 830 * Encodes this {@code String} into a sequence of bytes using the named 831 * charset, storing the result into a new byte array. 832 * 833 * <p> The behavior of this method when this string cannot be encoded in 834 * the given charset is unspecified. The {@link 835 * java.nio.charset.CharsetEncoder} class should be used when more control 836 * over the encoding process is required. 837 * 838 * @param charsetName 839 * The name of a supported {@linkplain java.nio.charset.Charset 840 * charset} 841 * 842 * @return The resultant byte array 843 * 844 * @throws UnsupportedEncodingException 845 * If the named charset is not supported 846 * 847 * @since JDK1.1 848 */ getBytes(String charsetName)849 public byte[] getBytes(String charsetName) 850 throws UnsupportedEncodingException { 851 return getBytes(Charset.forNameUEE(charsetName)); 852 } 853 854 /** 855 * Encodes this {@code String} into a sequence of bytes using the given 856 * {@linkplain java.nio.charset.Charset charset}, storing the result into a 857 * new byte array. 858 * 859 * <p> This method always replaces malformed-input and unmappable-character 860 * sequences with this charset's default replacement byte array. The 861 * {@link java.nio.charset.CharsetEncoder} class should be used when more 862 * control over the encoding process is required. 863 * 864 * @param charset 865 * The {@linkplain java.nio.charset.Charset} to be used to encode 866 * the {@code String} 867 * 868 * @return The resultant byte array 869 * 870 * @since 1.6 871 */ getBytes(Charset charset)872 public byte[] getBytes(Charset charset) { 873 if (charset == null) { 874 throw new NullPointerException("charset == null"); 875 } 876 877 final String name = charset.name(); 878 if ("UTF-8".equals(name)) { 879 return CharsetUtils.toUtf8Bytes(this, 0, count); 880 } else if ("ISO-8859-1".equals(name)) { 881 return CharsetUtils.toIsoLatin1Bytes(this, 0, count); 882 } else if ("US-ASCII".equals(name)) { 883 return CharsetUtils.toAsciiBytes(this, 0, count); 884 } else if ("UTF-16BE".equals(name)) { 885 return CharsetUtils.toBigEndianUtf16Bytes(this, 0, count); 886 } 887 888 return StringCoding.encode(charset, this); 889 } 890 891 /** 892 * Encodes this {@code String} into a sequence of bytes using the 893 * platform's default charset, storing the result into a new byte array. 894 * 895 * <p> The behavior of this method when this string cannot be encoded in 896 * the default charset is unspecified. The {@link 897 * java.nio.charset.CharsetEncoder} class should be used when more control 898 * over the encoding process is required. 899 * 900 * @return The resultant byte array 901 * 902 * @since JDK1.1 903 */ getBytes()904 public byte[] getBytes() { 905 return getBytes(Charset.defaultCharset()); 906 } 907 908 /** 909 * Compares this string to the specified object. The result is {@code 910 * true} if and only if the argument is not {@code null} and is a {@code 911 * String} object that represents the same sequence of characters as this 912 * object. 913 * 914 * @param anObject 915 * The object to compare this {@code String} against 916 * 917 * @return {@code true} if the given object represents a {@code String} 918 * equivalent to this string, {@code false} otherwise 919 * 920 * @see #compareTo(String) 921 * @see #equalsIgnoreCase(String) 922 */ equals(Object anObject)923 public boolean equals(Object anObject) { 924 if (this == anObject) { 925 return true; 926 } 927 if (anObject instanceof String) { 928 String anotherString = (String) anObject; 929 int n = count; 930 if (n == anotherString.count) { 931 int i = 0; 932 while (n-- != 0) { 933 if (charAt(i) != anotherString.charAt(i)) 934 return false; 935 i++; 936 } 937 return true; 938 } 939 } 940 return false; 941 } 942 943 /** 944 * Compares this string to the specified {@code StringBuffer}. The result 945 * is {@code true} if and only if this {@code String} represents the same 946 * sequence of characters as the specified {@code StringBuffer}. 947 * 948 * @param sb 949 * The {@code StringBuffer} to compare this {@code String} against 950 * 951 * @return {@code true} if this {@code String} represents the same 952 * sequence of characters as the specified {@code StringBuffer}, 953 * {@code false} otherwise 954 * 955 * @since 1.4 956 */ contentEquals(StringBuffer sb)957 public boolean contentEquals(StringBuffer sb) { 958 synchronized (sb) { 959 return contentEquals((CharSequence) sb); 960 } 961 } 962 963 /** 964 * Compares this string to the specified {@code CharSequence}. The result 965 * is {@code true} if and only if this {@code String} represents the same 966 * sequence of char values as the specified sequence. 967 * 968 * @param cs 969 * The sequence to compare this {@code String} against 970 * 971 * @return {@code true} if this {@code String} represents the same 972 * sequence of char values as the specified sequence, {@code 973 * false} otherwise 974 * 975 * @since 1.5 976 */ contentEquals(CharSequence cs)977 public boolean contentEquals(CharSequence cs) { 978 if (count != cs.length()) 979 return false; 980 // Argument is a StringBuffer, StringBuilder 981 if (cs instanceof AbstractStringBuilder) { 982 char v2[] = ((AbstractStringBuilder) cs).getValue(); 983 int i = 0; 984 int n = count; 985 while (n-- != 0) { 986 if (charAt(i) != v2[i]) 987 return false; 988 i++; 989 } 990 return true; 991 } 992 // Argument is a String 993 if (cs.equals(this)) 994 return true; 995 // Argument is a generic CharSequence 996 int i = 0; 997 int n = count; 998 while (n-- != 0) { 999 if (charAt(i) != cs.charAt(i)) 1000 return false; 1001 i++; 1002 } 1003 return true; 1004 } 1005 1006 /** 1007 * Compares this {@code String} to another {@code String}, ignoring case 1008 * considerations. Two strings are considered equal ignoring case if they 1009 * are of the same length and corresponding characters in the two strings 1010 * are equal ignoring case. 1011 * 1012 * <p> Two characters {@code c1} and {@code c2} are considered the same 1013 * ignoring case if at least one of the following is true: 1014 * <ul> 1015 * <li> The two characters are the same (as compared by the 1016 * {@code ==} operator) 1017 * <li> Applying the method {@link 1018 * java.lang.Character#toUpperCase(char)} to each character 1019 * produces the same result 1020 * <li> Applying the method {@link 1021 * java.lang.Character#toLowerCase(char)} to each character 1022 * produces the same result 1023 * </ul> 1024 * 1025 * @param anotherString 1026 * The {@code String} to compare this {@code String} against 1027 * 1028 * @return {@code true} if the argument is not {@code null} and it 1029 * represents an equivalent {@code String} ignoring case; {@code 1030 * false} otherwise 1031 * 1032 * @see #equals(Object) 1033 */ equalsIgnoreCase(String anotherString)1034 public boolean equalsIgnoreCase(String anotherString) { 1035 return (this == anotherString) ? true 1036 : (anotherString != null) 1037 && (anotherString.count == count) 1038 && regionMatches(true, 0, anotherString, 0, count); 1039 } 1040 1041 /** 1042 * Compares two strings lexicographically. 1043 * The comparison is based on the Unicode value of each character in 1044 * the strings. The character sequence represented by this 1045 * <code>String</code> object is compared lexicographically to the 1046 * character sequence represented by the argument string. The result is 1047 * a negative integer if this <code>String</code> object 1048 * lexicographically precedes the argument string. The result is a 1049 * positive integer if this <code>String</code> object lexicographically 1050 * follows the argument string. The result is zero if the strings 1051 * are equal; <code>compareTo</code> returns <code>0</code> exactly when 1052 * the {@link #equals(Object)} method would return <code>true</code>. 1053 * <p> 1054 * This is the definition of lexicographic ordering. If two strings are 1055 * different, then either they have different characters at some index 1056 * that is a valid index for both strings, or their lengths are different, 1057 * or both. If they have different characters at one or more index 1058 * positions, let <i>k</i> be the smallest such index; then the string 1059 * whose character at position <i>k</i> has the smaller value, as 1060 * determined by using the < operator, lexicographically precedes the 1061 * other string. In this case, <code>compareTo</code> returns the 1062 * difference of the two character values at position <code>k</code> in 1063 * the two string -- that is, the value: 1064 * <blockquote><pre> 1065 * this.charAt(k)-anotherString.charAt(k) 1066 * </pre></blockquote> 1067 * If there is no index position at which they differ, then the shorter 1068 * string lexicographically precedes the longer string. In this case, 1069 * <code>compareTo</code> returns the difference of the lengths of the 1070 * strings -- that is, the value: 1071 * <blockquote><pre> 1072 * this.length()-anotherString.length() 1073 * </pre></blockquote> 1074 * 1075 * @param anotherString the <code>String</code> to be compared. 1076 * @return the value <code>0</code> if the argument string is equal to 1077 * this string; a value less than <code>0</code> if this string 1078 * is lexicographically less than the string argument; and a 1079 * value greater than <code>0</code> if this string is 1080 * lexicographically greater than the string argument. 1081 */ compareTo(String anotherString)1082 public native int compareTo(String anotherString); 1083 1084 /** 1085 * A Comparator that orders <code>String</code> objects as by 1086 * <code>compareToIgnoreCase</code>. This comparator is serializable. 1087 * <p> 1088 * Note that this Comparator does <em>not</em> take locale into account, 1089 * and will result in an unsatisfactory ordering for certain locales. 1090 * The java.text package provides <em>Collators</em> to allow 1091 * locale-sensitive ordering. 1092 * 1093 * @see java.text.Collator#compare(String, String) 1094 * @since 1.2 1095 */ 1096 public static final Comparator<String> CASE_INSENSITIVE_ORDER 1097 = new CaseInsensitiveComparator(); 1098 private static class CaseInsensitiveComparator 1099 implements Comparator<String>, java.io.Serializable { 1100 // use serialVersionUID from JDK 1.2.2 for interoperability 1101 private static final long serialVersionUID = 8575799808933029326L; 1102 compare(String s1, String s2)1103 public int compare(String s1, String s2) { 1104 int n1 = s1.length(); 1105 int n2 = s2.length(); 1106 int min = Math.min(n1, n2); 1107 for (int i = 0; i < min; i++) { 1108 char c1 = s1.charAt(i); 1109 char c2 = s2.charAt(i); 1110 if (c1 != c2) { 1111 c1 = Character.toUpperCase(c1); 1112 c2 = Character.toUpperCase(c2); 1113 if (c1 != c2) { 1114 c1 = Character.toLowerCase(c1); 1115 c2 = Character.toLowerCase(c2); 1116 if (c1 != c2) { 1117 // No overflow because of numeric promotion 1118 return c1 - c2; 1119 } 1120 } 1121 } 1122 } 1123 return n1 - n2; 1124 } 1125 } 1126 1127 /** 1128 * Compares two strings lexicographically, ignoring case 1129 * differences. This method returns an integer whose sign is that of 1130 * calling <code>compareTo</code> with normalized versions of the strings 1131 * where case differences have been eliminated by calling 1132 * <code>Character.toLowerCase(Character.toUpperCase(character))</code> on 1133 * each character. 1134 * <p> 1135 * Note that this method does <em>not</em> take locale into account, 1136 * and will result in an unsatisfactory ordering for certain locales. 1137 * The java.text package provides <em>collators</em> to allow 1138 * locale-sensitive ordering. 1139 * 1140 * @param str the <code>String</code> to be compared. 1141 * @return a negative integer, zero, or a positive integer as the 1142 * specified String is greater than, equal to, or less 1143 * than this String, ignoring case considerations. 1144 * @see java.text.Collator#compare(String, String) 1145 * @since 1.2 1146 */ compareToIgnoreCase(String str)1147 public int compareToIgnoreCase(String str) { 1148 return CASE_INSENSITIVE_ORDER.compare(this, str); 1149 } 1150 1151 /** 1152 * Tests if two string regions are equal. 1153 * <p> 1154 * A substring of this <tt>String</tt> object is compared to a substring 1155 * of the argument other. The result is true if these substrings 1156 * represent identical character sequences. The substring of this 1157 * <tt>String</tt> object to be compared begins at index <tt>toffset</tt> 1158 * and has length <tt>len</tt>. The substring of other to be compared 1159 * begins at index <tt>ooffset</tt> and has length <tt>len</tt>. The 1160 * result is <tt>false</tt> if and only if at least one of the following 1161 * is true: 1162 * <ul><li><tt>toffset</tt> is negative. 1163 * <li><tt>ooffset</tt> is negative. 1164 * <li><tt>toffset+len</tt> is greater than the length of this 1165 * <tt>String</tt> object. 1166 * <li><tt>ooffset+len</tt> is greater than the length of the other 1167 * argument. 1168 * <li>There is some nonnegative integer <i>k</i> less than <tt>len</tt> 1169 * such that: 1170 * <tt>this.charAt(toffset+<i>k</i>) != other.charAt(ooffset+<i>k</i>)</tt> 1171 * </ul> 1172 * 1173 * @param toffset the starting offset of the subregion in this string. 1174 * @param other the string argument. 1175 * @param ooffset the starting offset of the subregion in the string 1176 * argument. 1177 * @param len the number of characters to compare. 1178 * @return <code>true</code> if the specified subregion of this string 1179 * exactly matches the specified subregion of the string argument; 1180 * <code>false</code> otherwise. 1181 */ regionMatches(int toffset, String other, int ooffset, int len)1182 public boolean regionMatches(int toffset, String other, int ooffset, 1183 int len) { 1184 int to = toffset; 1185 int po = ooffset; 1186 // Note: toffset, ooffset, or len might be near -1>>>1. 1187 if ((ooffset < 0) || (toffset < 0) 1188 || (toffset > (long)count - len) 1189 || (ooffset > (long)other.count - len)) { 1190 return false; 1191 } 1192 while (len-- > 0) { 1193 if (charAt(to++) != other.charAt(po++)) { 1194 return false; 1195 } 1196 } 1197 return true; 1198 } 1199 1200 /** 1201 * Tests if two string regions are equal. 1202 * <p> 1203 * A substring of this <tt>String</tt> object is compared to a substring 1204 * of the argument <tt>other</tt>. The result is <tt>true</tt> if these 1205 * substrings represent character sequences that are the same, ignoring 1206 * case if and only if <tt>ignoreCase</tt> is true. The substring of 1207 * this <tt>String</tt> object to be compared begins at index 1208 * <tt>toffset</tt> and has length <tt>len</tt>. The substring of 1209 * <tt>other</tt> to be compared begins at index <tt>ooffset</tt> and 1210 * has length <tt>len</tt>. The result is <tt>false</tt> if and only if 1211 * at least one of the following is true: 1212 * <ul><li><tt>toffset</tt> is negative. 1213 * <li><tt>ooffset</tt> is negative. 1214 * <li><tt>toffset+len</tt> is greater than the length of this 1215 * <tt>String</tt> object. 1216 * <li><tt>ooffset+len</tt> is greater than the length of the other 1217 * argument. 1218 * <li><tt>ignoreCase</tt> is <tt>false</tt> and there is some nonnegative 1219 * integer <i>k</i> less than <tt>len</tt> such that: 1220 * <blockquote><pre> 1221 * this.charAt(toffset+k) != other.charAt(ooffset+k) 1222 * </pre></blockquote> 1223 * <li><tt>ignoreCase</tt> is <tt>true</tt> and there is some nonnegative 1224 * integer <i>k</i> less than <tt>len</tt> such that: 1225 * <blockquote><pre> 1226 * Character.toLowerCase(this.charAt(toffset+k)) != 1227 Character.toLowerCase(other.charAt(ooffset+k)) 1228 * </pre></blockquote> 1229 * and: 1230 * <blockquote><pre> 1231 * Character.toUpperCase(this.charAt(toffset+k)) != 1232 * Character.toUpperCase(other.charAt(ooffset+k)) 1233 * </pre></blockquote> 1234 * </ul> 1235 * 1236 * @param ignoreCase if <code>true</code>, ignore case when comparing 1237 * characters. 1238 * @param toffset the starting offset of the subregion in this 1239 * string. 1240 * @param other the string argument. 1241 * @param ooffset the starting offset of the subregion in the string 1242 * argument. 1243 * @param len the number of characters to compare. 1244 * @return <code>true</code> if the specified subregion of this string 1245 * matches the specified subregion of the string argument; 1246 * <code>false</code> otherwise. Whether the matching is exact 1247 * or case insensitive depends on the <code>ignoreCase</code> 1248 * argument. 1249 */ regionMatches(boolean ignoreCase, int toffset, String other, int ooffset, int len)1250 public boolean regionMatches(boolean ignoreCase, int toffset, 1251 String other, int ooffset, int len) { 1252 int to = toffset; 1253 int po = ooffset; 1254 // Note: toffset, ooffset, or len might be near -1>>>1. 1255 if ((ooffset < 0) || (toffset < 0) 1256 || (toffset > (long)count - len) 1257 || (ooffset > (long)other.count - len)) { 1258 return false; 1259 } 1260 while (len-- > 0) { 1261 char c1 = charAt(to++); 1262 char c2 = other.charAt(po++); 1263 if (c1 == c2) { 1264 continue; 1265 } 1266 if (ignoreCase) { 1267 // If characters don't match but case may be ignored, 1268 // try converting both characters to uppercase. 1269 // If the results match, then the comparison scan should 1270 // continue. 1271 char u1 = Character.toUpperCase(c1); 1272 char u2 = Character.toUpperCase(c2); 1273 if (u1 == u2) { 1274 continue; 1275 } 1276 // Unfortunately, conversion to uppercase does not work properly 1277 // for the Georgian alphabet, which has strange rules about case 1278 // conversion. So we need to make one last check before 1279 // exiting. 1280 if (Character.toLowerCase(u1) == Character.toLowerCase(u2)) { 1281 continue; 1282 } 1283 } 1284 return false; 1285 } 1286 return true; 1287 } 1288 1289 /** 1290 * Tests if the substring of this string beginning at the 1291 * specified index starts with the specified prefix. 1292 * 1293 * @param prefix the prefix. 1294 * @param toffset where to begin looking in this string. 1295 * @return <code>true</code> if the character sequence represented by the 1296 * argument is a prefix of the substring of this object starting 1297 * at index <code>toffset</code>; <code>false</code> otherwise. 1298 * The result is <code>false</code> if <code>toffset</code> is 1299 * negative or greater than the length of this 1300 * <code>String</code> object; otherwise the result is the same 1301 * as the result of the expression 1302 * <pre> 1303 * this.substring(toffset).startsWith(prefix) 1304 * </pre> 1305 */ startsWith(String prefix, int toffset)1306 public boolean startsWith(String prefix, int toffset) { 1307 int to = toffset; 1308 int po = 0; 1309 int pc = prefix.count; 1310 // Note: toffset might be near -1>>>1. 1311 if ((toffset < 0) || (toffset > count - pc)) { 1312 return false; 1313 } 1314 while (--pc >= 0) { 1315 if (charAt(to++) != prefix.charAt(po++)) { 1316 return false; 1317 } 1318 } 1319 return true; 1320 } 1321 1322 /** 1323 * Tests if this string starts with the specified prefix. 1324 * 1325 * @param prefix the prefix. 1326 * @return <code>true</code> if the character sequence represented by the 1327 * argument is a prefix of the character sequence represented by 1328 * this string; <code>false</code> otherwise. 1329 * Note also that <code>true</code> will be returned if the 1330 * argument is an empty string or is equal to this 1331 * <code>String</code> object as determined by the 1332 * {@link #equals(Object)} method. 1333 * @since 1. 0 1334 */ startsWith(String prefix)1335 public boolean startsWith(String prefix) { 1336 return startsWith(prefix, 0); 1337 } 1338 1339 /** 1340 * Tests if this string ends with the specified suffix. 1341 * 1342 * @param suffix the suffix. 1343 * @return <code>true</code> if the character sequence represented by the 1344 * argument is a suffix of the character sequence represented by 1345 * this object; <code>false</code> otherwise. Note that the 1346 * result will be <code>true</code> if the argument is the 1347 * empty string or is equal to this <code>String</code> object 1348 * as determined by the {@link #equals(Object)} method. 1349 */ endsWith(String suffix)1350 public boolean endsWith(String suffix) { 1351 return startsWith(suffix, count - suffix.count); 1352 } 1353 1354 /** 1355 * Returns a hash code for this string. The hash code for a 1356 * <code>String</code> object is computed as 1357 * <blockquote><pre> 1358 * s[0]*31^(n-1) + s[1]*31^(n-2) + ... + s[n-1] 1359 * </pre></blockquote> 1360 * using <code>int</code> arithmetic, where <code>s[i]</code> is the 1361 * <i>i</i>th character of the string, <code>n</code> is the length of 1362 * the string, and <code>^</code> indicates exponentiation. 1363 * (The hash value of the empty string is zero.) 1364 * 1365 * @return a hash code value for this object. 1366 */ hashCode()1367 public int hashCode() { 1368 int h = hash; 1369 if (h == 0 && count > 0) { 1370 for (int i = 0; i < count; i++) { 1371 h = 31 * h + charAt(i); 1372 } 1373 hash = h; 1374 } 1375 return h; 1376 } 1377 1378 /** 1379 * Returns the index within this string of the first occurrence of 1380 * the specified character. If a character with value 1381 * <code>ch</code> occurs in the character sequence represented by 1382 * this <code>String</code> object, then the index (in Unicode 1383 * code units) of the first such occurrence is returned. For 1384 * values of <code>ch</code> in the range from 0 to 0xFFFF 1385 * (inclusive), this is the smallest value <i>k</i> such that: 1386 * <blockquote><pre> 1387 * this.charAt(<i>k</i>) == ch 1388 * </pre></blockquote> 1389 * is true. For other values of <code>ch</code>, it is the 1390 * smallest value <i>k</i> such that: 1391 * <blockquote><pre> 1392 * this.codePointAt(<i>k</i>) == ch 1393 * </pre></blockquote> 1394 * is true. In either case, if no such character occurs in this 1395 * string, then <code>-1</code> is returned. 1396 * 1397 * @param ch a character (Unicode code point). 1398 * @return the index of the first occurrence of the character in the 1399 * character sequence represented by this object, or 1400 * <code>-1</code> if the character does not occur. 1401 */ indexOf(int ch)1402 public int indexOf(int ch) { 1403 return indexOf(ch, 0); 1404 } 1405 1406 /** 1407 * Returns the index within this string of the first occurrence of the 1408 * specified character, starting the search at the specified index. 1409 * <p> 1410 * If a character with value <code>ch</code> occurs in the 1411 * character sequence represented by this <code>String</code> 1412 * object at an index no smaller than <code>fromIndex</code>, then 1413 * the index of the first such occurrence is returned. For values 1414 * of <code>ch</code> in the range from 0 to 0xFFFF (inclusive), 1415 * this is the smallest value <i>k</i> such that: 1416 * <blockquote><pre> 1417 * (this.charAt(<i>k</i>) == ch) && (<i>k</i> >= fromIndex) 1418 * </pre></blockquote> 1419 * is true. For other values of <code>ch</code>, it is the 1420 * smallest value <i>k</i> such that: 1421 * <blockquote><pre> 1422 * (this.codePointAt(<i>k</i>) == ch) && (<i>k</i> >= fromIndex) 1423 * </pre></blockquote> 1424 * is true. In either case, if no such character occurs in this 1425 * string at or after position <code>fromIndex</code>, then 1426 * <code>-1</code> is returned. 1427 * 1428 * <p> 1429 * There is no restriction on the value of <code>fromIndex</code>. If it 1430 * is negative, it has the same effect as if it were zero: this entire 1431 * string may be searched. If it is greater than the length of this 1432 * string, it has the same effect as if it were equal to the length of 1433 * this string: <code>-1</code> is returned. 1434 * 1435 * <p>All indices are specified in <code>char</code> values 1436 * (Unicode code units). 1437 * 1438 * @param ch a character (Unicode code point). 1439 * @param fromIndex the index to start the search from. 1440 * @return the index of the first occurrence of the character in the 1441 * character sequence represented by this object that is greater 1442 * than or equal to <code>fromIndex</code>, or <code>-1</code> 1443 * if the character does not occur. 1444 */ indexOf(int ch, int fromIndex)1445 public int indexOf(int ch, int fromIndex) { 1446 final int max = count; 1447 if (fromIndex < 0) { 1448 fromIndex = 0; 1449 } else if (fromIndex >= max) { 1450 // Note: fromIndex might be near -1>>>1. 1451 return -1; 1452 } 1453 1454 if (ch < Character.MIN_SUPPLEMENTARY_CODE_POINT) { 1455 // handle most cases here (ch is a BMP code point or a 1456 // negative value (invalid code point)) 1457 for (int i = fromIndex; i < max; i++) { 1458 if (charAt(i) == ch) { 1459 return i; 1460 } 1461 } 1462 return -1; 1463 } else { 1464 return indexOfSupplementary(ch, fromIndex); 1465 } 1466 } 1467 fastIndexOf(int c, int start)1468 private native int fastIndexOf(int c, int start); 1469 1470 /** 1471 * Handles (rare) calls of indexOf with a supplementary character. 1472 */ indexOfSupplementary(int ch, int fromIndex)1473 private int indexOfSupplementary(int ch, int fromIndex) { 1474 if (Character.isValidCodePoint(ch)) { 1475 final char hi = Character.highSurrogate(ch); 1476 final char lo = Character.lowSurrogate(ch); 1477 final int max = count - 1; 1478 for (int i = fromIndex; i < max; i++) { 1479 if (charAt(i) == hi && charAt(i + 1) == lo) { 1480 return i; 1481 } 1482 } 1483 } 1484 return -1; 1485 } 1486 1487 /** 1488 * Returns the index within this string of the last occurrence of 1489 * the specified character. For values of <code>ch</code> in the 1490 * range from 0 to 0xFFFF (inclusive), the index (in Unicode code 1491 * units) returned is the largest value <i>k</i> such that: 1492 * <blockquote><pre> 1493 * this.charAt(<i>k</i>) == ch 1494 * </pre></blockquote> 1495 * is true. For other values of <code>ch</code>, it is the 1496 * largest value <i>k</i> such that: 1497 * <blockquote><pre> 1498 * this.codePointAt(<i>k</i>) == ch 1499 * </pre></blockquote> 1500 * is true. In either case, if no such character occurs in this 1501 * string, then <code>-1</code> is returned. The 1502 * <code>String</code> is searched backwards starting at the last 1503 * character. 1504 * 1505 * @param ch a character (Unicode code point). 1506 * @return the index of the last occurrence of the character in the 1507 * character sequence represented by this object, or 1508 * <code>-1</code> if the character does not occur. 1509 */ lastIndexOf(int ch)1510 public int lastIndexOf(int ch) { 1511 return lastIndexOf(ch, count - 1); 1512 } 1513 1514 /** 1515 * Returns the index within this string of the last occurrence of 1516 * the specified character, searching backward starting at the 1517 * specified index. For values of <code>ch</code> in the range 1518 * from 0 to 0xFFFF (inclusive), the index returned is the largest 1519 * value <i>k</i> such that: 1520 * <blockquote><pre> 1521 * (this.charAt(<i>k</i>) == ch) && (<i>k</i> <= fromIndex) 1522 * </pre></blockquote> 1523 * is true. For other values of <code>ch</code>, it is the 1524 * largest value <i>k</i> such that: 1525 * <blockquote><pre> 1526 * (this.codePointAt(<i>k</i>) == ch) && (<i>k</i> <= fromIndex) 1527 * </pre></blockquote> 1528 * is true. In either case, if no such character occurs in this 1529 * string at or before position <code>fromIndex</code>, then 1530 * <code>-1</code> is returned. 1531 * 1532 * <p>All indices are specified in <code>char</code> values 1533 * (Unicode code units). 1534 * 1535 * @param ch a character (Unicode code point). 1536 * @param fromIndex the index to start the search from. There is no 1537 * restriction on the value of <code>fromIndex</code>. If it is 1538 * greater than or equal to the length of this string, it has 1539 * the same effect as if it were equal to one less than the 1540 * length of this string: this entire string may be searched. 1541 * If it is negative, it has the same effect as if it were -1: 1542 * -1 is returned. 1543 * @return the index of the last occurrence of the character in the 1544 * character sequence represented by this object that is less 1545 * than or equal to <code>fromIndex</code>, or <code>-1</code> 1546 * if the character does not occur before that point. 1547 */ lastIndexOf(int ch, int fromIndex)1548 public int lastIndexOf(int ch, int fromIndex) { 1549 if (ch < Character.MIN_SUPPLEMENTARY_CODE_POINT) { 1550 // handle most cases here (ch is a BMP code point or a 1551 // negative value (invalid code point)) 1552 int i = Math.min(fromIndex, count - 1); 1553 for (; i >= 0; i--) { 1554 if (charAt(i) == ch) { 1555 return i; 1556 } 1557 } 1558 return -1; 1559 } else { 1560 return lastIndexOfSupplementary(ch, fromIndex); 1561 } 1562 } 1563 1564 /** 1565 * Handles (rare) calls of lastIndexOf with a supplementary character. 1566 */ lastIndexOfSupplementary(int ch, int fromIndex)1567 private int lastIndexOfSupplementary(int ch, int fromIndex) { 1568 if (Character.isValidCodePoint(ch)) { 1569 char hi = Character.highSurrogate(ch); 1570 char lo = Character.lowSurrogate(ch); 1571 int i = Math.min(fromIndex, count - 2); 1572 for (; i >= 0; i--) { 1573 if (charAt(i) == hi && charAt(i + 1) == lo) { 1574 return i; 1575 } 1576 } 1577 } 1578 return -1; 1579 } 1580 1581 /** 1582 * Returns the index within this string of the first occurrence of the 1583 * specified substring. 1584 * 1585 * <p>The returned index is the smallest value <i>k</i> for which: 1586 * <blockquote><pre> 1587 * this.startsWith(str, <i>k</i>) 1588 * </pre></blockquote> 1589 * If no such value of <i>k</i> exists, then {@code -1} is returned. 1590 * 1591 * @param str the substring to search for. 1592 * @return the index of the first occurrence of the specified substring, 1593 * or {@code -1} if there is no such occurrence. 1594 */ indexOf(String str)1595 public int indexOf(String str) { 1596 return indexOf(str, 0); 1597 } 1598 1599 /** 1600 * Returns the index within this string of the first occurrence of the 1601 * specified substring, starting at the specified index. 1602 * 1603 * <p>The returned index is the smallest value <i>k</i> for which: 1604 * <blockquote><pre> 1605 * <i>k</i> >= fromIndex && this.startsWith(str, <i>k</i>) 1606 * </pre></blockquote> 1607 * If no such value of <i>k</i> exists, then {@code -1} is returned. 1608 * 1609 * @param str the substring to search for. 1610 * @param fromIndex the index from which to start the search. 1611 * @return the index of the first occurrence of the specified substring, 1612 * starting at the specified index, 1613 * or {@code -1} if there is no such occurrence. 1614 */ indexOf(String str, int fromIndex)1615 public int indexOf(String str, int fromIndex) { 1616 return indexOf(this, str, fromIndex); 1617 } 1618 1619 /** 1620 * Code shared by String and StringBuffer to do searches. The 1621 * source is the character array being searched, and the target 1622 * is the string being searched for. 1623 * 1624 * @param source the characters being searched. 1625 * @param target the characters being searched for. 1626 * @param fromIndex the index to begin searching from. 1627 */ indexOf(String source, String target, int fromIndex)1628 static int indexOf(String source, 1629 String target, 1630 int fromIndex) { 1631 if (fromIndex >= source.count) { 1632 return (target.count == 0 ? source.count : -1); 1633 } 1634 if (fromIndex < 0) { 1635 fromIndex = 0; 1636 } 1637 if (target.count == 0) { 1638 return fromIndex; 1639 } 1640 1641 char first = target.charAt(0); 1642 int max = (source.count - target.count); 1643 1644 for (int i = fromIndex; i <= max; i++) { 1645 /* Look for first character. */ 1646 if (source.charAt(i)!= first) { 1647 while (++i <= max && source.charAt(i) != first); 1648 } 1649 1650 /* Found first character, now look at the rest of v2 */ 1651 if (i <= max) { 1652 int j = i + 1; 1653 int end = j + target.count - 1; 1654 for (int k = 1; j < end && source.charAt(j) 1655 == target.charAt(k); j++, k++); 1656 1657 if (j == end) { 1658 /* Found whole string. */ 1659 return i; 1660 } 1661 } 1662 } 1663 return -1; 1664 } 1665 1666 /** 1667 * Code shared by String and StringBuffer to do searches. The 1668 * source is the character array being searched, and the target 1669 * is the string being searched for. 1670 * 1671 * @param source the characters being searched. 1672 * @param sourceOffset offset of the source string. 1673 * @param sourceCount count of the source string. 1674 * @param target the characters being searched for. 1675 * @param targetOffset offset of the target string. 1676 * @param targetCount count of the target string. 1677 * @param fromIndex the index to begin searching from. 1678 */ indexOf(char[] source, int sourceOffset, int sourceCount, char[] target, int targetOffset, int targetCount, int fromIndex)1679 static int indexOf(char[] source, int sourceOffset, int sourceCount, 1680 char[] target, int targetOffset, int targetCount, 1681 int fromIndex) { 1682 if (fromIndex >= sourceCount) { 1683 return (targetCount == 0 ? sourceCount : -1); 1684 } 1685 if (fromIndex < 0) { 1686 fromIndex = 0; 1687 } 1688 if (targetCount == 0) { 1689 return fromIndex; 1690 } 1691 1692 char first = target[targetOffset]; 1693 int max = sourceOffset + (sourceCount - targetCount); 1694 1695 for (int i = sourceOffset + fromIndex; i <= max; i++) { 1696 /* Look for first character. */ 1697 if (source[i] != first) { 1698 while (++i <= max && source[i] != first); 1699 } 1700 1701 /* Found first character, now look at the rest of v2 */ 1702 if (i <= max) { 1703 int j = i + 1; 1704 int end = j + targetCount - 1; 1705 for (int k = targetOffset + 1; j < end && source[j] 1706 == target[k]; j++, k++); 1707 1708 if (j == end) { 1709 /* Found whole string. */ 1710 return i - sourceOffset; 1711 } 1712 } 1713 } 1714 return -1; 1715 } 1716 1717 /** 1718 * Returns the index within this string of the last occurrence of the 1719 * specified substring. The last occurrence of the empty string "" 1720 * is considered to occur at the index value {@code this.length()}. 1721 * 1722 * <p>The returned index is the largest value <i>k</i> for which: 1723 * <blockquote><pre> 1724 * this.startsWith(str, <i>k</i>) 1725 * </pre></blockquote> 1726 * If no such value of <i>k</i> exists, then {@code -1} is returned. 1727 * 1728 * @param str the substring to search for. 1729 * @return the index of the last occurrence of the specified substring, 1730 * or {@code -1} if there is no such occurrence. 1731 */ lastIndexOf(String str)1732 public int lastIndexOf(String str) { 1733 return lastIndexOf(str, count); 1734 } 1735 1736 /** 1737 * Returns the index within this string of the last occurrence of the 1738 * specified substring, searching backward starting at the specified index. 1739 * 1740 * <p>The returned index is the largest value <i>k</i> for which: 1741 * <blockquote><pre> 1742 * <i>k</i> <= fromIndex && this.startsWith(str, <i>k</i>) 1743 * </pre></blockquote> 1744 * If no such value of <i>k</i> exists, then {@code -1} is returned. 1745 * 1746 * @param str the substring to search for. 1747 * @param fromIndex the index to start the search from. 1748 * @return the index of the last occurrence of the specified substring, 1749 * searching backward from the specified index, 1750 * or {@code -1} if there is no such occurrence. 1751 */ lastIndexOf(String str, int fromIndex)1752 public int lastIndexOf(String str, int fromIndex) { 1753 return lastIndexOf(this, str, fromIndex); 1754 } 1755 1756 /** 1757 * Code shared by String and StringBuffer to do searches. The 1758 * source is the character array being searched, and the target 1759 * is the string being searched for. 1760 * 1761 * @param source the characters being searched. 1762 * @param sourceOffset offset of the source string. 1763 * @param sourceCount count of the source string. 1764 * @param target the characters being searched for. 1765 * @param targetOffset offset of the target string. 1766 * @param targetCount count of the target string. 1767 * @param fromIndex the index to begin searching from. 1768 */ lastIndexOf(String source, String target, int fromIndex)1769 static int lastIndexOf(String source, 1770 String target, 1771 int fromIndex) { 1772 /* 1773 * Check arguments; return immediately where possible. For 1774 * consistency, don't check for null str. 1775 */ 1776 int rightIndex = source.count - target.count; 1777 if (fromIndex < 0) { 1778 return -1; 1779 } 1780 if (fromIndex > rightIndex) { 1781 fromIndex = rightIndex; 1782 } 1783 /* Empty string always matches. */ 1784 if (target.count == 0) { 1785 return fromIndex; 1786 } 1787 1788 int strLastIndex = target.count - 1; 1789 char strLastChar = target.charAt(strLastIndex); 1790 int min = target.count - 1; 1791 int i = min + fromIndex; 1792 1793 startSearchForLastChar: 1794 while (true) { 1795 while (i >= min && source.charAt(i) != strLastChar) { 1796 i--; 1797 } 1798 if (i < min) { 1799 return -1; 1800 } 1801 int j = i - 1; 1802 int start = j - (target.count - 1); 1803 int k = strLastIndex - 1; 1804 1805 while (j > start) { 1806 if (source.charAt(j--) != target.charAt(k--)) { 1807 i--; 1808 continue startSearchForLastChar; 1809 } 1810 } 1811 return start + 1; 1812 } 1813 } 1814 1815 /** 1816 * Code shared by String and StringBuffer to do searches. The 1817 * source is the character array being searched, and the target 1818 * is the string being searched for. 1819 * 1820 * @param source the characters being searched. 1821 * @param sourceOffset offset of the source string. 1822 * @param sourceCount count of the source string. 1823 * @param target the characters being searched for. 1824 * @param targetOffset offset of the target string. 1825 * @param targetCount count of the target string. 1826 * @param fromIndex the index to begin searching from. 1827 */ lastIndexOf(char[] source, int sourceOffset, int sourceCount, char[] target, int targetOffset, int targetCount, int fromIndex)1828 static int lastIndexOf(char[] source, int sourceOffset, int sourceCount, 1829 char[] target, int targetOffset, int targetCount, 1830 int fromIndex) { 1831 /* 1832 * Check arguments; return immediately where possible. For 1833 * consistency, don't check for null str. 1834 */ 1835 int rightIndex = sourceCount - targetCount; 1836 if (fromIndex < 0) { 1837 return -1; 1838 } 1839 if (fromIndex > rightIndex) { 1840 fromIndex = rightIndex; 1841 } 1842 /* Empty string always matches. */ 1843 if (targetCount == 0) { 1844 return fromIndex; 1845 } 1846 1847 int strLastIndex = targetOffset + targetCount - 1; 1848 char strLastChar = target[strLastIndex]; 1849 int min = sourceOffset + targetCount - 1; 1850 int i = min + fromIndex; 1851 1852 startSearchForLastChar: 1853 while (true) { 1854 while (i >= min && source[i] != strLastChar) { 1855 i--; 1856 } 1857 if (i < min) { 1858 return -1; 1859 } 1860 int j = i - 1; 1861 int start = j - (targetCount - 1); 1862 int k = strLastIndex - 1; 1863 1864 while (j > start) { 1865 if (source[j--] != target[k--]) { 1866 i--; 1867 continue startSearchForLastChar; 1868 } 1869 } 1870 return start - sourceOffset + 1; 1871 } 1872 } 1873 1874 /** 1875 * Returns a new string that is a substring of this string. The 1876 * substring begins with the character at the specified index and 1877 * extends to the end of this string. <p> 1878 * Examples: 1879 * <blockquote><pre> 1880 * "unhappy".substring(2) returns "happy" 1881 * "Harbison".substring(3) returns "bison" 1882 * "emptiness".substring(9) returns "" (an empty string) 1883 * </pre></blockquote> 1884 * 1885 * @param beginIndex the beginning index, inclusive. 1886 * @return the specified substring. 1887 * @exception IndexOutOfBoundsException if 1888 * <code>beginIndex</code> is negative or larger than the 1889 * length of this <code>String</code> object. 1890 */ substring(int beginIndex)1891 public String substring(int beginIndex) { 1892 if (beginIndex < 0) { 1893 throw new StringIndexOutOfBoundsException(this, beginIndex); 1894 } 1895 int subLen = count - beginIndex; 1896 if (subLen < 0) { 1897 throw new StringIndexOutOfBoundsException(this, beginIndex); 1898 } 1899 return (beginIndex == 0) ? this : fastSubstring(beginIndex, subLen); 1900 } 1901 1902 /** 1903 * Returns a new string that is a substring of this string. The 1904 * substring begins at the specified <code>beginIndex</code> and 1905 * extends to the character at index <code>endIndex - 1</code>. 1906 * Thus the length of the substring is <code>endIndex-beginIndex</code>. 1907 * <p> 1908 * Examples: 1909 * <blockquote><pre> 1910 * "hamburger".substring(4, 8) returns "urge" 1911 * "smiles".substring(1, 5) returns "mile" 1912 * </pre></blockquote> 1913 * 1914 * @param beginIndex the beginning index, inclusive. 1915 * @param endIndex the ending index, exclusive. 1916 * @return the specified substring. 1917 * @exception IndexOutOfBoundsException if the 1918 * <code>beginIndex</code> is negative, or 1919 * <code>endIndex</code> is larger than the length of 1920 * this <code>String</code> object, or 1921 * <code>beginIndex</code> is larger than 1922 * <code>endIndex</code>. 1923 */ substring(int beginIndex, int endIndex)1924 public String substring(int beginIndex, int endIndex) { 1925 if (beginIndex < 0) { 1926 throw new StringIndexOutOfBoundsException(this, beginIndex); 1927 } 1928 1929 int subLen = endIndex - beginIndex; 1930 if (endIndex > count || subLen < 0) { 1931 throw new StringIndexOutOfBoundsException(this, beginIndex, subLen); 1932 } 1933 1934 return ((beginIndex == 0) && (endIndex == count)) ? this 1935 : fastSubstring(beginIndex, subLen); 1936 } 1937 fastSubstring(int start, int length)1938 private native String fastSubstring(int start, int length); 1939 1940 /** 1941 * Returns a new character sequence that is a subsequence of this sequence. 1942 * 1943 * <p> An invocation of this method of the form 1944 * 1945 * <blockquote><pre> 1946 * str.subSequence(begin, end)</pre></blockquote> 1947 * 1948 * behaves in exactly the same way as the invocation 1949 * 1950 * <blockquote><pre> 1951 * str.substring(begin, end)</pre></blockquote> 1952 * 1953 * This method is defined so that the <tt>String</tt> class can implement 1954 * the {@link CharSequence} interface. </p> 1955 * 1956 * @param beginIndex the begin index, inclusive. 1957 * @param endIndex the end index, exclusive. 1958 * @return the specified subsequence. 1959 * 1960 * @throws IndexOutOfBoundsException 1961 * if <tt>beginIndex</tt> or <tt>endIndex</tt> are negative, 1962 * if <tt>endIndex</tt> is greater than <tt>length()</tt>, 1963 * or if <tt>beginIndex</tt> is greater than <tt>startIndex</tt> 1964 * 1965 * @since 1.4 1966 * @spec JSR-51 1967 */ subSequence(int beginIndex, int endIndex)1968 public CharSequence subSequence(int beginIndex, int endIndex) { 1969 return this.substring(beginIndex, endIndex); 1970 } 1971 1972 /** 1973 * Concatenates the specified string to the end of this string. 1974 * <p> 1975 * If the length of the argument string is <code>0</code>, then this 1976 * <code>String</code> object is returned. Otherwise, a new 1977 * <code>String</code> object is created, representing a character 1978 * sequence that is the concatenation of the character sequence 1979 * represented by this <code>String</code> object and the character 1980 * sequence represented by the argument string.<p> 1981 * Examples: 1982 * <blockquote><pre> 1983 * "cares".concat("s") returns "caress" 1984 * "to".concat("get").concat("her") returns "together" 1985 * </pre></blockquote> 1986 * 1987 * @param str the <code>String</code> that is concatenated to the end 1988 * of this <code>String</code>. 1989 * @return a string that represents the concatenation of this object's 1990 * characters followed by the string argument's characters. 1991 */ concat(String str)1992 public native String concat(String str); 1993 1994 /** 1995 * Returns a new string resulting from replacing all occurrences of 1996 * <code>oldChar</code> in this string with <code>newChar</code>. 1997 * <p> 1998 * If the character <code>oldChar</code> does not occur in the 1999 * character sequence represented by this <code>String</code> object, 2000 * then a reference to this <code>String</code> object is returned. 2001 * Otherwise, a new <code>String</code> object is created that 2002 * represents a character sequence identical to the character sequence 2003 * represented by this <code>String</code> object, except that every 2004 * occurrence of <code>oldChar</code> is replaced by an occurrence 2005 * of <code>newChar</code>. 2006 * <p> 2007 * Examples: 2008 * <blockquote><pre> 2009 * "mesquite in your cellar".replace('e', 'o') 2010 * returns "mosquito in your collar" 2011 * "the war of baronets".replace('r', 'y') 2012 * returns "the way of bayonets" 2013 * "sparring with a purple porpoise".replace('p', 't') 2014 * returns "starring with a turtle tortoise" 2015 * "JonL".replace('q', 'x') returns "JonL" (no change) 2016 * </pre></blockquote> 2017 * 2018 * @param oldChar the old character. 2019 * @param newChar the new character. 2020 * @return a string derived from this string by replacing every 2021 * occurrence of <code>oldChar</code> with <code>newChar</code>. 2022 */ replace(char oldChar, char newChar)2023 public String replace(char oldChar, char newChar) { 2024 String replaced = this; 2025 if (oldChar != newChar) { 2026 for (int i = 0; i < count; ++i) { 2027 if (charAt(i) == oldChar) { 2028 if (replaced == this) { 2029 replaced = StringFactory.newStringFromString(this); 2030 } 2031 replaced.setCharAt(i, newChar); 2032 } 2033 } 2034 } 2035 return replaced; 2036 } 2037 2038 /** 2039 * Tells whether or not this string matches the given <a 2040 * href="../util/regex/Pattern.html#sum">regular expression</a>. 2041 * 2042 * <p> An invocation of this method of the form 2043 * <i>str</i><tt>.matches(</tt><i>regex</i><tt>)</tt> yields exactly the 2044 * same result as the expression 2045 * 2046 * <blockquote><tt> {@link java.util.regex.Pattern}.{@link 2047 * java.util.regex.Pattern#matches(String,CharSequence) 2048 * matches}(</tt><i>regex</i><tt>,</tt> <i>str</i><tt>)</tt></blockquote> 2049 * 2050 * @param regex 2051 * the regular expression to which this string is to be matched 2052 * 2053 * @return <tt>true</tt> if, and only if, this string matches the 2054 * given regular expression 2055 * 2056 * @throws PatternSyntaxException 2057 * if the regular expression's syntax is invalid 2058 * 2059 * @see java.util.regex.Pattern 2060 * 2061 * @since 1.4 2062 * @spec JSR-51 2063 */ matches(String regex)2064 public boolean matches(String regex) { 2065 return Pattern.matches(regex, this); 2066 } 2067 2068 /** 2069 * Returns true if and only if this string contains the specified 2070 * sequence of char values. 2071 * 2072 * @param s the sequence to search for 2073 * @return true if this string contains <code>s</code>, false otherwise 2074 * @throws NullPointerException if <code>s</code> is <code>null</code> 2075 * @since 1.5 2076 */ contains(CharSequence s)2077 public boolean contains(CharSequence s) { 2078 return indexOf(s.toString()) > -1; 2079 } 2080 2081 /** 2082 * Replaces the first substring of this string that matches the given <a 2083 * href="../util/regex/Pattern.html#sum">regular expression</a> with the 2084 * given replacement. 2085 * 2086 * <p> An invocation of this method of the form 2087 * <i>str</i><tt>.replaceFirst(</tt><i>regex</i><tt>,</tt> <i>repl</i><tt>)</tt> 2088 * yields exactly the same result as the expression 2089 * 2090 * <blockquote><tt> 2091 * {@link java.util.regex.Pattern}.{@link java.util.regex.Pattern#compile 2092 * compile}(</tt><i>regex</i><tt>).{@link 2093 * java.util.regex.Pattern#matcher(java.lang.CharSequence) 2094 * matcher}(</tt><i>str</i><tt>).{@link java.util.regex.Matcher#replaceFirst 2095 * replaceFirst}(</tt><i>repl</i><tt>)</tt></blockquote> 2096 * 2097 *<p> 2098 * Note that backslashes (<tt>\</tt>) and dollar signs (<tt>$</tt>) in the 2099 * replacement string may cause the results to be different than if it were 2100 * being treated as a literal replacement string; see 2101 * {@link java.util.regex.Matcher#replaceFirst}. 2102 * Use {@link java.util.regex.Matcher#quoteReplacement} to suppress the special 2103 * meaning of these characters, if desired. 2104 * 2105 * @param regex 2106 * the regular expression to which this string is to be matched 2107 * @param replacement 2108 * the string to be substituted for the first match 2109 * 2110 * @return The resulting <tt>String</tt> 2111 * 2112 * @throws PatternSyntaxException 2113 * if the regular expression's syntax is invalid 2114 * 2115 * @see java.util.regex.Pattern 2116 * 2117 * @since 1.4 2118 * @spec JSR-51 2119 */ replaceFirst(String regex, String replacement)2120 public String replaceFirst(String regex, String replacement) { 2121 return Pattern.compile(regex).matcher(this).replaceFirst(replacement); 2122 } 2123 2124 /** 2125 * Replaces each substring of this string that matches the given <a 2126 * href="../util/regex/Pattern.html#sum">regular expression</a> with the 2127 * given replacement. 2128 * 2129 * <p> An invocation of this method of the form 2130 * <i>str</i><tt>.replaceAll(</tt><i>regex</i><tt>,</tt> <i>repl</i><tt>)</tt> 2131 * yields exactly the same result as the expression 2132 * 2133 * <blockquote><tt> 2134 * {@link java.util.regex.Pattern}.{@link java.util.regex.Pattern#compile 2135 * compile}(</tt><i>regex</i><tt>).{@link 2136 * java.util.regex.Pattern#matcher(java.lang.CharSequence) 2137 * matcher}(</tt><i>str</i><tt>).{@link java.util.regex.Matcher#replaceAll 2138 * replaceAll}(</tt><i>repl</i><tt>)</tt></blockquote> 2139 * 2140 *<p> 2141 * Note that backslashes (<tt>\</tt>) and dollar signs (<tt>$</tt>) in the 2142 * replacement string may cause the results to be different than if it were 2143 * being treated as a literal replacement string; see 2144 * {@link java.util.regex.Matcher#replaceAll Matcher.replaceAll}. 2145 * Use {@link java.util.regex.Matcher#quoteReplacement} to suppress the special 2146 * meaning of these characters, if desired. 2147 * 2148 * @param regex 2149 * the regular expression to which this string is to be matched 2150 * @param replacement 2151 * the string to be substituted for each match 2152 * 2153 * @return The resulting <tt>String</tt> 2154 * 2155 * @throws PatternSyntaxException 2156 * if the regular expression's syntax is invalid 2157 * 2158 * @see java.util.regex.Pattern 2159 * 2160 * @since 1.4 2161 * @spec JSR-51 2162 */ replaceAll(String regex, String replacement)2163 public String replaceAll(String regex, String replacement) { 2164 return Pattern.compile(regex).matcher(this).replaceAll(replacement); 2165 } 2166 2167 /** 2168 * Replaces each substring of this string that matches the literal target 2169 * sequence with the specified literal replacement sequence. The 2170 * replacement proceeds from the beginning of the string to the end, for 2171 * example, replacing "aa" with "b" in the string "aaa" will result in 2172 * "ba" rather than "ab". 2173 * 2174 * @param target The sequence of char values to be replaced 2175 * @param replacement The replacement sequence of char values 2176 * @return The resulting string 2177 * @throws NullPointerException if <code>target</code> or 2178 * <code>replacement</code> is <code>null</code>. 2179 * @since 1.5 2180 */ replace(CharSequence target, CharSequence replacement)2181 public String replace(CharSequence target, CharSequence replacement) { 2182 if (target == null) { 2183 throw new NullPointerException("target == null"); 2184 } 2185 2186 if (replacement == null) { 2187 throw new NullPointerException("replacement == null"); 2188 } 2189 2190 String replacementStr = replacement.toString(); 2191 String targetStr = target.toString(); 2192 2193 // Special case when target == "". This is a pretty nonsensical transformation and nobody 2194 // should be hitting this. 2195 // 2196 // See commit 870b23b3febc85 and http://code.google.com/p/android/issues/detail?id=8807 2197 // An empty target is inserted at the start of the string, the end of the string and 2198 // between all characters. 2199 if (targetStr.isEmpty()) { 2200 // Note that overallocates by |replacement.size()| if |this| is the empty string, but 2201 // that should be a rare case within an already nonsensical case. 2202 StringBuilder sb = new StringBuilder(replacementStr.length() * (count + 2) + count); 2203 sb.append(replacementStr); 2204 for (int i = 0; i < count; ++i) { 2205 sb.append(charAt(i)); 2206 sb.append(replacementStr); 2207 } 2208 2209 return sb.toString(); 2210 } 2211 2212 // This is the "regular" case. 2213 int lastMatch = 0; 2214 StringBuilder sb = null; 2215 for (;;) { 2216 int currentMatch = indexOf(this, targetStr, lastMatch); 2217 if (currentMatch == -1) { 2218 break; 2219 } 2220 2221 if (sb == null) { 2222 sb = new StringBuilder(count); 2223 } 2224 2225 sb.append(this, lastMatch, currentMatch); 2226 sb.append(replacementStr); 2227 lastMatch = currentMatch + targetStr.count; 2228 } 2229 2230 if (sb != null) { 2231 sb.append(this, lastMatch, count); 2232 return sb.toString(); 2233 } else { 2234 return this; 2235 } 2236 } 2237 2238 /** 2239 * Splits this string around matches of the given 2240 * <a href="../util/regex/Pattern.html#sum">regular expression</a>. 2241 * 2242 * <p> The array returned by this method contains each substring of this 2243 * string that is terminated by another substring that matches the given 2244 * expression or is terminated by the end of the string. The substrings in 2245 * the array are in the order in which they occur in this string. If the 2246 * expression does not match any part of the input then the resulting array 2247 * has just one element, namely this string. 2248 * 2249 * <p> The <tt>limit</tt> parameter controls the number of times the 2250 * pattern is applied and therefore affects the length of the resulting 2251 * array. If the limit <i>n</i> is greater than zero then the pattern 2252 * will be applied at most <i>n</i> - 1 times, the array's 2253 * length will be no greater than <i>n</i>, and the array's last entry 2254 * will contain all input beyond the last matched delimiter. If <i>n</i> 2255 * is non-positive then the pattern will be applied as many times as 2256 * possible and the array can have any length. If <i>n</i> is zero then 2257 * the pattern will be applied as many times as possible, the array can 2258 * have any length, and trailing empty strings will be discarded. 2259 * 2260 * <p> The string <tt>"boo:and:foo"</tt>, for example, yields the 2261 * following results with these parameters: 2262 * 2263 * <blockquote><table cellpadding=1 cellspacing=0 summary="Split example showing regex, limit, and result"> 2264 * <tr> 2265 * <th>Regex</th> 2266 * <th>Limit</th> 2267 * <th>Result</th> 2268 * </tr> 2269 * <tr><td align=center>:</td> 2270 * <td align=center>2</td> 2271 * <td><tt>{ "boo", "and:foo" }</tt></td></tr> 2272 * <tr><td align=center>:</td> 2273 * <td align=center>5</td> 2274 * <td><tt>{ "boo", "and", "foo" }</tt></td></tr> 2275 * <tr><td align=center>:</td> 2276 * <td align=center>-2</td> 2277 * <td><tt>{ "boo", "and", "foo" }</tt></td></tr> 2278 * <tr><td align=center>o</td> 2279 * <td align=center>5</td> 2280 * <td><tt>{ "b", "", ":and:f", "", "" }</tt></td></tr> 2281 * <tr><td align=center>o</td> 2282 * <td align=center>-2</td> 2283 * <td><tt>{ "b", "", ":and:f", "", "" }</tt></td></tr> 2284 * <tr><td align=center>o</td> 2285 * <td align=center>0</td> 2286 * <td><tt>{ "b", "", ":and:f" }</tt></td></tr> 2287 * </table></blockquote> 2288 * 2289 * <p> An invocation of this method of the form 2290 * <i>str.</i><tt>split(</tt><i>regex</i><tt>,</tt> <i>n</i><tt>)</tt> 2291 * yields the same result as the expression 2292 * 2293 * <blockquote> 2294 * {@link java.util.regex.Pattern}.{@link java.util.regex.Pattern#compile 2295 * compile}<tt>(</tt><i>regex</i><tt>)</tt>.{@link 2296 * java.util.regex.Pattern#split(java.lang.CharSequence,int) 2297 * split}<tt>(</tt><i>str</i><tt>,</tt> <i>n</i><tt>)</tt> 2298 * </blockquote> 2299 * 2300 * 2301 * @param regex 2302 * the delimiting regular expression 2303 * 2304 * @param limit 2305 * the result threshold, as described above 2306 * 2307 * @return the array of strings computed by splitting this string 2308 * around matches of the given regular expression 2309 * 2310 * @throws PatternSyntaxException 2311 * if the regular expression's syntax is invalid 2312 * 2313 * @see java.util.regex.Pattern 2314 * 2315 * @since 1.4 2316 * @spec JSR-51 2317 */ split(String regex, int limit)2318 public String[] split(String regex, int limit) { 2319 // Try fast splitting without allocating Pattern object 2320 String[] fast = Pattern.fastSplit(regex, this, limit); 2321 if (fast != null) { 2322 return fast; 2323 } 2324 2325 return Pattern.compile(regex).split(this, limit); 2326 } 2327 2328 /** 2329 * Splits this string around matches of the given <a 2330 * href="../util/regex/Pattern.html#sum">regular expression</a>. 2331 * 2332 * <p> This method works as if by invoking the two-argument {@link 2333 * #split(String, int) split} method with the given expression and a limit 2334 * argument of zero. Trailing empty strings are therefore not included in 2335 * the resulting array. 2336 * 2337 * <p> The string <tt>"boo:and:foo"</tt>, for example, yields the following 2338 * results with these expressions: 2339 * 2340 * <blockquote><table cellpadding=1 cellspacing=0 summary="Split examples showing regex and result"> 2341 * <tr> 2342 * <th>Regex</th> 2343 * <th>Result</th> 2344 * </tr> 2345 * <tr><td align=center>:</td> 2346 * <td><tt>{ "boo", "and", "foo" }</tt></td></tr> 2347 * <tr><td align=center>o</td> 2348 * <td><tt>{ "b", "", ":and:f" }</tt></td></tr> 2349 * </table></blockquote> 2350 * 2351 * 2352 * @param regex 2353 * the delimiting regular expression 2354 * 2355 * @return the array of strings computed by splitting this string 2356 * around matches of the given regular expression 2357 * 2358 * @throws PatternSyntaxException 2359 * if the regular expression's syntax is invalid 2360 * 2361 * @see java.util.regex.Pattern 2362 * 2363 * @since 1.4 2364 * @spec JSR-51 2365 */ split(String regex)2366 public String[] split(String regex) { 2367 return split(regex, 0); 2368 } 2369 2370 /** 2371 * Converts all of the characters in this <code>String</code> to lower 2372 * case using the rules of the given <code>Locale</code>. Case mapping is based 2373 * on the Unicode Standard version specified by the {@link java.lang.Character Character} 2374 * class. Since case mappings are not always 1:1 char mappings, the resulting 2375 * <code>String</code> may be a different length than the original <code>String</code>. 2376 * <p> 2377 * Examples of lowercase mappings are in the following table: 2378 * <table border="1" summary="Lowercase mapping examples showing language code of locale, upper case, lower case, and description"> 2379 * <tr> 2380 * <th>Language Code of Locale</th> 2381 * <th>Upper Case</th> 2382 * <th>Lower Case</th> 2383 * <th>Description</th> 2384 * </tr> 2385 * <tr> 2386 * <td>tr (Turkish)</td> 2387 * <td>\u0130</td> 2388 * <td>\u0069</td> 2389 * <td>capital letter I with dot above -> small letter i</td> 2390 * </tr> 2391 * <tr> 2392 * <td>tr (Turkish)</td> 2393 * <td>\u0049</td> 2394 * <td>\u0131</td> 2395 * <td>capital letter I -> small letter dotless i </td> 2396 * </tr> 2397 * <tr> 2398 * <td>(all)</td> 2399 * <td>French Fries</td> 2400 * <td>french fries</td> 2401 * <td>lowercased all chars in String</td> 2402 * </tr> 2403 * <tr> 2404 * <td>(all)</td> 2405 * <td><img src="doc-files/capiota.gif" alt="capiota"><img src="doc-files/capchi.gif" alt="capchi"> 2406 * <img src="doc-files/captheta.gif" alt="captheta"><img src="doc-files/capupsil.gif" alt="capupsil"> 2407 * <img src="doc-files/capsigma.gif" alt="capsigma"></td> 2408 * <td><img src="doc-files/iota.gif" alt="iota"><img src="doc-files/chi.gif" alt="chi"> 2409 * <img src="doc-files/theta.gif" alt="theta"><img src="doc-files/upsilon.gif" alt="upsilon"> 2410 * <img src="doc-files/sigma1.gif" alt="sigma"></td> 2411 * <td>lowercased all chars in String</td> 2412 * </tr> 2413 * </table> 2414 * 2415 * @param locale use the case transformation rules for this locale 2416 * @return the <code>String</code>, converted to lowercase. 2417 * @see java.lang.String#toLowerCase() 2418 * @see java.lang.String#toUpperCase() 2419 * @see java.lang.String#toUpperCase(Locale) 2420 * @since 1.1 2421 */ toLowerCase(Locale locale)2422 public String toLowerCase(Locale locale) { 2423 return CaseMapper.toLowerCase(locale, this); 2424 } 2425 2426 /** 2427 * Converts all of the characters in this <code>String</code> to lower 2428 * case using the rules of the default locale. This is equivalent to calling 2429 * <code>toLowerCase(Locale.getDefault())</code>. 2430 * <p> 2431 * <b>Note:</b> This method is locale sensitive, and may produce unexpected 2432 * results if used for strings that are intended to be interpreted locale 2433 * independently. 2434 * Examples are programming language identifiers, protocol keys, and HTML 2435 * tags. 2436 * For instance, <code>"TITLE".toLowerCase()</code> in a Turkish locale 2437 * returns <code>"t\u005Cu0131tle"</code>, where '\u005Cu0131' is the 2438 * LATIN SMALL LETTER DOTLESS I character. 2439 * To obtain correct results for locale insensitive strings, use 2440 * <code>toLowerCase(Locale.ENGLISH)</code>. 2441 * <p> 2442 * @return the <code>String</code>, converted to lowercase. 2443 * @see java.lang.String#toLowerCase(Locale) 2444 */ toLowerCase()2445 public String toLowerCase() { 2446 return toLowerCase(Locale.getDefault()); 2447 } 2448 2449 /** 2450 * Converts all of the characters in this <code>String</code> to upper 2451 * case using the rules of the given <code>Locale</code>. Case mapping is based 2452 * on the Unicode Standard version specified by the {@link java.lang.Character Character} 2453 * class. Since case mappings are not always 1:1 char mappings, the resulting 2454 * <code>String</code> may be a different length than the original <code>String</code>. 2455 * <p> 2456 * Examples of locale-sensitive and 1:M case mappings are in the following table. 2457 * <p> 2458 * <table border="1" summary="Examples of locale-sensitive and 1:M case mappings. Shows Language code of locale, lower case, upper case, and description."> 2459 * <tr> 2460 * <th>Language Code of Locale</th> 2461 * <th>Lower Case</th> 2462 * <th>Upper Case</th> 2463 * <th>Description</th> 2464 * </tr> 2465 * <tr> 2466 * <td>tr (Turkish)</td> 2467 * <td>\u0069</td> 2468 * <td>\u0130</td> 2469 * <td>small letter i -> capital letter I with dot above</td> 2470 * </tr> 2471 * <tr> 2472 * <td>tr (Turkish)</td> 2473 * <td>\u0131</td> 2474 * <td>\u0049</td> 2475 * <td>small letter dotless i -> capital letter I</td> 2476 * </tr> 2477 * <tr> 2478 * <td>(all)</td> 2479 * <td>\u00df</td> 2480 * <td>\u0053 \u0053</td> 2481 * <td>small letter sharp s -> two letters: SS</td> 2482 * </tr> 2483 * <tr> 2484 * <td>(all)</td> 2485 * <td>Fahrvergnügen</td> 2486 * <td>FAHRVERGNÜGEN</td> 2487 * <td></td> 2488 * </tr> 2489 * </table> 2490 * @param locale use the case transformation rules for this locale 2491 * @return the <code>String</code>, converted to uppercase. 2492 * @see java.lang.String#toUpperCase() 2493 * @see java.lang.String#toLowerCase() 2494 * @see java.lang.String#toLowerCase(Locale) 2495 * @since 1.1 2496 */ toUpperCase(Locale locale)2497 public String toUpperCase(Locale locale) { 2498 return CaseMapper.toUpperCase(locale, this, count); 2499 } 2500 2501 /** 2502 * Converts all of the characters in this <code>String</code> to upper 2503 * case using the rules of the default locale. This method is equivalent to 2504 * <code>toUpperCase(Locale.getDefault())</code>. 2505 * <p> 2506 * <b>Note:</b> This method is locale sensitive, and may produce unexpected 2507 * results if used for strings that are intended to be interpreted locale 2508 * independently. 2509 * Examples are programming language identifiers, protocol keys, and HTML 2510 * tags. 2511 * For instance, <code>"title".toUpperCase()</code> in a Turkish locale 2512 * returns <code>"T\u005Cu0130TLE"</code>, where '\u005Cu0130' is the 2513 * LATIN CAPITAL LETTER I WITH DOT ABOVE character. 2514 * To obtain correct results for locale insensitive strings, use 2515 * <code>toUpperCase(Locale.ENGLISH)</code>. 2516 * <p> 2517 * @return the <code>String</code>, converted to uppercase. 2518 * @see java.lang.String#toUpperCase(Locale) 2519 */ toUpperCase()2520 public String toUpperCase() { 2521 return toUpperCase(Locale.getDefault()); 2522 } 2523 2524 /** 2525 * Returns a copy of the string, with leading and trailing whitespace 2526 * omitted. 2527 * <p> 2528 * If this <code>String</code> object represents an empty character 2529 * sequence, or the first and last characters of character sequence 2530 * represented by this <code>String</code> object both have codes 2531 * greater than <code>'\u0020'</code> (the space character), then a 2532 * reference to this <code>String</code> object is returned. 2533 * <p> 2534 * Otherwise, if there is no character with a code greater than 2535 * <code>'\u0020'</code> in the string, then a new 2536 * <code>String</code> object representing an empty string is created 2537 * and returned. 2538 * <p> 2539 * Otherwise, let <i>k</i> be the index of the first character in the 2540 * string whose code is greater than <code>'\u0020'</code>, and let 2541 * <i>m</i> be the index of the last character in the string whose code 2542 * is greater than <code>'\u0020'</code>. A new <code>String</code> 2543 * object is created, representing the substring of this string that 2544 * begins with the character at index <i>k</i> and ends with the 2545 * character at index <i>m</i>-that is, the result of 2546 * <code>this.substring(<i>k</i>, <i>m</i>+1)</code>. 2547 * <p> 2548 * This method may be used to trim whitespace (as defined above) from 2549 * the beginning and end of a string. 2550 * 2551 * @return A copy of this string with leading and trailing white 2552 * space removed, or this string if it has no leading or 2553 * trailing white space. 2554 */ trim()2555 public String trim() { 2556 int len = count; 2557 int st = 0; 2558 2559 while ((st < len) && (charAt(st) <= ' ')) { 2560 st++; 2561 } 2562 while ((st < len) && (charAt(len - 1) <= ' ')) { 2563 len--; 2564 } 2565 return ((st > 0) || (len < count)) ? substring(st, len) : this; 2566 } 2567 2568 /** 2569 * This object (which is already a string!) is itself returned. 2570 * 2571 * @return the string itself. 2572 */ toString()2573 public String toString() { 2574 return this; 2575 } 2576 2577 /** 2578 * Converts this string to a new character array. 2579 * 2580 * @return a newly allocated character array whose length is the length 2581 * of this string and whose contents are initialized to contain 2582 * the character sequence represented by this string. 2583 */ toCharArray()2584 public native char[] toCharArray(); 2585 2586 2587 /** 2588 * Returns a formatted string using the specified format string and 2589 * arguments. 2590 * 2591 * <p> The locale always used is the one returned by {@link 2592 * java.util.Locale#getDefault() Locale.getDefault()}. 2593 * 2594 * @param format 2595 * A <a href="../util/Formatter.html#syntax">format string</a> 2596 * 2597 * @param args 2598 * Arguments referenced by the format specifiers in the format 2599 * string. If there are more arguments than format specifiers, the 2600 * extra arguments are ignored. The number of arguments is 2601 * variable and may be zero. The maximum number of arguments is 2602 * limited by the maximum dimension of a Java array as defined by 2603 * <cite>The Java™ Virtual Machine Specification</cite>. 2604 * The behaviour on a 2605 * <tt>null</tt> argument depends on the <a 2606 * href="../util/Formatter.html#syntax">conversion</a>. 2607 * 2608 * @throws IllegalFormatException 2609 * If a format string contains an illegal syntax, a format 2610 * specifier that is incompatible with the given arguments, 2611 * insufficient arguments given the format string, or other 2612 * illegal conditions. For specification of all possible 2613 * formatting errors, see the <a 2614 * href="../util/Formatter.html#detail">Details</a> section of the 2615 * formatter class specification. 2616 * 2617 * @throws NullPointerException 2618 * If the <tt>format</tt> is <tt>null</tt> 2619 * 2620 * @return A formatted string 2621 * 2622 * @see java.util.Formatter 2623 * @since 1.5 2624 */ format(String format, Object... args)2625 public static String format(String format, Object... args) { 2626 return new Formatter().format(format, args).toString(); 2627 } 2628 2629 /** 2630 * Returns a formatted string using the specified locale, format string, 2631 * and arguments. 2632 * 2633 * @param l 2634 * The {@linkplain java.util.Locale locale} to apply during 2635 * formatting. If <tt>l</tt> is <tt>null</tt> then no localization 2636 * is applied. 2637 * 2638 * @param format 2639 * A <a href="../util/Formatter.html#syntax">format string</a> 2640 * 2641 * @param args 2642 * Arguments referenced by the format specifiers in the format 2643 * string. If there are more arguments than format specifiers, the 2644 * extra arguments are ignored. The number of arguments is 2645 * variable and may be zero. The maximum number of arguments is 2646 * limited by the maximum dimension of a Java array as defined by 2647 * <cite>The Java™ Virtual Machine Specification</cite>. 2648 * The behaviour on a 2649 * <tt>null</tt> argument depends on the <a 2650 * href="../util/Formatter.html#syntax">conversion</a>. 2651 * 2652 * @throws IllegalFormatException 2653 * If a format string contains an illegal syntax, a format 2654 * specifier that is incompatible with the given arguments, 2655 * insufficient arguments given the format string, or other 2656 * illegal conditions. For specification of all possible 2657 * formatting errors, see the <a 2658 * href="../util/Formatter.html#detail">Details</a> section of the 2659 * formatter class specification 2660 * 2661 * @throws NullPointerException 2662 * If the <tt>format</tt> is <tt>null</tt> 2663 * 2664 * @return A formatted string 2665 * 2666 * @see java.util.Formatter 2667 * @since 1.5 2668 */ format(Locale l, String format, Object... args)2669 public static String format(Locale l, String format, Object... args) { 2670 return new Formatter(l).format(format, args).toString(); 2671 } 2672 2673 /** 2674 * Returns the string representation of the <code>Object</code> argument. 2675 * 2676 * @param obj an <code>Object</code>. 2677 * @return if the argument is <code>null</code>, then a string equal to 2678 * <code>"null"</code>; otherwise, the value of 2679 * <code>obj.toString()</code> is returned. 2680 * @see java.lang.Object#toString() 2681 */ valueOf(Object obj)2682 public static String valueOf(Object obj) { 2683 return (obj == null) ? "null" : obj.toString(); 2684 } 2685 2686 /** 2687 * Returns the string representation of the <code>char</code> array 2688 * argument. The contents of the character array are copied; subsequent 2689 * modification of the character array does not affect the newly 2690 * created string. 2691 * 2692 * @param data a <code>char</code> array. 2693 * @return a newly allocated string representing the same sequence of 2694 * characters contained in the character array argument. 2695 */ valueOf(char data[])2696 public static String valueOf(char data[]) { 2697 return StringFactory.newStringFromChars(data); 2698 } 2699 2700 /** 2701 * Returns the string representation of a specific subarray of the 2702 * <code>char</code> array argument. 2703 * <p> 2704 * The <code>offset</code> argument is the index of the first 2705 * character of the subarray. The <code>count</code> argument 2706 * specifies the length of the subarray. The contents of the subarray 2707 * are copied; subsequent modification of the character array does not 2708 * affect the newly created string. 2709 * 2710 * @param data the character array. 2711 * @param offset the initial offset into the value of the 2712 * <code>String</code>. 2713 * @param count the length of the value of the <code>String</code>. 2714 * @return a string representing the sequence of characters contained 2715 * in the subarray of the character array argument. 2716 * @exception IndexOutOfBoundsException if <code>offset</code> is 2717 * negative, or <code>count</code> is negative, or 2718 * <code>offset+count</code> is larger than 2719 * <code>data.length</code>. 2720 */ valueOf(char data[], int offset, int count)2721 public static String valueOf(char data[], int offset, int count) { 2722 return StringFactory.newStringFromChars(data, offset, count); 2723 } 2724 2725 /** 2726 * Returns a String that represents the character sequence in the 2727 * array specified. 2728 * 2729 * @param data the character array. 2730 * @param offset initial offset of the subarray. 2731 * @param count length of the subarray. 2732 * @return a <code>String</code> that contains the characters of the 2733 * specified subarray of the character array. 2734 */ copyValueOf(char data[], int offset, int count)2735 public static String copyValueOf(char data[], int offset, int count) { 2736 // All public String constructors now copy the data. 2737 return StringFactory.newStringFromChars(data, offset, count); 2738 } 2739 2740 /** 2741 * Returns a String that represents the character sequence in the 2742 * array specified. 2743 * 2744 * @param data the character array. 2745 * @return a <code>String</code> that contains the characters of the 2746 * character array. 2747 */ copyValueOf(char data[])2748 public static String copyValueOf(char data[]) { 2749 return StringFactory.newStringFromChars(data); 2750 } 2751 2752 /** 2753 * Returns the string representation of the <code>boolean</code> argument. 2754 * 2755 * @param b a <code>boolean</code>. 2756 * @return if the argument is <code>true</code>, a string equal to 2757 * <code>"true"</code> is returned; otherwise, a string equal to 2758 * <code>"false"</code> is returned. 2759 */ valueOf(boolean b)2760 public static String valueOf(boolean b) { 2761 return b ? "true" : "false"; 2762 } 2763 2764 /** 2765 * Returns the string representation of the <code>char</code> 2766 * argument. 2767 * 2768 * @param c a <code>char</code>. 2769 * @return a string of length <code>1</code> containing 2770 * as its single character the argument <code>c</code>. 2771 */ valueOf(char c)2772 public static String valueOf(char c) { 2773 return StringFactory.newStringFromChars(0, 1, new char[] { c }); 2774 } 2775 2776 /** 2777 * Returns the string representation of the <code>int</code> argument. 2778 * <p> 2779 * The representation is exactly the one returned by the 2780 * <code>Integer.toString</code> method of one argument. 2781 * 2782 * @param i an <code>int</code>. 2783 * @return a string representation of the <code>int</code> argument. 2784 * @see java.lang.Integer#toString(int, int) 2785 */ valueOf(int i)2786 public static String valueOf(int i) { 2787 return Integer.toString(i); 2788 } 2789 2790 /** 2791 * Returns the string representation of the <code>long</code> argument. 2792 * <p> 2793 * The representation is exactly the one returned by the 2794 * <code>Long.toString</code> method of one argument. 2795 * 2796 * @param l a <code>long</code>. 2797 * @return a string representation of the <code>long</code> argument. 2798 * @see java.lang.Long#toString(long) 2799 */ valueOf(long l)2800 public static String valueOf(long l) { 2801 return Long.toString(l); 2802 } 2803 2804 /** 2805 * Returns the string representation of the <code>float</code> argument. 2806 * <p> 2807 * The representation is exactly the one returned by the 2808 * <code>Float.toString</code> method of one argument. 2809 * 2810 * @param f a <code>float</code>. 2811 * @return a string representation of the <code>float</code> argument. 2812 * @see java.lang.Float#toString(float) 2813 */ valueOf(float f)2814 public static String valueOf(float f) { 2815 return Float.toString(f); 2816 } 2817 2818 /** 2819 * Returns the string representation of the <code>double</code> argument. 2820 * <p> 2821 * The representation is exactly the one returned by the 2822 * <code>Double.toString</code> method of one argument. 2823 * 2824 * @param d a <code>double</code>. 2825 * @return a string representation of the <code>double</code> argument. 2826 * @see java.lang.Double#toString(double) 2827 */ valueOf(double d)2828 public static String valueOf(double d) { 2829 return Double.toString(d); 2830 } 2831 2832 /** 2833 * Returns a canonical representation for the string object. 2834 * <p> 2835 * A pool of strings, initially empty, is maintained privately by the 2836 * class <code>String</code>. 2837 * <p> 2838 * When the intern method is invoked, if the pool already contains a 2839 * string equal to this <code>String</code> object as determined by 2840 * the {@link #equals(Object)} method, then the string from the pool is 2841 * returned. Otherwise, this <code>String</code> object is added to the 2842 * pool and a reference to this <code>String</code> object is returned. 2843 * <p> 2844 * It follows that for any two strings <code>s</code> and <code>t</code>, 2845 * <code>s.intern() == t.intern()</code> is <code>true</code> 2846 * if and only if <code>s.equals(t)</code> is <code>true</code>. 2847 * <p> 2848 * All literal strings and string-valued constant expressions are 2849 * interned. String literals are defined in section 3.10.5 of the 2850 * <cite>The Java™ Language Specification</cite>. 2851 * 2852 * @return a string that has the same contents as this string, but is 2853 * guaranteed to be from a pool of unique strings. 2854 */ intern()2855 public native String intern(); 2856 } 2857