Class java.lang.String
All Packages This Package Previous Next
Class java.lang.String
java.lang.Object
|
+----java.lang.String
-
public final class
String
-
extends Object
A general class of objects to represent character strings.
Strings are constant, their values cannot be changed after creation.
The compiler makes sure that each string constant actually results
in a String object. 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,1);
String d = cde.substring(1, 1);
-
See Also:
-
StringBuffer
-
Version:
-
1.36, 09 May 1995
-
Author:
-
Lee Boynton,
Arthur van Hoff
-
String()
-
Constructs a new empty string.
-
String(String)
-
Constructs a new string that is a copy of the specified string.
-
String(char[])
-
Constructs a new string from an array of characters as its initial value.
-
String(char[], int, int)
-
Constructs a new string from a sub array of characters.
-
String(byte[], int, int, int)
-
Constructs a new string from a sub array of bytes.
-
String(byte[], int)
-
Constructs a new string from an array of bytes.
-
charAt(int)
-
Returns the character at the specified index.
-
compareTo(String)
-
Compares this string against another string.
-
concat(String)
-
Concatenates a string.
-
copyValueOf(char[], int, int)
-
Returns a string that is equivalent to the given character array.
-
copyValueOf(char[])
-
Returns a string that is equivalent to the given character array.
-
endsWith(String)
-
Determines whether a string ends with some suffix.
-
equals(Object)
-
Compares a string against another object.
-
equalsIgnoreCase(Object)
-
Compares the string against another object.
-
getBytes(int, int, byte[], int)
-
Copies characters from the string into a byte array.
-
getChars(int, int, char[], int)
-
Copies characters from the string into an character array.
-
hashCode()
-
Returns a hashcode of the string.
-
indexOf(int)
-
Returns the index of the first occurrence of aCharacter.
-
indexOf(int, int)
-
Returns the index of the first occurrence of ch, starting
the search at fromIndex.
-
indexOf(String)
-
Returns the index of the first occurrence of a substring.
-
indexOf(String, int)
-
Returns the index of the first occurrence of a substring.
-
intern()
-
Returns a string that is equal to the current string
bug which is guaranteed to be from the unique string pool.
-
lastIndexOf(int)
-
Returns the index of the last occurrence of ch in a string.
-
lastIndexOf(int, int)
-
Returns the index of the last occurrence of ch in a string.
-
lastIndexOf(String)
-
Returns the index of the last occurrence of a substring.
-
lastIndexOf(String, int)
-
Returns the index of the last occurrence of a substring.
-
length()
-
Returns the length of the string.
-
regionMatches(int, String, int, int)
-
Determines whether a region of one string matches a region
of another string.
-
regionMatches(boolean, int, String, int, int)
-
Determines whether a region of one string matches a region
of another string.
-
replace(char, char)
-
Converts a string by replacing occurences of oldChar with newChar.
-
startsWith(String, int)
-
Determines whether a string starts with some prefix.
-
startsWith(String)
-
Determines whether a string starts with some prefix.
-
substring(int)
-
Returns the substring of a String.
-
substring(int, int)
-
Returns the substring of a String.
-
toCharArray()
-
Converts to a character array.
-
toLowerCase()
-
Converts a string to lower case.
-
toString()
-
Converts this string to a string.
-
toUpperCase()
-
Converts a string to upper case.
-
trim()
-
Trims leading and trailing whitespace from a string.
-
valueOf(Object)
-
Returns a string that represents the string value of the object.
-
valueOf(char[])
-
Returns a string that is equivalent to the given character array.
-
valueOf(char[], int, int)
-
Returns a string that is equivalent to the given character array.
-
valueOf(boolean)
-
Returns a string object that represents the state of a boolean.
-
valueOf(int)
-
Returns a string object that represents an integer.
-
valueOf(long)
-
Returns a string object that represents a long.
-
valueOf(float)
-
Returns a string object that represents a float.
-
valueOf(double)
-
Returns a string object that represents a double.
String
public String()
-
Constructs a new empty string.
String
public String(String value)
-
Constructs a new string that is a copy of the specified string.
-
Parameters:
-
value
-
the initial value of the String
String
public String(char value[])
-
Constructs a new string from an array of characters as its initial value.
The character array is NOT copied. Do not modify the array after
the string is created.
-
Parameters:
-
value
-
the initial value of the String
String
public String(char value[],
int offset,
int count)
-
Constructs a new string from a sub array of characters.
The value of the new string will be count characters
starting at offset.
The character array is NOT copied. Do not modify the array after
the string is created.
-
Parameters:
-
value
-
the initial value of the String, an array of characters
-
offset
-
the offset into the value of the string
-
count
-
the length of the value of the string
String
public String(byte ascii[],
int hibyte,
int offset,
int count)
-
Constructs a new string from a sub array of bytes.
The high-byte of each character can be specified, it should usually be 0.
The value of the new string will be count characters
starting at offset.
-
Parameters:
-
ascii
-
the bytes that will be converted to characters
-
hibyte
-
the high byte of each UNICODE character
-
offset
-
the offset into the ascii array
-
count
-
the length of the string
String
public String(byte ascii[],
int hibyte)
-
Constructs a new string from an array of bytes.
The byte array transformed into UNICODE chars using hibyte
as the upper byte of each character.
-
Parameters:
-
ascii
-
the byte that will be converted to characters
-
hibyte
-
the top 8 bits of each 16 bit UNICODE character
length
public int length()
-
Returns the length of the string.
The length of the string is equal to the number of 16 bit
UNICODE characters in the string.
-
Returns:
-
the length of the string
charAt
public char charAt(int index)
-
Returns the character at the specified index. An index ranges
from 0 to length() - 1.
-
Parameters:
-
index
-
the index of the desired character
-
Returns:
-
the desired character
-
Throws: StringIndexOutOfRangeException
-
The index is not
in the range 0 to length()-1.
getChars
public void getChars(int srcBegin,
int srcEnd,
char dst[],
int dstBegin)
-
Copies characters from the string into an character array.
The characters of the specified substring (determined by
srcBegin and srcEnd) are copied into the character array,
starting at the array's dstIndex location.
-
Parameters:
-
srcBegin
-
index of the first character in the string
-
srcEnd
-
end of the characters that are copied
-
dst
-
the destination array
-
dstBegin
-
the start offset in the destination array
getBytes
public void getBytes(int srcBegin,
int srcEnd,
byte dst[],
int dstBegin)
-
Copies characters from the string into a byte array.
Copies the characters of the specified substring (determined by
srcBegin and srcEnd) into the byte array, starting at the
array's dstIndex location.
-
Parameters:
-
srcBegin
-
index of the first character in the string
-
srcEnd
-
end of the characters that are copied
-
dst
-
the destination array
-
dstBegin
-
the start offset in the destination array
equals
public boolean equals(Object anObject)
-
Compares a string against another object.
Returns true if the object is equal to this string; that is,
has the same length and the same characters in the same sequence.
-
Parameters:
-
anObject
-
the object to compare this string against
-
Returns:
-
true if they are equal
-
Overrides:
-
equals in class Object
equalsIgnoreCase
public boolean equalsIgnoreCase(Object anObject)
-
Compares the string against another object.
Returns true if the object is equal to this string; that is,
has the same length and the same characters in the same sequence.
Upper case character are folded to lower case character before
they are compared.
-
Parameters:
-
anObject
-
the object to compare this string against
-
Returns:
-
true if they are equal, ignoring case
compareTo
public int compareTo(String anotherString)
-
Compares this string against another string.
Returns an integer that is less than, equal to, or greater than zero,
depending on whether this string is less than, equal to, or greater
than anotherString.
-
Returns:
-
a number less than zero if this string is lexically
smaller, 0 if the string is equal or a number greater
than 0 if this string is lexically greater.
regionMatches
public boolean regionMatches(int toffset,
String other,
int ooffset,
int len)
-
Determines whether a region of one string matches a region
of another string.
-
Parameters:
-
toffset
-
where to start looking in this string
-
other
-
the other string
-
ooffset
-
where to start looking in the other string
-
len
-
the number of characters to match
-
Returns:
-
true if the region matches with the other
regionMatches
public boolean regionMatches(boolean ignoreCase,
int toffset,
String other,
int ooffset,
int len)
-
Determines whether a region of one string matches a region
of another string. Upper case characters are considered equivalent
to lower case letters.
-
Parameters:
-
ignoreCase
-
if true, case is ignored
-
toffset
-
where to start looking in this string
-
other
-
the other string
-
ooffset
-
where to start looking in the other string
-
len
-
the number of characters to match
-
Returns:
-
true if the region matches with the other
startsWith
public boolean startsWith(String prefix,
int toffset)
-
Determines whether a string starts with some prefix.
-
Parameters:
-
prefix
-
the prefix
-
toffset
-
where to start looking
-
Returns:
-
true if the string starts with the prefix
startsWith
public boolean startsWith(String prefix)
-
Determines whether a string starts with some prefix.
-
Parameters:
-
prefix
-
the prefix
-
Returns:
-
true if the string starts with the prefix
endsWith
public boolean endsWith(String suffix)
-
Determines whether a string ends with some suffix.
-
Parameters:
-
suffix
-
the suffix
-
Returns:
-
true if the string ends with the suffix
hashCode
public int hashCode()
-
Returns a hashcode of the string. This is a large
number composed of the character values in the string.
-
Returns:
-
the hash code
-
Overrides:
-
hashCode in class Object
indexOf
public int indexOf(int ch)
-
Returns the index of the first occurrence of aCharacter.
-
Parameters:
-
ch
-
the character
-
Returns:
-
the index if the character is found, -1 otherwise
indexOf
public int indexOf(int ch,
int fromIndex)
-
Returns the index of the first occurrence of ch, starting
the search at fromIndex.
-
Parameters:
-
ch
-
the character
-
fromIndex
-
the index to start the search from
-
Returns:
-
the index if the character is found, -1 otherwise
lastIndexOf
public int lastIndexOf(int ch)
-
Returns the index of the last occurrence of ch in a string.
The string is searched backwards starting at the last character.
-
Parameters:
-
ch
-
the character
-
Returns:
-
the index if the character is found, -1 otherwise
lastIndexOf
public int lastIndexOf(int ch,
int fromIndex)
-
Returns the index of the last occurrence of ch in a string.
The string is searched backwards starting at fromIndex.
-
Parameters:
-
ch
-
the character
-
fromIndex
-
the index to start the search from
-
Returns:
-
the index if the character is found, -1 otherwise
indexOf
public int indexOf(String str)
-
Returns the index of the first occurrence of a substring.
-
Parameters:
-
str
-
the substring
-
Returns:
-
the index if the substring is found, -1 otherwise
indexOf
public int indexOf(String str,
int fromIndex)
-
Returns the index of the first occurrence of a substring. The
search is started at fromIndex.
-
Parameters:
-
str
-
the substring
-
fromIndex
-
the index to start the search from
-
Returns:
-
the index if the substring is found, -1 otherwise
lastIndexOf
public int lastIndexOf(String str)
-
Returns the index of the last occurrence of a substring.
The string is searched backwards.
-
Parameters:
-
str
-
the substring
-
Returns:
-
the index if the substring is found, -1 otherwise
lastIndexOf
public int lastIndexOf(String str,
int fromIndex)
-
Returns the index of the last occurrence of a substring.
The string is searched backwards starting at fromIndex.
-
Parameters:
-
str
-
the substring
-
fromIndex
-
the index to start the search from
-
Returns:
-
the index if the substring is found, -1 otherwise
substring
public String substring(int beginIndex)
-
Returns the substring of a String. The substring is specified
by a beginIndex (inclusive) and the end of the string.
-
Parameters:
-
beginIndex
-
begin index, inclusive
-
Returns:
-
the substring upto the end of the string
substring
public String substring(int beginIndex,
int endIndex)
-
Returns the substring of a String. The substring is specified
by a beginIndex (inclusive) and an endIndex (exclusive).
-
Parameters:
-
beginIndex
-
begin index, inclusive
-
endIndex
-
end index, exclusive
-
Returns:
-
the substring
concat
public String concat(String str)
-
Concatenates a string.
-
Parameters:
-
str
-
the string which is concatenated to the end of this string
-
Returns:
-
the resulting string
replace
public String replace(char oldChar,
char newChar)
-
Converts a string by replacing occurences of oldChar with newChar.
-
Parameters:
-
oldChar
-
the old character
-
newChar
-
the new character
-
Returns:
-
the resulting string
toLowerCase
public String toLowerCase()
-
Converts a string to lower case.
-
Returns:
-
the string, converted to lowercase
-
See Also:
-
toLowerCase,
toUpperCase
toUpperCase
public String toUpperCase()
-
Converts a string to upper case.
-
Returns:
-
the string, converted to lowercase
-
See Also:
-
toUpperCase,
toLowerCase
trim
public String trim()
-
Trims leading and trailing whitespace from a string.
-
Returns:
-
the string, with whitespace removed.
toString
public String toString()
-
Converts this string to a string.
-
Returns:
-
the string itself
-
Overrides:
-
toString in class Object
toCharArray
public char toCharArray()
-
Converts to a character array. This creates a new array.
-
Returns:
-
an array of characters
valueOf
public static String valueOf(Object obj)
-
Returns a string that represents the string value of the object.
The object may choose how to represent itself by implementing
a "toString()" method.
-
Parameters:
-
obj
-
the object to be converted
-
Returns:
-
the resulting string
valueOf
public static String valueOf(char data[])
-
Returns a string that is equivalent to the given character array.
Uses the original array as the body of the string (ie. it doesn't
copy it to a new array).
-
Parameters:
-
data
-
the character array
-
Returns:
-
the resulting string
valueOf
public static String valueOf(char data[],
int offset,
int count)
-
Returns a string that is equivalent to the given character array.
Uses the original array as the body of the string (ie. it doesn't
copy it to a new array).
-
Parameters:
-
data
-
the character array
-
offset
-
the offset into the value of the string
-
count
-
the length of the value of the string
-
Returns:
-
the resulting string
copyValueOf
public static String copyValueOf(char data[],
int offset,
int count)
-
Returns a string that is equivalent to the given character array.
It creates a new array and copies the characters into it.
-
Parameters:
-
data
-
the character array
-
offset
-
the offset into the value of the string
-
count
-
the length of the value of the string
-
Returns:
-
the resulting string
copyValueOf
public static String copyValueOf(char data[])
-
Returns a string that is equivalent to the given character array.
It creates a new array and copies the characters into it.
-
Parameters:
-
data
-
the character array
-
Returns:
-
the resulting string
valueOf
public static String valueOf(boolean b)
-
Returns a string object that represents the state of a boolean.
-
Parameters:
-
b
-
the boolean
-
Returns:
-
the resulting string
valueOf
public static String valueOf(int i)
-
Returns a string object that represents an integer.
-
Parameters:
-
i
-
the integer
-
Returns:
-
the resulting string
valueOf
public static String valueOf(long l)
-
Returns a string object that represents a long.
-
Parameters:
-
l
-
the long
-
Returns:
-
the resulting string
valueOf
public static String valueOf(float f)
-
Returns a string object that represents a float.
-
Parameters:
-
f
-
the float
-
Returns:
-
the resulting string
valueOf
public static String valueOf(double d)
-
Returns a string object that represents a double.
-
Parameters:
-
d
-
the double
-
Returns:
-
the resulting string
intern
public String intern()
-
Returns a string that is equal to the current string
bug which is guaranteed to be from the unique string pool.
s1.intern() == s2.intern() <=> s1.equals(s2).
All Packages This Package Previous Next