|
||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||||
Object
String
public class String
The String class represents character strings. All
string literals, 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.
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.
Buffer| Method Summary | |
|---|---|
char |
charAt(int index)
Returns the character at the specified index. |
int |
compareTo(String str)
Compares two strings lexicographically. |
int |
compareToIgnoreCase(String str)
Compares two strings lexicographically, ignoring case differences. |
boolean |
endsWith(String suffix)
Tests if this string ends with the specified suffix. |
boolean |
equalsIgnoreCase(String str)
Compares this String to another String, ignoring case considerations. |
List |
explode(String divider)
Splits this String into list of substrings according to given divider string. |
int |
indexOf(char ch,
int fromIndex)
Returns the index within this string of the first occurrence of the specified character, starting the search at the specified index. |
int |
indexOf(String str,
int fromIndex)
Returns the index within this string of the first occurrence of the specified substring, starting at the specified index. |
int |
lastIndexOf(char ch,
int fromIndex)
Returns the index within this string of the last occurrence of the specified character, searching backward starting at the specified index. |
int |
length()
Returns the length of this string. |
char |
operator_get(int index)
Returns the character at the specified index. |
String |
replace(char oldChar,
char newChar)
Returns a new string resulting from replacing all occurrences of oldChar in this string with newChar. |
boolean |
startsWith(String prefix)
Tests if this string starts with the specified prefix. |
String |
substring(int beginIndex,
int endIndex)
Returns a new string that is a substring of this string. |
String |
toLowerCase()
Converts all of the characters in this String to lower case. |
String |
toUpperCase()
Converts all of the characters in this String to upper case. |
String |
trim()
Removes white space from both ends of this string. |
| Methods inherited from class Object |
|---|
toString, equals, hashCode |
| Methods inherited from |
|---|
equals, getClass, notify, notifyAll, toString, wait, wait, wait |
| Method Detail |
|---|
public int length()
public char charAt(int index)
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.
index - the index of the character.
0.
public String substring(int beginIndex,
int endIndex)
beginIndex and
extends to the character at index endIndex - 1.
Thus the length of the substring is endIndex-beginIndex.
beginIndex - the beginning index, inclusive.endIndex - the ending index, exclusive.
public boolean startsWith(String prefix)
prefix - the prefix.
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
Object.equals(Object) method.public boolean endsWith(String suffix)
suffix - the suffix.
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 Object.equals(Object) method.
java.lang.NullPointerException - if suffix is
null.
public int indexOf(String str,
int fromIndex)
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.
str - the substring to search for.fromIndex - the index to start the search from.
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.
public int indexOf(char ch,
int fromIndex)
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:
is true. If no such character occurs in this string at or after position(this.charAt(k) == ch) && (k >= fromIndex)
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.
ch - a character.fromIndex - the index to start the search from.
fromIndex, or -1
if the character does not occur.
public int lastIndexOf(char ch,
int fromIndex)
is true.(this.charAt(k) == ch) && (k <= fromIndex)
ch - a character.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.
fromIndex, or -1
if the character does not occur before that point.public String trim()
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(k, m+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.
public String toUpperCase()
toLowerCase()public String toLowerCase()
toUpperCase()
public String replace(char oldChar,
char newChar)
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.
oldChar - the old character.newChar - the new character.
oldChar with newChar.public int compareTo(String str)
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 Object.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:
If there is no index position at which they differ, then the shorter string lexicographically precedes the longer string. In this case,this.charAt(k)-str.charAt(k)
compareTo returns the difference of the lengths of the
strings -- that is, the value:
this.length()-str.length()
str - the String to be compared.
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.public int compareToIgnoreCase(String str)
str - the String to be compared.
public boolean equalsIgnoreCase(String str)
str - the String to compare this String against.
true if the argument is not null
and the Strings are equal, ignoring case; false otherwise.public char operator_get(int index)
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.
index - the index of the character.
0.public List explode(String divider)
divider - Divider string
|
||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||||