< prev index next >

src/java.base/share/classes/java/lang/String.java

Print this page
rev 52979 : 8215281: Use String.isEmpty() when applicable in java.base
Reviewed-by: TBD


 647      * toString} method is likely to run faster and is generally preferred.
 648      *
 649      * @param   builder
 650      *          A {@code StringBuilder}
 651      *
 652      * @since  1.5
 653      */
 654     public String(StringBuilder builder) {
 655         this(builder, null);
 656     }
 657 
 658     /**
 659      * Returns the length of this string.
 660      * The length is equal to the number of <a href="Character.html#unicode">Unicode
 661      * code units</a> in the string.
 662      *
 663      * @return  the length of the sequence of characters represented by this
 664      *          object.
 665      */
 666     public int length() {
 667         return value.length >> coder();
 668     }
 669 
 670     /**
 671      * Returns {@code true} if, and only if, {@link #length()} is {@code 0}.
 672      *
 673      * @return {@code true} if {@link #length()} is {@code 0}, otherwise
 674      * {@code false}
 675      *
 676      * @since 1.6
 677      */
 678     public boolean isEmpty() {
 679         return value.length == 0;
 680     }
 681 
 682     /**
 683      * Returns the {@code char} value at the
 684      * specified index. An index ranges from {@code 0} to
 685      * {@code length() - 1}. The first {@code char} value of the sequence
 686      * is at index {@code 0}, the next at index {@code 1},
 687      * and so on, as for array indexing.


1926      * Concatenates the specified string to the end of this string.
1927      * <p>
1928      * If the length of the argument string is {@code 0}, then this
1929      * {@code String} object is returned. Otherwise, a
1930      * {@code String} object is returned that represents a character
1931      * sequence that is the concatenation of the character sequence
1932      * represented by this {@code String} object and the character
1933      * sequence represented by the argument string.<p>
1934      * Examples:
1935      * <blockquote><pre>
1936      * "cares".concat("s") returns "caress"
1937      * "to".concat("get").concat("her") returns "together"
1938      * </pre></blockquote>
1939      *
1940      * @param   str   the {@code String} that is concatenated to the end
1941      *                of this {@code String}.
1942      * @return  a string that represents the concatenation of this object's
1943      *          characters followed by the string argument's characters.
1944      */
1945     public String concat(String str) {
1946         int olen = str.length();
1947         if (olen == 0) {
1948             return this;
1949         }
1950         if (coder() == str.coder()) {
1951             byte[] val = this.value;
1952             byte[] oval = str.value;
1953             int len = val.length + oval.length;
1954             byte[] buf = Arrays.copyOf(val, len);
1955             System.arraycopy(oval, 0, buf, val.length, oval.length);
1956             return new String(buf, coder);
1957         }
1958         int len = length();

1959         byte[] buf = StringUTF16.newBytesFor(len + olen);
1960         getBytes(buf, 0, UTF16);
1961         str.getBytes(buf, len, UTF16);
1962         return new String(buf, UTF16);
1963     }
1964 
1965     /**
1966      * Returns a string resulting from replacing all occurrences of
1967      * {@code oldChar} in this string with {@code newChar}.
1968      * <p>
1969      * If the character {@code oldChar} does not occur in the
1970      * character sequence represented by this {@code String} object,
1971      * then a reference to this {@code String} object is returned.
1972      * Otherwise, a {@code String} object is returned that
1973      * represents a character sequence identical to the character sequence
1974      * represented by this {@code String} object, except that every
1975      * occurrence of {@code oldChar} is replaced by an occurrence
1976      * of {@code newChar}.
1977      * <p>
1978      * Examples:


2299                     off = next + 1;
2300                 } else {    // last one
2301                     //assert (list.size() == limit - 1);
2302                     int last = length();
2303                     list.add(substring(off, last));
2304                     off = last;
2305                     break;
2306                 }
2307             }
2308             // If no match was found, return this
2309             if (off == 0)
2310                 return new String[]{this};
2311 
2312             // Add remaining segment
2313             if (!limited || list.size() < limit)
2314                 list.add(substring(off, length()));
2315 
2316             // Construct result
2317             int resultSize = list.size();
2318             if (limit == 0) {
2319                 while (resultSize > 0 && list.get(resultSize - 1).length() == 0) {
2320                     resultSize--;
2321                 }
2322             }
2323             String[] result = new String[resultSize];
2324             return list.subList(0, resultSize).toArray(result);
2325         }
2326         return Pattern.compile(regex).split(this, limit);
2327     }
2328 
2329     /**
2330      * Splits this string around matches of the given <a
2331      * href="../util/regex/Pattern.html#sum">regular expression</a>.
2332      *
2333      * <p> This method works as if by invoking the two-argument {@link
2334      * #split(String, int) split} method with the given expression and a limit
2335      * argument of zero.  Trailing empty strings are therefore not included in
2336      * the resulting array.
2337      *
2338      * <p> The string {@code "boo:and:foo"}, for example, yields the following
2339      * results with these expressions:




 647      * toString} method is likely to run faster and is generally preferred.
 648      *
 649      * @param   builder
 650      *          A {@code StringBuilder}
 651      *
 652      * @since  1.5
 653      */
 654     public String(StringBuilder builder) {
 655         this(builder, null);
 656     }
 657 
 658     /**
 659      * Returns the length of this string.
 660      * The length is equal to the number of <a href="Character.html#unicode">Unicode
 661      * code units</a> in the string.
 662      *
 663      * @return  the length of the sequence of characters represented by this
 664      *          object.
 665      */
 666     public int length() {
 667         return isLatin1() ? value.length : value.length >> UTF16;
 668     }
 669 
 670     /**
 671      * Returns {@code true} if, and only if, {@link #length()} is {@code 0}.
 672      *
 673      * @return {@code true} if {@link #length()} is {@code 0}, otherwise
 674      * {@code false}
 675      *
 676      * @since 1.6
 677      */
 678     public boolean isEmpty() {
 679         return value.length == 0;
 680     }
 681 
 682     /**
 683      * Returns the {@code char} value at the
 684      * specified index. An index ranges from {@code 0} to
 685      * {@code length() - 1}. The first {@code char} value of the sequence
 686      * is at index {@code 0}, the next at index {@code 1},
 687      * and so on, as for array indexing.


1926      * Concatenates the specified string to the end of this string.
1927      * <p>
1928      * If the length of the argument string is {@code 0}, then this
1929      * {@code String} object is returned. Otherwise, a
1930      * {@code String} object is returned that represents a character
1931      * sequence that is the concatenation of the character sequence
1932      * represented by this {@code String} object and the character
1933      * sequence represented by the argument string.<p>
1934      * Examples:
1935      * <blockquote><pre>
1936      * "cares".concat("s") returns "caress"
1937      * "to".concat("get").concat("her") returns "together"
1938      * </pre></blockquote>
1939      *
1940      * @param   str   the {@code String} that is concatenated to the end
1941      *                of this {@code String}.
1942      * @return  a string that represents the concatenation of this object's
1943      *          characters followed by the string argument's characters.
1944      */
1945     public String concat(String str) {
1946         if (str.isEmpty()) {

1947             return this;
1948         }
1949         if (coder() == str.coder()) {
1950             byte[] val = this.value;
1951             byte[] oval = str.value;
1952             int len = val.length + oval.length;
1953             byte[] buf = Arrays.copyOf(val, len);
1954             System.arraycopy(oval, 0, buf, val.length, oval.length);
1955             return new String(buf, coder);
1956         }
1957         int len = length();
1958         int olen = str.length();
1959         byte[] buf = StringUTF16.newBytesFor(len + olen);
1960         getBytes(buf, 0, UTF16);
1961         str.getBytes(buf, len, UTF16);
1962         return new String(buf, UTF16);
1963     }
1964 
1965     /**
1966      * Returns a string resulting from replacing all occurrences of
1967      * {@code oldChar} in this string with {@code newChar}.
1968      * <p>
1969      * If the character {@code oldChar} does not occur in the
1970      * character sequence represented by this {@code String} object,
1971      * then a reference to this {@code String} object is returned.
1972      * Otherwise, a {@code String} object is returned that
1973      * represents a character sequence identical to the character sequence
1974      * represented by this {@code String} object, except that every
1975      * occurrence of {@code oldChar} is replaced by an occurrence
1976      * of {@code newChar}.
1977      * <p>
1978      * Examples:


2299                     off = next + 1;
2300                 } else {    // last one
2301                     //assert (list.size() == limit - 1);
2302                     int last = length();
2303                     list.add(substring(off, last));
2304                     off = last;
2305                     break;
2306                 }
2307             }
2308             // If no match was found, return this
2309             if (off == 0)
2310                 return new String[]{this};
2311 
2312             // Add remaining segment
2313             if (!limited || list.size() < limit)
2314                 list.add(substring(off, length()));
2315 
2316             // Construct result
2317             int resultSize = list.size();
2318             if (limit == 0) {
2319                 while (resultSize > 0 && list.get(resultSize - 1).isEmpty()) {
2320                     resultSize--;
2321                 }
2322             }
2323             String[] result = new String[resultSize];
2324             return list.subList(0, resultSize).toArray(result);
2325         }
2326         return Pattern.compile(regex).split(this, limit);
2327     }
2328 
2329     /**
2330      * Splits this string around matches of the given <a
2331      * href="../util/regex/Pattern.html#sum">regular expression</a>.
2332      *
2333      * <p> This method works as if by invoking the two-argument {@link
2334      * #split(String, int) split} method with the given expression and a limit
2335      * argument of zero.  Trailing empty strings are therefore not included in
2336      * the resulting array.
2337      *
2338      * <p> The string {@code "boo:and:foo"}, for example, yields the following
2339      * results with these expressions:


< prev index next >