|
||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||||
java.lang.Objectjava.lang.String
public class String
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:
byte data[] = {(byte)'a', (byte)'b', (byte)'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".concat(cde));
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
concatentation 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 Gosling, Joy, and Steele,
The Java Language Specification.
Currently on JControl the class StringBuffer is not implemented.
| all |
| Constructor Summary | |
|---|---|
String()
Initializes a newly created String object so that it
represents an empty character sequence. |
|
String(byte[] bytes)
Construct a new String by converting the specified array
of bytes using the platform's default character encoding. |
|
String(byte[] bytes,
int offset,
int length)
Construct a new String by converting the specified
subarray of bytes using the platform's default character encoding. |
|
| Method Summary | |
|---|---|
char |
charAt(int index)
Returns the character at the specified index. |
String |
concat(String str)
Concatenates the specified string to the end of this string. |
boolean |
equals(Object anObject)
Compares this string to the specified object. |
byte[] |
getBytes()
Convert this String into bytes according to the platform's
default character encoding, storing the result into a new byte array. |
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 |
length()
Returns the length of this string. |
String |
substring(int beginIndex,
int endIndex)
Returns a new string that is a substring of this string. |
char[] |
toCharArray()
Converts this string to a new character array. |
String |
trim()
Removes white space from both ends of this string. |
static String |
valueOf(int i)
Returns the string representation of the int argument. |
| Methods inherited from class java.lang.Object |
|---|
clone, notifyAll, wait |
| Constructor Detail |
|---|
public String()
String object so that it
represents an empty character sequence.
public String(byte[] bytes)
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.
bytes - The bytes to be converted into characters
public String(byte[] bytes,
int offset,
int length)
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.
bytes - The bytes to be converted into charactersoffset - Index of the first byte to convertlength - Number of bytes to convert| Method Detail |
|---|
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.
java.lang.IndexOutOfBoundsException - if the index
argument is negative or not less than the length of this
string.public String concat(String str)
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"
str - the String that is concatenated to the end
of this String.
java.lang.NullPointerException - if str is
null.public boolean equals(Object anObject)
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.
equals in class ObjectanObject - the object to compare this String
against.
true if the String are equal;
false otherwise.public byte[] getBytes()
String into bytes according to the platform's
default character encoding, storing the result into a new byte array.
public int indexOf(String str,
int fromIndex)
isthis.startsWith(str, k) && (k >= fromIndex)
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.
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.
java.lang.NullPointerException - if str is
nullpublic int length()
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.
Examples:
"hamburger".substring(4, 8) returns "urge" "smiles".substring(1, 5) returns "mile"
beginIndex - the beginning index, inclusive.endIndex - the ending index, exclusive.
java.lang.IndexOutOfBoundsException - if the
beginIndex is negative, or
endIndex is larger than the length of
this String object, or
beginIndex is larger than
endIndex.public char[] toCharArray()
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 static String valueOf(int i)
int argument.
The representation is exactly the one returned by the
Integer.toString method of one argument.
i - an int.
int argument.Integer.toString(int)
|
||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||||