|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Object java.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. 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:
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 and for extracting substrings.
String conversions are implemented through the method
toString
, defined by Object
and
inherited by all classes in Java.
NOTE: Currently on JControl the class StringBuffer
is not implemented.
So it is not possible to use any construct like 'String a = "Hello "+"World!";'. Use instead
'String a = "Hello ".concat(" World!");'
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. |
|
String(char[] chars,
int offset,
int length)
Construct a new String by converting the specified
subarray of chars 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. |
protected void |
finalize()
finalize method |
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 |
lastIndexOf(String substr)
Returns the last occurrence index of overgiven String or -1 if not found |
int |
length()
Returns the length of this string. |
String |
substring(int start)
Returns a substring of the 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 |
toString()
Returns the empty string. |
String |
trim()
Removes white space from both ends of this string. |
static String |
valueOf(int i)
Returns the string representation of the int argument. |
static String |
valueOf(Object obj)
Returns the string representation of the Object argument. |
Methods inherited from class java.lang.Object |
---|
clone, getClass, 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 characterspublic 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 convertpublic String(char[] chars, int offset, int length)
String
by converting the specified
subarray of chars 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.
chars
- The charactersoffset
- Index of the first character to uselength
- Number of charactersMethod Detail |
---|
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
.
NullPointerException
- if str
is
null
.public char charAt(int index) throws IndexOutOfBoundsException
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
.
IndexOutOfBoundsException
- if the index
argument is negative or not less than the length of this
string.public byte[] getBytes()
String
into bytes according to the platform's
default character encoding, storing the result into a new byte array.
public char[] toCharArray()
public 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.
IndexOutOfBoundsException
- if the
beginIndex
is negative, or
endIndex
is larger than the length of
this String
object, or
beginIndex
is larger than
endIndex
.public String substring(int start)
string.substring(4, string.length()-4);a string created from characters 4 to string.length()-1 will be returned.
start
- the first character of the substringpublic 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 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.
NullPointerException
- if str
is
null
public int lastIndexOf(String substr)
substr
-
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)
public static String valueOf(Object obj)
Object
argument.
obj
- an Object.
Object.toString()
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 Object
anObject
- the object to compare this String
against.
true
if the String
are equal;
false
otherwise.protected void finalize()
public String toString()
Object
toString
in class Object
java/lang/Object.toString();
|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |