Package java.lang Previous
Previous
Java API
Java API
Index
Index
Next
Next

Class String

Constructors , Methods

public  final  class  java.lang.String
    extends  java.lang.Object  
{
        // Constructors
    public String();	
    public String(byte  ascii[], int  hibyte);	
    public String(byte  ascii[], int  hibyte, 	
                          int  offset, int  count);
    public String(char  value[]);	
    public String(char  value[], int  offset, int  count);	 
    public String(String  value);	
    public String(StringBuffer  buffer);	

        // Methods
    public char charAt(int  index);	
    public int compareTo(String  anotherString);	
    public String concat(String  str);	
    public static String copyValueOf(char  data[]);	
    public static String	
        copyValueOf(char  data[], int  offset, int count);
    public boolean endsWith(String  suffix);	
    public boolean equals(Object  anObject);	
    public boolean equalsIgnoreCase(String  anotherString);	
    public void getBytes(int  srcBegin, int  srcEnd,	
                                            byte  dst[], int  dstBegin);
    public void getChars(int  srcBegin, int  srcEnd,	
                                          char  dst[], int  dstBegin);
    public int hashCode();	
    public int indexOf(int  ch);	
    public int indexOf(int  ch, int  fromIndex);	
    public int indexOf(String  str);	
    public int indexOf(String  str, int  fromIndex);	
    public String intern();	
    public int lastIndexOf(int  ch);	
    public int lastIndexOf(int  ch, int  fromIndex);	
    public int lastIndexOf(String  str);	
    public int lastIndexOf(String  str, int  fromIndex);	
                       
    public int length();	
    public boolean regionMatches(boolean  ignoreCase,	
             int  toffset, String  other, int  ooffset, int  len);
    public boolean regionMatches(int  toffset, String  other,	  
                int  ooffset, int  len);
    public String replace(char  oldChar, 	
                      char  newChar);
    public boolean startsWith(String  prefix);	
    public boolean startsWith(String  prefix, int toffset);	
    public String substring(int  beginIndex);	
    public String substring(int  beginIndex, int endIndex);	
    public char[] toCharArray();	
    public String toLowerCase();	
    public String toString();	
    public String toUpperCase();	
    public String trim();	
    public static String valueOf(boolean  b);	
    public static String valueOf(char  c);	
    public static String valueOf(char  data[]);	
    public static String 	
        valueOf(char  data[], int  offset, int  count);
    public static String valueOf(double  d);	
    public static String valueOf(float  f);	
    public static String valueOf(int  i);	
    public static String valueOf(long  l);	
    public static String valueOf(Object  obj);	
}

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.

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.


Constructors


String

public String() 

Allocates a new String containing no characters.


String

public String(byte  ascii[], int  hibyte) 

Allocates a new String containg characters constructed from an array of 8- bit integer values. Each character c in the resulting string is constructed from the corresponding component b in the byte array such that

c == (char)(((hibyte &amp; 0xff) << 8) | (b &amp; 0xff))

ParameterDescription
ascii the bytes to be converted to characters
hibyte the top 8 bits of each 16-bit Unicode character


String

public String(byte  ascii[], int  hibyte, int  offset,
                            int  count) 

Allocates a new String constructed from a subarray of an array of 8-bit integer values.

The offset argument is the index of the first byte of the subarray, and the count argument specifies the length of the subarray.

Each byte in the subarray is converted to a char as specified in the method above .

ParameterDescription
ascii the bytes to be converted to characters
hibyte
the top 8 bits of each 16-bit Unicode character
offset the initial offset
count the length

Throw:

StringIndexOutOfBoundsException

If the offset or count argument is invalid.


String

public String(char  value[]) 

Allocates a new String so that it represents the sequence of characters currently contained in the character array argument.

ParameterDescription
value the initial value of the string


String

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.

ParameterDescription
value array that is the source of characters
offset the initial offset
count the length

Throw:

StringIndexOutOfBoundsException

If the offset and count arguments index characters outside the bounds of the value array.


String

public String(String  value) 

Allocates a new string that contains the same sequence of characters as the string argument.

ParameterDescription
value a String.


String

public String(StringBuffer  buffer) 

Allocates a new string that contains the sequence of characters currently contained in the string buffer argument.

ParameterDescription
buffer a StringBuffer.


Methods


charAt

public char charAt(int  index) 

Return Value:

Returns The character at the specified index of this string. The first character is at index 0.

ParameterDescription
index the index of the desired character

Throw:

StringIndexOutOfBoundsException

If the index is out of range.


compareTo

public int compareTo(String  anotherString) 

Compares two strings lexicographically.

Return Value:

Returns 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.

ParameterDescription
anotherString the String to be compared


concat

public String concat(String  str) 

Concatenates the string argument to the end of this string.

If the length of the argument string is zero, then this object is returned.

Return Value:

Returns A string that represents the concatenation of this object's characters followed by the string argument's characters.

ParameterDescription
str the String which is concatenated to the end of this String


copyValueOf

public static String copyValueOf(char  data[]) 

Return Value:

Returns a String that contains the characters of the character array.

ParameterDescription
data the character array


copyValueOf

public static String
copyValueOf(char  data[], int  offset, int  count) 

Return Value:

Returns a String that contains the characters of the specified subarray of the character array.

ParameterDescription
data the character array
offset initial offset of the subarray
count length of the subarray


endsWith

public boolean endsWith(String  suffix) 

Return Value:

Returns true if the character sequence represented by the argument is a suffix of the character sequence represented by this object; false otherwise.

ParameterDescription
suffix the suffix


equals

public boolean equals(Object  anObject) 

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.

Return Value:

Returns true if the String's are equal; false otherwise.

ParameterDescription
anObject the object to compare this String against

Overrides:

equals in class Object .

See Also: equalsIgnoreCase

compareTo .


equalsIgnoreCase

public boolean equalsIgnoreCase(String  anotherString) 

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, where case is ignored.

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

Two sequence of characters are the same, ignoring case, if the sequences have the same length and corresponding characters are the same, ignoring case.

Return Value:

Returns true if the String's are equal, ignoring case; false otherwise.

ParameterDescription
anotherString the String to compare this String against


getBytes

public void getBytes(int  srcBegin, int  srcEnd,
                                          byte  dst[], int  dstBegin) 

Copies characters from this string into the destination byte array. Each byte receives the 8 low-order bits of the corresponding character.

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

dstbegin+(srcEnd-srcBegin)-1

ParameterDescription
srcBegin index of the first character in the string to copy
srcEnd index after the last character in the string to copy
dst the destination array
dstBegin the start offset in the destination array


getChars

public void getChars(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

ParameterDescription
srcBegin index of the first character in the string to copy
srcEnd index after the last character in the string to copy
dst the destination array
dstBegin the start offset in the destination array


hashCode

public int hashCode() 

Return Value:

Returns a hash code value for this object.

Overrides:

hashCode in class Object .


indexOf

public int indexOf(int  ch) 

Return Value:

Returns 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.

ParameterDescription
ch a character


indexOf

public int indexOf(int  ch, int  fromIndex) 

Return Value:

Returns 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.

ParameterDescription
ch a character
fromIndex the index to start the search from


indexOf

public int indexOf(String  str) 

Return Value:

Returns 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.

ParameterDescription
str any string


indexOf

public int indexOf(String  str, int  fromIndex) 

Return Value:

Returns 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.

ParameterDescription
str the substring to search for
fromIndex the index to start the search from


intern

public String intern() 

Creates a canonical representation for the string object.

If s and t are strings such that s.equals(t), it is guaranteed that s.intern() == t.intern().

Return Value:

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


lastIndexOf

public int lastIndexOf(int  ch) 

Return Value:

Returns 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.

ParameterDescription
ch a character.


lastIndexOf

public int lastIndexOf(int  ch, int  fromIndex) 

Return Value:

Returns the index of the last occurrence of the character in the character sequence represented by this object that is less than or equal to from-Index, or -1 if the character does not occur before that point.

ParameterDescription
ch a character
fromIndex the index to start the search from


lastIndexOf

public int lastIndexOf(String  str) 

Return Value:

Returns If the string argument occurs as a substring within this object, then the index of the first character of the last such substring is returned. If it does not occur as a substring, -1 is returned.

ParameterDescription
str the substring to search for


lastIndexOf

public int lastIndexOf(String  str, int  fromIndex) 

Return Value:

Returns If the string argument occurs as a substring within this object at a starting index no greater than fromIndex then the index of the first character of the lastsuch substring is returned. If it does not occur as a substring starting at fromIndex or earlier, -1 is returned.

ParameterDescription
str the substring to search for
fromIndex the index to start the search from


length

public int length() 

Return Value:

Returns the length of the sequence of characters represented by this object is returned.


regionMatches

public boolean
regionMatches(boolean  ignoreCase, int  toffset,
                            String  other, int  ooffset, int  len) 

Determines if two string regions are equal.

If toffset or ooffset is negative, or if toffset+length is greater than the length of this string, or if ooffset+length is greater than the length of the string argument, then this method returns false.

Return Value:

Returns 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.

ParameterDescription
ignoreCase if true, ignore case when comparing characters
toffset starting offset of the subregion in this string
other string argument
ooffset starting offset of the subregion in the string argument
len the number of characters to compare


regionMatches

public boolean
regionMatches(int  toffset, String  other, 
                            int  ooffset, int  len) 

Determines if two string regions are equal.

If toffset or ooffset is negative, or if toffset+length is greater than the length of this string, or if ooffset+length is greater than the length of the string argument, then this method returns false.

Return Value:

Returns true if the specified subregion of this string exactly matches the specified subregion of the string argument; false otherwise.

ParameterDescription
toffset starting offset of the subregion in this string
other string argument
ooffset starting offset of the subregion in the string argument
len the number of characters to compare


replace

public String replace(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 object, then this string is returned.

Return Value:

Returns a string derived from this string by replacing every occurrence of oldChar with newChar.

ParameterDescription
oldChar the old character
newChar the new character


startsWith

public boolean startsWith(String  prefix) 

Return Value:

Returns true if the character sequence represented by the argument is a prefix of the character sequence represented by this string; false otherwise

ParameterDescription
prefix the prefix


startsWith

public boolean startsWith(String  prefix, int  toffset) 

Return Value:

Returns true if the character sequence represented by the argument is a prefix of the substring of this object starting at index toffset; false otherwise

ParameterDescription
prefix the prefix
toffset where to begin looking in the the String


substring

public String substring(int  beginIndex) 

Creates a new string that is a substring of this string. The substring begins at the specified index and extends to the end of this string.

Return Value:

Returns the specified substring.

ParameterDescription
beginIndex the beginning index, inclusive

Throw:

StringIndexOutOfBoundsException

If the beginIndex is out of range.


substring

public String substring(int  beginIndex, int  endIndex) 

Creates 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.

Return Value:

Returns the specified substring.

ParameterDescription
beginIndex the beginning index, inclusive
endIndex the ending index, exclusive

Throw:

StringIndexOutOfBoundsException

If the beginIndex or the endIndex is out of range.


toCharArray

public char[] toCharArray() 

Return Value:

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


toLowerCase

public String toLowerCase() 

Converts a string to lowercase.

If no character in this string has a different lowercase version , then this string is returned.

Otherwise, a new string is allocated, whose length is identical to this string, and such that each character which has a difference lowercase version is mapped to this lower case equivalent.

Return Value:

Returns the string, converted to lowercase.


toString

public String toString() 

This object (which is already a string!) is itself returned.

Return Value:

Returns the string itself.

Overrides:

toString in class Object .


toUpperCase

public String toUpperCase() 

Converts a string to uppercase.

If no character in this string has a different uppercase version , then this string is returned.

Otherwise, a new string is allocated, whose length is identical to this string, and such that each character which has a difference uppercase version is mapped to this uppercase equivalent.

Return Value:

Returns the string, converted to uppercase.


trim

public String trim() 

Removes white space from both ends of a string.

All characters that have codes less than or equal to '\u0020' (the space character) are considered to be white space.

Return Value:

Returns this string, with whitespace removed from the front and end


valueOf

public static String valueOf(boolean  b) 

Creates the string representation of the boolean argument.

Return Value:

Returns if the argument is true, a string equal to "true" is returned; otherwise, a string equal to "false" is returned.

ParameterDescription
b a boolean


valueOf

public static String valueOf(char  c) 

Creates the string representation of the char argument.

Return Value:

Returns a newly allocated string of length one containing as its single characer the argument c.

ParameterDescription
c a char


valueOf

public static String valueOf(char  data[]) 

Creates the string representation of the char array argument.

Return Value:

Returns a newly allocated string representing the same sequence of characters contained in the character array argument.

ParameterDescription
data a char array


valueOf

public static String
valueOf(char  data[], int  offset, int  count) 

Creates 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.

Return Value:

Returns a newly allocated string representing the sequence of characters contained in the subarray of the character array argument.

ParameterDescription
data the character array
offset the initial offset into the value of the String
count the length of the value of the String


valueOf

public static String valueOf(double  d) 

Creates the string representation of the double argument.

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

Return Value:

Returns a newly allocated string containing a string representation of the double argument.

ParameterDescription
d a double


valueOf

public static String valueOf(float  f) 

Creates the string representation of the float argument.

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

Return Value:

Returns a newly allocated string containing a string representation of the float argument.

ParameterDescription
f a float


valueOf

public static String valueOf(int  i) 

Creates the string representation of the int argument.

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

Return Value:

Returns a newly allocated string containing a string representation of the int argument.

ParameterDescription
i an int


valueOf

public static String valueOf(long  l) 

Creates the string representation of a the long argument.

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

Return Value:

Returns a newly allocated string containing a string representation of the long argument.

ParameterDescription
l a long


valueOf

public static String valueOf(Object  obj) 

Creates the string representation of the Object argument.

Return Value:

Returns If the argument is null, then a string equal to "null"; otherwise the value of obj.toString() is returned.

ParameterDescription
obj an Object.

See Also: toString in class Object .



Top© 1996 Sun Microsystems, Inc. All rights reserved.