FileDocCategorySizeDatePackage
String.javaAPI DocphoneME MR2 API (J2ME)55269Wed May 02 17:59:56 BST 2007java.lang

String

public final class String extends Object
The String class represents character strings. All string literals in Java programs, such as "abc", are implemented as instances of this class.

Strings are constant; their values cannot be changed after they are created. String buffers support mutable strings. Because String objects are immutable they can be shared. For example:

String str = "abc";

is equivalent to:

char data[] = {'a', 'b', 'c'};
String str = new String(data);

Here are some more examples of how strings can be used:

System.out.println("abc");
String cde = "cde";
System.out.println("abc" + cde);
String c = "abc".substring(2,3);
String d = cde.substring(1, 2);

The class String includes methods for examining individual characters of the sequence, for comparing strings, for searching strings, for extracting substrings, and for creating a copy of a string with all characters translated to uppercase or to lowercase.

The Java language provides special support for the string concatenation operator ( + ), and for conversion of other objects to strings. String concatenation is implemented through the StringBuffer class and its append method. String conversions are implemented through the method toString, defined by Object and inherited by all classes in Java. For additional information on string concatenation and conversion, see The Java Language Specification.

version
12/17/01 (CLDC 1.1)
see
java.lang.Object#toString()
see
java.lang.StringBuffer
see
java.lang.StringBuffer#append(boolean)
see
java.lang.StringBuffer#append(char)
see
java.lang.StringBuffer#append(char[])
see
java.lang.StringBuffer#append(char[], int, int)
see
java.lang.StringBuffer#append(int)
see
java.lang.StringBuffer#append(long)
see
java.lang.StringBuffer#append(java.lang.Object)
see
java.lang.StringBuffer#append(java.lang.String)
since
JDK1.0, CLDC 1.0

Fields Summary
private char[]
value
The value is used for character storage.
private int
offset
The offset is the first index of the storage that is used.
private int
count
The count is the number of characters in the String.
Constructors Summary
public String()
Initializes a newly created String object so that it represents an empty character sequence.

        value = new char[0];
    
String(int offset, int count, char[] value)

        this.value = value;
        this.offset = offset;
        this.count = count;
    
public String(String value)
Initializes a newly created String object so that it represents the same sequence of characters as the argument; in other words, the newly created string is a copy of the argument string.

param
value a String.

        count = value.length();
        this.value = new char[count];
        value.getChars(0, count, this.value, 0);
    
public String(char[] value)
Allocates a new String so that it represents the sequence of characters currently contained in the character array argument. The contents of the character array are copied; subsequent modification of the character array does not affect the newly created string.

param
value the initial value of the string.
throws
NullPointerException if value is null.

        this.count = value.length;
        this.value = new char[count];
        JVM.unchecked_char_arraycopy(value, 0, this.value, 0, count);
    
public String(char[] value, int offset, int count)
Allocates a new String that contains characters from a subarray of the character array argument. The offset argument is the index of the first character of the subarray and the count argument specifies the length of the subarray. The contents of the subarray are copied; subsequent modification of the character array does not affect the newly created string.

param
value array that is the source of characters.
param
offset the initial offset.
param
count the length.
exception
IndexOutOfBoundsException if the offset and count arguments index characters outside the bounds of the value array.
exception
NullPointerException if value is null.

        if (offset < 0) {
            throw new StringIndexOutOfBoundsException(
/* #ifdef VERBOSE_EXCEPTIONS */
/// skipped                       offset
/* #endif */
            );
        }
        if (count < 0) {
            throw new StringIndexOutOfBoundsException(
/* #ifdef VERBOSE_EXCEPTIONS */
/// skipped                       count
/* #endif */
            );
        }
        // Note: offset or count might be near -1>>>1.
        if (offset > value.length - count) {
            throw new StringIndexOutOfBoundsException(
/* #ifdef VERBOSE_EXCEPTIONS */
/// skipped                       offset + count
/* #endif */
            );
        }

        this.value = new char[count];
        this.count = count;
        JVM.unchecked_char_arraycopy(value, offset, this.value, 0, count);
    
