638 * toString} method is likely to run faster and is generally preferred.
639 *
640 * @param builder
641 * A {@code StringBuilder}
642 *
643 * @since 1.5
644 */
645 public String(StringBuilder builder) {
646 this(builder, null);
647 }
648
649 /**
650 * Returns the length of this string.
651 * The length is equal to the number of <a href="Character.html#unicode">Unicode
652 * code units</a> in the string.
653 *
654 * @return the length of the sequence of characters represented by this
655 * object.
656 */
657 public int length() {
658 return value.length >> coder();
659 }
660
661 /**
662 * Returns {@code true} if, and only if, {@link #length()} is {@code 0}.
663 *
664 * @return {@code true} if {@link #length()} is {@code 0}, otherwise
665 * {@code false}
666 *
667 * @since 1.6
668 */
669 public boolean isEmpty() {
670 return value.length == 0;
671 }
672
673 /**
674 * Returns the {@code char} value at the
675 * specified index. An index ranges from {@code 0} to
676 * {@code length() - 1}. The first {@code char} value of the sequence
677 * is at index {@code 0}, the next at index {@code 1},
678 * and so on, as for array indexing.
1917 * Concatenates the specified string to the end of this string.
1918 * <p>
1919 * If the length of the argument string is {@code 0}, then this
1920 * {@code String} object is returned. Otherwise, a
1921 * {@code String} object is returned that represents a character
1922 * sequence that is the concatenation of the character sequence
1923 * represented by this {@code String} object and the character
1924 * sequence represented by the argument string.<p>
1925 * Examples:
1926 * <blockquote><pre>
1927 * "cares".concat("s") returns "caress"
1928 * "to".concat("get").concat("her") returns "together"
1929 * </pre></blockquote>
1930 *
1931 * @param str the {@code String} that is concatenated to the end
1932 * of this {@code String}.
1933 * @return a string that represents the concatenation of this object's
1934 * characters followed by the string argument's characters.
1935 */
1936 public String concat(String str) {
1937 int olen = str.length();
1938 if (olen == 0) {
1939 return this;
1940 }
1941 if (coder() == str.coder()) {
1942 byte[] val = this.value;
1943 byte[] oval = str.value;
1944 int len = val.length + oval.length;
1945 byte[] buf = Arrays.copyOf(val, len);
1946 System.arraycopy(oval, 0, buf, val.length, oval.length);
1947 return new String(buf, coder);
1948 }
1949 int len = length();
1950 byte[] buf = StringUTF16.newBytesFor(len + olen);
1951 getBytes(buf, 0, UTF16);
1952 str.getBytes(buf, len, UTF16);
1953 return new String(buf, UTF16);
1954 }
1955
1956 /**
1957 * Returns a string resulting from replacing all occurrences of
1958 * {@code oldChar} in this string with {@code newChar}.
1959 * <p>
1960 * If the character {@code oldChar} does not occur in the
1961 * character sequence represented by this {@code String} object,
1962 * then a reference to this {@code String} object is returned.
1963 * Otherwise, a {@code String} object is returned that
1964 * represents a character sequence identical to the character sequence
1965 * represented by this {@code String} object, except that every
1966 * occurrence of {@code oldChar} is replaced by an occurrence
1967 * of {@code newChar}.
1968 * <p>
1969 * Examples:
2290 off = next + 1;
2291 } else { // last one
2292 //assert (list.size() == limit - 1);
2293 int last = length();
2294 list.add(substring(off, last));
2295 off = last;
2296 break;
2297 }
2298 }
2299 // If no match was found, return this
2300 if (off == 0)
2301 return new String[]{this};
2302
2303 // Add remaining segment
2304 if (!limited || list.size() < limit)
2305 list.add(substring(off, length()));
2306
2307 // Construct result
2308 int resultSize = list.size();
2309 if (limit == 0) {
2310 while (resultSize > 0 && list.get(resultSize - 1).length() == 0) {
2311 resultSize--;
2312 }
2313 }
2314 String[] result = new String[resultSize];
2315 return list.subList(0, resultSize).toArray(result);
2316 }
2317 return Pattern.compile(regex).split(this, limit);
2318 }
2319
2320 /**
2321 * Splits this string around matches of the given <a
2322 * href="../util/regex/Pattern.html#sum">regular expression</a>.
2323 *
2324 * <p> This method works as if by invoking the two-argument {@link
2325 * #split(String, int) split} method with the given expression and a limit
2326 * argument of zero. Trailing empty strings are therefore not included in
2327 * the resulting array.
2328 *
2329 * <p> The string {@code "boo:and:foo"}, for example, yields the following
2330 * results with these expressions:
|
638 * toString} method is likely to run faster and is generally preferred.
639 *
640 * @param builder
641 * A {@code StringBuilder}
642 *
643 * @since 1.5
644 */
645 public String(StringBuilder builder) {
646 this(builder, null);
647 }
648
649 /**
650 * Returns the length of this string.
651 * The length is equal to the number of <a href="Character.html#unicode">Unicode
652 * code units</a> in the string.
653 *
654 * @return the length of the sequence of characters represented by this
655 * object.
656 */
657 public int length() {
658 return isLatin1() ? value.length : value.length >> UTF16;
659 }
660
661 /**
662 * Returns {@code true} if, and only if, {@link #length()} is {@code 0}.
663 *
664 * @return {@code true} if {@link #length()} is {@code 0}, otherwise
665 * {@code false}
666 *
667 * @since 1.6
668 */
669 public boolean isEmpty() {
670 return value.length == 0;
671 }
672
673 /**
674 * Returns the {@code char} value at the
675 * specified index. An index ranges from {@code 0} to
676 * {@code length() - 1}. The first {@code char} value of the sequence
677 * is at index {@code 0}, the next at index {@code 1},
678 * and so on, as for array indexing.
1917 * Concatenates the specified string to the end of this string.
1918 * <p>
1919 * If the length of the argument string is {@code 0}, then this
1920 * {@code String} object is returned. Otherwise, a
1921 * {@code String} object is returned that represents a character
1922 * sequence that is the concatenation of the character sequence
1923 * represented by this {@code String} object and the character
1924 * sequence represented by the argument string.<p>
1925 * Examples:
1926 * <blockquote><pre>
1927 * "cares".concat("s") returns "caress"
1928 * "to".concat("get").concat("her") returns "together"
1929 * </pre></blockquote>
1930 *
1931 * @param str the {@code String} that is concatenated to the end
1932 * of this {@code String}.
1933 * @return a string that represents the concatenation of this object's
1934 * characters followed by the string argument's characters.
1935 */
1936 public String concat(String str) {
1937 if (str.isEmpty()) {
1938 return this;
1939 }
1940 if (coder() == str.coder()) {
1941 byte[] val = this.value;
1942 byte[] oval = str.value;
1943 int len = val.length + oval.length;
1944 byte[] buf = Arrays.copyOf(val, len);
1945 System.arraycopy(oval, 0, buf, val.length, oval.length);
1946 return new String(buf, coder);
1947 }
1948 int len = length();
1949 int olen = str.length();
1950 byte[] buf = StringUTF16.newBytesFor(len + olen);
1951 getBytes(buf, 0, UTF16);
1952 str.getBytes(buf, len, UTF16);
1953 return new String(buf, UTF16);
1954 }
1955
1956 /**
1957 * Returns a string resulting from replacing all occurrences of
1958 * {@code oldChar} in this string with {@code newChar}.
1959 * <p>
1960 * If the character {@code oldChar} does not occur in the
1961 * character sequence represented by this {@code String} object,
1962 * then a reference to this {@code String} object is returned.
1963 * Otherwise, a {@code String} object is returned that
1964 * represents a character sequence identical to the character sequence
1965 * represented by this {@code String} object, except that every
1966 * occurrence of {@code oldChar} is replaced by an occurrence
1967 * of {@code newChar}.
1968 * <p>
1969 * Examples:
2290 off = next + 1;
2291 } else { // last one
2292 //assert (list.size() == limit - 1);
2293 int last = length();
2294 list.add(substring(off, last));
2295 off = last;
2296 break;
2297 }
2298 }
2299 // If no match was found, return this
2300 if (off == 0)
2301 return new String[]{this};
2302
2303 // Add remaining segment
2304 if (!limited || list.size() < limit)
2305 list.add(substring(off, length()));
2306
2307 // Construct result
2308 int resultSize = list.size();
2309 if (limit == 0) {
2310 while (resultSize > 0 && list.get(resultSize - 1).isEmpty()) {
2311 resultSize--;
2312 }
2313 }
2314 String[] result = new String[resultSize];
2315 return list.subList(0, resultSize).toArray(result);
2316 }
2317 return Pattern.compile(regex).split(this, limit);
2318 }
2319
2320 /**
2321 * Splits this string around matches of the given <a
2322 * href="../util/regex/Pattern.html#sum">regular expression</a>.
2323 *
2324 * <p> This method works as if by invoking the two-argument {@link
2325 * #split(String, int) split} method with the given expression and a limit
2326 * argument of zero. Trailing empty strings are therefore not included in
2327 * the resulting array.
2328 *
2329 * <p> The string {@code "boo:and:foo"}, for example, yields the following
2330 * results with these expressions:
|