public String(byte[] bytes, int off, int len, String enc)
Construct a new String by converting the specified subarray of bytes using the specified character encoding. The length of the new String is a function of the encoding, and hence may not be equal to the length of the subarray.

param
bytes The bytes to be converted into characters
param
off Index of the first byte to convert
param
len Number of bytes to convert
param
enc The name of a character encoding
exception
UnsupportedEncodingException If the named encoding is not supported
since
JDK1.1

        this(Helper.byteToCharArray(bytes, off, len, enc));
    
public String(byte[] bytes, String enc)
Construct a new String by converting the specified array of bytes using the specified character encoding. The length of the new String is a function of the encoding, and hence may not be equal to the length of the byte array.

param
bytes The bytes to be converted into characters
param
enc The name of a supported character encoding
exception
UnsupportedEncodingException If the named encoding is not supported
since
JDK1.1

        this(bytes, 0, bytes.length, enc);
    
public String(byte[] bytes, int off, int len)
Construct a new String by converting the specified subarray of bytes using the platform's default character encoding. The length of the new String is a function of the encoding, and hence may not be equal to the length of the subarray.

param
bytes The bytes to be converted into characters
param
off Index of the first byte to convert
param
len Number of bytes to convert
since
JDK1.1

        this(Helper.byteToCharArray(bytes, off, len));
    
public String(byte[] bytes)
Construct a new String by converting the specified array of bytes using the platform's default character encoding. The length of the new String is a function of the encoding, and hence may not be equal to the length of the byte array.

param
bytes The bytes to be converted into characters
since
JDK1.1

        this(bytes, 0, bytes.length);
    
public String(StringBuffer buffer)
Allocates a new string that contains the sequence of characters currently contained in the string buffer argument. The contents of the string buffer are copied; subsequent modification of the string buffer does not affect the newly created string.

param
buffer a StringBuffer.
throws
NullPointerException If buffer is null.

        synchronized(buffer) {
            buffer.setShared();
            this.value = buffer.getValue();
            this.offset = 0;
            this.count = buffer.length();
        }
    
Methods Summary
public charcharAt(int index)
Returns the character at the specified index. An index ranges from 0 to length() - 1. The first character of the sequence is at index 0, the next at index 1, and so on, as for array indexing.

param
index the index of the character.
return
the character at the specified index of this string. The first character is at index 0.
exception
IndexOutOfBoundsException if the index argument is negative or not less than the length of this string.

        if ((index < 0) || (index >= count)) {
            throw new StringIndexOutOfBoundsException(
/* #ifdef VERBOSE_EXCEPTIONS */
/// skipped                       index
/* #endif */
            );
        }
        return value[index + offset];
    
public intcompareTo(java.lang.String anotherString)
Compares two strings lexicographically. The comparison is based on the Unicode value of each character in the strings. The character sequence represented by this String object is compared lexicographically to the character sequence represented by the argument string. The result is a negative integer if this String object lexicographically precedes the argument string. The result is a positive integer if this String object lexicographically follows the argument string. The result is zero if the strings are equal; compareTo returns 0 exactly when the {@link #equals(Object)} method would return true.

This is the definition of lexicographic ordering. If two strings are different, then either they have different characters at some index that is a valid index for both strings, or their lengths are different, or both. If they have different characters at one or more index positions, let k be the smallest such index; then the string whose character at position k has the smaller value, as determined by using the < operator, lexicographically precedes the other string. In this case, compareTo returns the difference of the two character values at position k in the two string -- that is, the value:

this.charAt(k)-anotherString.charAt(k)
If there is no index position at which they differ, then the shorter string lexicographically precedes the longer string. In this case, compareTo returns the difference of the lengths of the strings -- that is, the value:
this.length()-anotherString.length()

param
anotherString the String to be compared.
return
the value 0 if the argument string is equal to this string; a value less than 0 if this string is lexicographically less than the string argument; and a value greater than 0 if this string is lexicographically greater than the string argument.
exception
java.lang.NullPointerException if anotherString is null.

        int len1 = count;
        int len2 = anotherString.count;
        int n = Math.min(len1, len2);
        char v1[] = value;
        char v2[] = anotherString.value;
        int i = offset;
        int j = anotherString.offset;

        if (i == j) {
            int k = i;
            int lim = n + i;
            while (k < lim) {
                char c1 = v1[k];
                char c2 = v2[k];
                if (c1 != c2) {
                    return c1 - c2;
                }
                k++;
           }
        } else {
            while (n-- != 0) {
                char c1 = v1[i++];
                char c2 = v2[j++];
                if (c1 != c2) {
                    return c1 - c2;
                }
            }
        }
        return len1 - len2;
    
public java.lang.Stringconcat(java.lang.String str)
Concatenates the specified string to the end of this string.

If the length of the argument string is 0, then this String object is returned. Otherwise, a new String object is created, representing a character sequence that is the concatenation of the character sequence represented by this String object and the character sequence represented by the argument string.

Examples:

"cares".concat("s") returns "caress"
"to".concat("get").concat("her") returns "together"

param
str the String that is concatenated to the end of this String.
return
a string that represents the concatenation of this object's characters followed by the string argument's characters.
exception
java.lang.NullPointerException if str is null.

        int otherLen = str.length();
        if (otherLen == 0) {
            return this;
        }
        char buf[] = new char[count + otherLen];
        getChars(0, count, buf, 0);
        str.getChars(0, otherLen, buf, count);
        return new String(0, count + otherLen, buf);
    
public booleanendsWith(java.lang.String suffix)
Tests if this string ends with the specified suffix.

param
suffix the suffix.
return
true if the character sequence represented by the argument is a suffix of the character sequence represented by this object; false otherwise. Note that the result will be true if the argument is the empty string or is equal to this String object as determined by the {@link #equals(Object)} method.
exception
java.lang.NullPointerException if suffix is null.

        return startsWith(suffix, count - suffix.count);
    
public booleanequals(java.lang.Object anObject)
Compares this string to the specified object. The result is true if and only if the argument is not null and is a String object that represents the same sequence of characters as this object.

param
anObject the object to compare this String against.
return
true if the String are equal; false otherwise.
see
java.lang.String#compareTo(java.lang.String)
see
java.lang.String#equalsIgnoreCase(java.lang.String)

        if (this == anObject) {
            return true;
        }
        if (anObject instanceof String) {
            String anotherString = (String)anObject;
            int n = count;
            if (n == anotherString.count) {
                char v1[] = value;
                char v2[] = anotherString.value;
                int i = offset;
                int j = anotherString.offset;
                while (n-- != 0) {
                    if (v1[i++] != v2[j++]) {
                        return false;
                    }
                }
                return true;
            }
        }
        return false;
    
public booleanequalsIgnoreCase(java.lang.String anotherString)
Compares this String to another String, ignoring case considerations. Two strings are considered equal ignoring case if they are of the same length, and corresponding characters in the two strings are equal ignoring case.

Two characters c1 and c2 are considered the same, ignoring case if at least one of the following is true:

  • The two characters are the same (as compared by the == operator).
  • Applying the method {@link java.lang.Character#toUpperCase(char)} to each character produces the same result.
  • Applying the method {@link java.lang.Character#toLowerCase(char)} to each character produces the same result.

param
anotherString the String to compare this String against.
return
true if the argument is not null and the Strings are equal, ignoring case; false otherwise.
see
#equals(Object)
see
java.lang.Character#toLowerCase(char)
see
java.lang.Character#toUpperCase(char)

        return (anotherString != null) && (anotherString.count == count) &&
            regionMatches(true, 0, anotherString, 0, count);
    
public byte[]getBytes(java.lang.String enc)
Convert this String into bytes according to the specified character encoding, storing the result into a new byte array.

param
enc A character-encoding name
return
The resultant byte array
exception
UnsupportedEncodingException If the named encoding is not supported
since
JDK1.1

        return Helper.charToByteArray(value, offset, count, enc);
    
public byte[]getBytes()
Convert this String into bytes according to the platform's default character encoding, storing the result into a new byte array.

return
the resultant byte array.
since
JDK1.1

        return Helper.charToByteArray(value, offset, count);
    
public voidgetChars(int srcBegin, int srcEnd, char[] dst, int dstBegin)
Copies characters from this string into the destination character array.

The first character to be copied is at index srcBegin; the last character to be copied is at index srcEnd-1 (thus the total number of characters to be copied is srcEnd-srcBegin). The characters are copied into the subarray of dst starting at index dstBegin and ending at index:

dstbegin + (srcEnd-srcBegin) - 1

param
srcBegin index of the first character in the string to copy.
param
srcEnd index after the last character in the string to copy.
param
dst the destination array.
param
dstBegin the start offset in the destination array.
exception
IndexOutOfBoundsException If any of the following is true:
  • srcBegin is negative.
  • srcBegin is greater than srcEnd
  • srcEnd is greater than the length of this string
  • dstBegin is negative
  • dstBegin+(srcEnd-srcBegin) is larger than dst.length
exception
NullPointerException if dst is null

        if (srcBegin < 0) {
            throw new StringIndexOutOfBoundsException(
/* #ifdef VERBOSE_EXCEPTIONS */
/// skipped                       srcBegin
/* #endif */
            );
        }
        if (srcEnd > count) {
            throw new StringIndexOutOfBoundsException(
/* #ifdef VERBOSE_EXCEPTIONS */
/// skipped                       srcEnd
/* #endif */
            );
        }
        if (srcBegin > srcEnd) {
            throw new StringIndexOutOfBoundsException(
/* #ifdef VERBOSE_EXCEPTIONS */
/// skipped                       srcEnd - srcBegin
/* #endif */
            );
        }
        // NOTE: dst not checked, cannot use unchecked arraycopy
        System.arraycopy(value, offset + srcBegin, dst, dstBegin,
                         srcEnd - srcBegin);
    
public native inthashCode()
Returns a hashcode for this string. The hashcode for a String object is computed as
s[0]*31^(n-1) + s[1]*31^(n-2) + ... + s[n-1]
using int arithmetic, where s[i] is the ith character of the string, n is the length of the string, and ^ indicates exponentiation. (The hash value of the empty string is zero.)

return
a hash code value for this object.

public native intindexOf(int ch)
Returns the index within this string of the first occurrence of the specified character. If a character with value ch occurs in the character sequence represented by this String object, then the index of the first such occurrence is returned -- that is, the smallest value k such that:
this.charAt(k) == ch
is true. If no such character occurs in this string, then -1 is returned.

param
ch a character.
return
the index of the first occurrence of the character in the character sequence represented by this object, or -1 if the character does not occur.

public native intindexOf(int ch, int fromIndex)
Returns the index within this string of the first occurrence of the specified character, starting the search at the specified index.

If a character with value ch occurs in the character sequence represented by this String object at an index no smaller than fromIndex, then the index of the first such occurrence is returned--that is, the smallest value k such that:

(this.charAt(k) == ch) && (k >= fromIndex)
is true. If no such character occurs in this string at or after position fromIndex, then -1 is returned.

There is no restriction on the value of fromIndex. If it is negative, it has the same effect as if it were zero: this entire string may be searched. If it is greater than the length of this string, it has the same effect as if it were equal to the length of this string: -1 is returned.

param
ch a character.
param
fromIndex the index to start the search from.
return
the index of the first occurrence of the character in the character sequence represented by this object that is greater than or equal to fromIndex, or -1 if the character does not occur.

public intindexOf(java.lang.String str)
Returns the index within this string of the first occurrence of the specified substring. The integer returned is the smallest value k such that:
this.startsWith(str, k)
is true.

param
str any string.
return
if the string argument occurs as a substring within this object, then the index of the first character of the first such substring is returned; if it does not occur as a substring, -1 is returned.
exception
java.lang.NullPointerException if str is null.

        return indexOf(str, 0);
    
public intindexOf(java.lang.String str, int fromIndex)
Returns the index within this string of the first occurrence of the specified substring, starting at the specified index. The integer returned is the smallest value k such that:
this.startsWith(str, k) && (k >= fromIndex)
is true.

There is no restriction on the value of fromIndex. If it is negative, it has the same effect as if it were zero: this entire string may be searched. If it is greater than the length of this string, it has the same effect as if it were equal to the length of this string: -1 is returned.

param
str the substring to search for.
param
fromIndex the index to start the search from.
return
If the string argument occurs as a substring within this object at a starting index no smaller than fromIndex, then the index of the first character of the first such substring is returned. If it does not occur as a substring starting at fromIndex or beyond, -1 is returned.
exception
java.lang.NullPointerException if str is null

        char v1[] = value;
        char v2[] = str.value;
        int max = offset + (count - str.count);
        if (fromIndex >= count) {
            if (count == 0 && fromIndex == 0 && str.count == 0) {
                /* There is an empty string at index 0 in an empty string. */
                return 0;
            }
            /* Note: fromIndex might be near -1>>>1 */
            return -1;
        }
        if (fromIndex < 0) {
            fromIndex = 0;
        }
        if (str.count == 0) {
            return fromIndex;
        }

        int strOffset = str.offset;
        char first  = v2[strOffset];
        int i = offset + fromIndex;

    startSearchForFirstChar:
        while (true) {

            /* Look for first character. */
            while (i <= max && v1[i] != first) {
                i++;
            }
            if (i > max) {
                return -1;
            }

            /* Found first character, now look at the rest of v2 */
            int j = i + 1;
            int end = j + str.count - 1;
            int k = strOffset + 1;
            while (j < end) {
                if (v1[j++] != v2[k++]) {
                    i++;
                    /* Look for str's first char again. */
                    continue startSearchForFirstChar;
                }
            }
            return i - offset;  /* Found whole string. */
        }
    
public native java.lang.Stringintern()
Returns a canonical representation for the string object.

A pool of strings, initially empty, is maintained privately by the class String.

When the intern method is invoked, if the pool already contains a string equal to this String object as determined by the {@link #equals(Object)} method, then the string from the pool is returned. Otherwise, this String object is added to the pool and a reference to this String object is returned.

It follows that for any two strings s and t, s.intern() == t.intern() is true if and only if s.equals(t) is true.

All literal strings and string-valued constant expressions are interned. String literals are defined in Section 3.10.5 of the Java Language Specification

return
a string that has the same contents as this string, but is guaranteed to be from a pool of unique strings.
since
CLDC 1.1

public native intlastIndexOf(int ch)
Returns the index within this string of the last occurrence of the specified character. That is, the index returned is the largest value k such that:
this.charAt(k) == ch
is true. The String is searched backwards starting at the last character.

param
ch a character.
return
the index of the last occurrence of the character in the character sequence represented by this object, or -1 if the character does not occur.

public native intlastIndexOf(int ch, int fromIndex)
Returns the index within this string of the last occurrence of the specified character, searching backward starting at the specified index. That is, the index returned is the largest value k such that:
(this.charAt(k) == ch) && (k <= fromIndex)
is true.

param
ch a character.
param
fromIndex the index to start the search from. There is no restriction on the value of fromIndex. If it is greater than or equal to the length of this string, it has the same effect as if it were equal to one less than the length of this string: this entire string may be searched. If it is negative, it has the same effect as if it were -1: -1 is returned.
return
the index of the last occurrence of the character in the character sequence represented by this object that is less than or equal to fromIndex, or -1 if the character does not occur before that point.

public intlength()
Returns the length of this string. The length is equal to the number of 16-bit Unicode characters in the string.

return
the length of the sequence of characters represented by this object.

        return count;
    
public booleanregionMatches(boolean ignoreCase, int toffset, java.lang.String other, int ooffset, int len)
Tests if two string regions are equal.

A substring of this String object is compared to a substring of the argument other. The result is true if these substrings represent character sequences that are the same, ignoring case if and only if ignoreCase is true. The substring of this String object to be compared begins at index toffset and has length len. The substring of other to be compared begins at index ooffset and has length len. The result is false if and only if at least one of the following is true:

  • toffset is negative.
  • ooffset is negative.
  • toffset+len is greater than the length of this String object.
  • ooffset+len is greater than the length of the other argument.
  • There is some nonnegative integer k less than len such that:
    this.charAt(toffset+k) != other.charAt(ooffset+k)
    
  • ignoreCase is true and there is some nonnegative integer k less than len such that:
    Character.toLowerCase(this.charAt(toffset+k)) !=
    Character.toLowerCase(other.charAt(ooffset+k))
    
    and:
    Character.toUpperCase(this.charAt(toffset+k)) !=
    Character.toUpperCase(other.charAt(ooffset+k))
    

param
ignoreCase if true, ignore case when comparing characters.
param
toffset the starting offset of the subregion in this string.
param
other the string argument.
param
ooffset the starting offset of the subregion in the string argument.
param
len the number of characters to compare.
return
true if the specified subregion of this string matches the specified subregion of the string argument; false otherwise. Whether the matching is exact or case insensitive depends on the ignoreCase argument.

        char ta[] = value;
        int to = offset + toffset;
        int tlim = offset + count;
        char pa[] = other.value;
        int po = other.offset + ooffset;

        // Note: toffset, ooffset, or len might be near -1>>>1.
        if ((ooffset < 0) || (toffset < 0) || (toffset > (long)count - len) ||
            (ooffset > (long)other.count - len)) {
            return false;
        }
        while (len-- > 0) {
            char c1 = ta[to++];
            char c2 = pa[po++];
            if (c1 == c2)
                continue;
            if (ignoreCase) {
                // If characters don't match but case may be ignored,
                // try converting both characters to uppercase.
                // If the results match, then the comparison scan should
                // continue.
                char u1 = Character.toUpperCase(c1);
                char u2 = Character.toUpperCase(c2);
                if (u1 == u2)
                    continue;
                // Unfortunately, conversion to uppercase does not work properly
                // for the Georgian alphabet, which has strange rules about case
                // conversion.  So we need to make one last check before
                // exiting.
                if (Character.toLowerCase(u1) == Character.toLowerCase(u2))
                    continue;
            }
            return false;
        }
        return true;
    
public java.lang.Stringreplace(char oldChar, char newChar)
Returns a new string resulting from replacing all occurrences of oldChar in this string with newChar.

If the character oldChar does not occur in the character sequence represented by this String object, then a reference to this String object is returned. Otherwise, a new String object is created that represents a character sequence identical to the character sequence represented by this String object, except that every occurrence of oldChar is replaced by an occurrence of newChar.

Examples:

"mesquite in your cellar".replace('e', 'o')
returns "mosquito in your collar"
"the war of baronets".replace('r', 'y')
returns "the way of bayonets"
"sparring with a purple porpoise".replace('p', 't')
returns "starring with a turtle tortoise"
"JonL".replace('q', 'x') returns "JonL" (no change)

param
oldChar the old character.
param
newChar the new character.
return
a string derived from this string by replacing every occurrence of oldChar with newChar.

        if (oldChar != newChar) {
            int len = count;
            int i = -1;
            char[] val = value; /* avoid getfield opcode */
            int off = offset;   /* avoid getfield opcode */

            while (++i < len) {
                if (val[off + i] == oldChar) {
                    break;
                }
            }
            if (i < len) {
                char buf[] = new char[len];
                for (int j = 0 ; j < i ; j++) {
                    buf[j] = val[off+j];
                }
                while (i < len) {
                    char c = val[off + i];
                    buf[i] = (c == oldChar) ? newChar : c;
                    i++;
                }
                return new String(0, len, buf);
            }
        }
        return this;
    
public booleanstartsWith(java.lang.String prefix, int toffset)
Tests if this string starts with the specified prefix beginning at the specified index.

param
prefix the prefix.
param
toffset where to begin looking in the string.
return
true if the character sequence represented by the argument is a prefix of the substring of this object starting at index toffset; false otherwise. The result is false if toffset is negative or greater than the length of this String object; otherwise the result is the same as the result of the expression
this.subString(toffset).startsWith(prefix)
exception
java.lang.NullPointerException if prefix is null.

        char ta[] = value;
        int to = offset + toffset;
        int tlim = offset + count;
        char pa[] = prefix.value;
        int po = prefix.offset;
        int pc = prefix.count;
        // Note: toffset might be near -1>>>1.
        if ((toffset < 0) || (toffset > count - pc)) {
            return false;
        }
        while (--pc >= 0) {
            if (ta[to++] != pa[po++]) {
                return false;
            }
        }
        return true;
    
public booleanstartsWith(java.lang.String prefix)
Tests if this string starts with the specified prefix.

param
prefix the prefix.
return
true if the character sequence represented by the argument is a prefix of the character sequence represented by this string; false otherwise. Note also that true will be returned if the argument is an empty string or is equal to this String object as determined by the {@link #equals(Object)} method.
exception
java.lang.NullPointerException if prefix is null.
since
JDK1.0

        return startsWith(prefix, 0);
    
public java.lang.Stringsubstring(int beginIndex)
Returns a new string that is a substring of this string. The substring begins with the character at the specified index and extends to the end of this string.

Examples:

"unhappy".substring(2) returns "happy"
"Harbison".substring(3) returns "bison"
"emptiness".substring(9) returns "" (an empty string)

param
beginIndex the beginning index, inclusive.
return
the specified substring.
exception
IndexOutOfBoundsException if beginIndex is negative or larger than the length of this String object.

        return substring(beginIndex, count);
    
public java.lang.Stringsubstring(int beginIndex, int endIndex)
Returns a new string that is a substring of this string. The substring begins at the specified beginIndex and extends to the character at index endIndex - 1. Thus the length of the substring is endIndex-beginIndex.

Examples:

"hamburger".substring(4, 8) returns "urge"
"smiles".substring(1, 5) returns "mile"

param
beginIndex the beginning index, inclusive.
param
endIndex the ending index, exclusive.
return
the specified substring.
exception
IndexOutOfBoundsException if the beginIndex is negative, or endIndex is larger than the length of this String object, or beginIndex is larger than endIndex.

        if (beginIndex < 0) {
            throw new StringIndexOutOfBoundsException(
/* #ifdef VERBOSE_EXCEPTIONS */
/// skipped                       beginIndex
/* #endif */
            );
        }
        if (endIndex > count) {
            throw new StringIndexOutOfBoundsException(
/* #ifdef VERBOSE_EXCEPTIONS */
/// skipped                       endIndex
/* #endif */
            );
        }
        if (beginIndex > endIndex) {
            throw new StringIndexOutOfBoundsException(
/* #ifdef VERBOSE_EXCEPTIONS */
/// skipped                       endIndex - beginIndex
/* #endif */
            );
        }
        return ((beginIndex == 0) && (endIndex == count)) ? this :
            new String(offset + beginIndex, endIndex - beginIndex, value);
    
public char[]toCharArray()
Converts this string to a new character array.

return
a newly allocated character array whose length is the length of this string and whose contents are initialized to contain the character sequence represented by this string.

        char result[] = new char[count];
        getChars(0, count, result, 0);
        return result;
    
public java.lang.StringtoLowerCase()
Converts all of the characters in this String to lower case.

return
the String, converted to lowercase.
see
Character#toLowerCase
see
String#toUpperCase

        int i;

        scan : {
            for(i = 0 ; i < count ; i++) {
                char c = value[offset+i];
                if (c != Character.toLowerCase(c)) {
                    break scan;
                }
            }
            return this;
        }

        char buf[] = new char[count];

        JVM.unchecked_char_arraycopy(value, offset, buf, 0, i);

        for(; i < count ; i++) {
            buf[i] = Character.toLowerCase(value[offset+i]);
        }
        return new String(0, count, buf);
    
public java.lang.StringtoString()
This object (which is already a string!) is itself returned.

return
the string itself.

        return this;
    
public java.lang.StringtoUpperCase()
Converts all of the characters in this String to upper case.

return
the String, converted to uppercase.
see
Character#toLowerCase
see
String#toUpperCase

        int i;

        scan : {
            for(i = 0 ; i < count ; i++) {
                char c = value[offset+i];
                if (c != Character.toUpperCase(c)) {
                    break scan;
                }
            }
            return this;
        }

        char buf[] = new char[count];

        JVM.unchecked_char_arraycopy(value, offset, buf, 0, i);

        for(; i < count ; i++) {
            buf[i] = Character.toUpperCase(value[offset+i]);
        }
        return new String(0, count, buf);
    
public java.lang.Stringtrim()
Removes white space from both ends of this string.

If this String object represents an empty character sequence, or the first and last characters of character sequence represented by this String object both have codes greater than '\u0020' (the space character), then a reference to this String object is returned.

Otherwise, if there is no character with a code greater than '\u0020' in the string, then a new String object representing an empty string is created and returned.

Otherwise, let k be the index of the first character in the string whose code is greater than '\u0020', and let m be the index of the last character in the string whose code is greater than '\u0020'. A new String object is created, representing the substring of this string that begins with the character at index k and ends with the character at index m-that is, the result of this.substring(km+1).

This method may be used to trim whitespace from the beginning and end of a string; in fact, it trims all ASCII control characters as well.

return
this string, with white space removed from the front and end.

        int len = count;
        int st = 0;
        int off = offset;      /* avoid getfield opcode */
        char[] val = value;    /* avoid getfield opcode */

        while ((st < len) && (val[off + st] <= ' ")) {
            st++;
        }
        while ((st < len) && (val[off + len - 1] <= ' ")) {
            len--;
        }
        return ((st > 0) || (len < count)) ? substring(st, len) : this;
    
public static java.lang.StringvalueOf(java.lang.Object obj)
Returns the string representation of the Object argument.

param
obj an Object.
return
if the argument is null, then a string equal to "null"; otherwise, the value of obj.toString() is returned.
see
java.lang.Object#toString()

        return (obj == null) ? "null" : obj.toString();
    
public static java.lang.StringvalueOf(char[] data)
Returns the string representation of the char array argument. The contents of the character array are copied; subsequent modification of the character array does not affect the newly created string.

param
data a char array.
return
a newly allocated string representing the same sequence of characters contained in the character array argument.

        return new String(data);
    
public static java.lang.StringvalueOf(char[] data, int offset, int count)
Returns the string representation of a specific subarray of the char array argument.

The offset argument is the index of the first character of the subarray. The count argument specifies the length of the subarray. The contents of the subarray are copied; subsequent modification of the character array does not affect the newly created string.

param
data the character array.
param
offset the initial offset into the value of the String.
param
count the length of the value of the String.
return
a newly allocated string representing the sequence of characters contained in the subarray of the character array argument.
exception
NullPointerException if data is null.
exception
IndexOutOfBoundsException if offset is negative, or count is negative, or offset+count is larger than data.length.

        return new String(data, offset, count);
    
public static java.lang.StringvalueOf(boolean b)
Returns the string representation of the boolean argument.

param
b a boolean.
return
if the argument is true, a string equal to "true" is returned; otherwise, a string equal to "false" is returned.

        return b ? "true" : "false";
    
public static java.lang.StringvalueOf(char c)
Returns the string representation of the char argument.

param
c a char.
return
a newly allocated string of length 1 containing as its single character the argument c.

        char data[] = {c};
        return new String(0, 1, data);
    
public static java.lang.StringvalueOf(int i)
Returns the string representation of the int argument.

The representation is exactly the one returned by the Integer.toString method of one argument.

param
i an int.
return
a newly allocated string containing a string representation of the int argument.
see
java.lang.Integer#toString(int, int)

        return Integer.toString(i, 10);
    
public static java.lang.StringvalueOf(long l)
Returns the string representation of the long argument.

The representation is exactly the one returned by the Long.toString method of one argument.

param
l a long.
return
a newly allocated string containing a string representation of the long argument.
see
java.lang.Long#toString(long)

        return Long.toString(l, 10);
    
public static java.lang.StringvalueOf(float f)
Returns the string representation of the float argument.

The representation is exactly the one returned by the Float.toString method of one argument.

param
f a float.
return
a newly allocated string containing a string representation of the float argument.
see
java.lang.Float#toString(float)
since
CLDC 1.1

        return Float.toString(f);
    
public static java.lang.StringvalueOf(double d)
Returns the string representation of the double argument.

The representation is exactly the one returned by the Double.toString method of one argument.

param
d a double.
return
a newly allocated string containing a string representation of the double argument.
see
java.lang.Double#toString(double)
since
CLDC 1.1

        return Double.toString(d);