java.lang
public final class String extends Object implements Serializable, Comparable<String>, CharSequence
This class also includes a number of methods for manipulating the contents of strings (of course, creating a new object if there are any changes, as String is immutable). Case mapping relies on Unicode 3.0.0 standards, where some character sequences have a different number of characters in the uppercase version than the lower case.
Strings are special, in that they are the only object with an overloaded operator. When you use '+' with at least one String argument, both arguments have String conversion performed on them, and another String (not guaranteed to be unique) results.
String is special-cased when doing data serialization - rather than listing the fields of this class, a String object is converted to a string literal in the object stream.
Since: 1.0
UNKNOWN: updated to 1.4; but could use better data sharing via offset field
Field Summary | |
---|---|
static Comparator<String> | CASE_INSENSITIVE_ORDER
A Comparator that uses String.compareToIgnoreCase(String) .
|
Constructor Summary | |
---|---|
String()
Creates an empty String (length 0). | |
String(String str)
Copies the contents of a String to a new String. | |
String(char[] data)
Creates a new String using the character sequence of the char array.
| |
String(char[] data, int offset, int count)
Creates a new String using the character sequence of a subarray of
characters. | |
String(byte[] ascii, int hibyte, int offset, int count)
Creates a new String using an 8-bit array of integer values, starting at
an offset, and copying up to the count. | |
String(byte[] ascii, int hibyte)
Creates a new String using an 8-bit array of integer values. | |
String(byte[] data, int offset, int count, String encoding)
Creates a new String using the portion of the byte array starting at the
offset and ending at offset + count. | |
String(byte[] data, String encoding)
Creates a new String using the byte array. | |
String(byte[] data, int offset, int count)
Creates a new String using the portion of the byte array starting at the
offset and ending at offset + count. | |
String(byte[] data)
Creates a new String using the byte array. | |
String(StringBuffer buffer)
Creates a new String using the character sequence represented by
the StringBuffer. | |
String(StringBuilder buffer)
Creates a new String using the character sequence represented by
the StringBuilder. | |
String(int[] codePoints, int offset, int count)
Creates a new String containing the characters represented in the
given subarray of Unicode code points. |
Method Summary | |
---|---|
char | charAt(int index)
Returns the character located at the specified index within this String.
|
int | codePointAt(int index)
Get the code point at the specified index. |
int | codePointBefore(int index)
Get the code point before the specified index. |
int | codePointCount(int start, int end)
Return the number of code points between two indices in the
String . |
int | compareTo(String anotherString)
Compares this String and another String (case sensitive,
lexicographically). |
int | compareToIgnoreCase(String str)
Compares this String and another String (case insensitive). |
String | concat(String str)
Concatenates a String to this String. |
boolean | contains(CharSequence s)
Returns true iff this String contains the sequence of Characters
described in s. |
boolean | contentEquals(StringBuffer buffer)
Compares the given StringBuffer to this String. |
boolean | contentEquals(CharSequence seq)
Compares the given CharSequence to this String. |
static String | copyValueOf(char[] data, int offset, int count)
Returns a String representing the character sequence of the char array,
starting at the specified offset, and copying chars up to the specified
count. |
static String | copyValueOf(char[] data)
Returns a String representation of a character array. |
boolean | endsWith(String suffix)
Predicate which determines if this String ends with a given suffix.
|
boolean | equals(Object anObject)
Predicate which compares anObject to this. |
boolean | equalsIgnoreCase(String anotherString)
Compares a String to this String, ignoring case. |
static String | format(Locale locale, String format, Object... args) |
static String | format(String format, Object... args) |
void | getBytes(int srcBegin, int srcEnd, byte[] dst, int dstBegin)
Copies the low byte of each character from this String starting at a
specified start index, ending at a specified stop index, to a byte array
starting at a specified destination begin index.
|
byte[] | getBytes(String enc)
Converts the Unicode characters in this String to a byte array. |
byte[] | getBytes()
Converts the Unicode characters in this String to a byte array. |
void | getChars(int srcBegin, int srcEnd, char[] dst, int dstBegin)
Copies characters from this String starting at a specified start index,
ending at a specified stop index, to a character array starting at
a specified destination begin index.
|
int | hashCode()
Computes the hashcode for this String. |
int | indexOf(int ch)
Finds the first instance of a character in this String.
|
int | indexOf(int ch, int fromIndex)
Finds the first instance of a character in this String, starting at
a given index. |
int | indexOf(String str)
Finds the first instance of a String in this String.
|
int | indexOf(String str, int fromIndex)
Finds the first instance of a String in this String, starting at
a given index. |
String | intern()
If two Strings are considered equal, by the equals() method,
then intern() will return the same String instance. ie.
if (s1.equals(s2)) then (s1.intern() == s2.intern()).
|
boolean | isEmpty()
Returns true if, and only if, {@link #length()}
is 0 .
|
int | lastIndexOf(int ch)
Finds the last instance of a character in this String.
|
int | lastIndexOf(int ch, int fromIndex)
Finds the last instance of a character in this String, starting at
a given index. |
int | lastIndexOf(String str)
Finds the last instance of a String in this String.
|
int | lastIndexOf(String str, int fromIndex)
Finds the last instance of a String in this String, starting at
a given index. |
int | length()
Returns the number of characters contained in this String.
|
boolean | matches(String regex)
Test if this String matches a regular expression. |
int | offsetByCodePoints(int index, int codePointOffset)
Return the index into this String that is offset from the given index by
codePointOffset code points. |
boolean | regionMatches(int toffset, String other, int ooffset, int len)
Predicate which determines if this String matches another String
starting at a specified offset for each String and continuing
for a specified length. |
boolean | regionMatches(boolean ignoreCase, int toffset, String other, int ooffset, int len)
Predicate which determines if this String matches another String
starting at a specified offset for each String and continuing
for a specified length, optionally ignoring case. |
String | replace(char oldChar, char newChar)
Replaces every instance of a character in this String with a new
character. |
String | replace(CharSequence target, CharSequence replacement)
Returns a string that is this string with all instances of the sequence
represented by target replaced by the sequence in
replacement . |
String | replaceAll(String regex, String replacement)
Replaces all matching substrings of the regular expression with a
given replacement. |
String | replaceFirst(String regex, String replacement)
Replaces the first substring match of the regular expression with a
given replacement. |
String[] | split(String regex, int limit)
Split this string around the matches of a regular expression. |
String[] | split(String regex)
Split this string around the matches of a regular expression. |
boolean | startsWith(String prefix, int toffset)
Predicate which determines if this String contains the given prefix,
beginning comparison at toffset. |
boolean | startsWith(String prefix)
Predicate which determines if this String starts with a given prefix.
|
String | substring(int begin)
Creates a substring of this String, starting at a specified index
and ending at the end of this String.
|
String | substring(int beginIndex, int endIndex)
Creates a substring of this String, starting at a specified index
and ending at one character before a specified index.
|
CharSequence | subSequence(int begin, int end)
Creates a substring of this String, starting at a specified index
and ending at one character before a specified index. |
char[] | toCharArray()
Copies the contents of this String into a character array. |
String | toLowerCase(Locale loc)
Lowercases this String according to a particular locale. |
String | toLowerCase()
Lowercases this String. |
String | toString()
Returns this, as it is already a String!
|
String | toUpperCase(Locale loc)
Uppercases this String according to a particular locale. |
String | toUpperCase()
Uppercases this String. |
String | trim()
Trims all characters less than or equal to ' '
(' ' ) from the beginning and end of this String. |
static String | valueOf(Object obj)
Returns a String representation of an Object. |
static String | valueOf(char[] data)
Returns a String representation of a character array. |
static String | valueOf(char[] data, int offset, int count)
Returns a String representing the character sequence of the char array,
starting at the specified offset, and copying chars up to the specified
count. |
static String | valueOf(boolean b)
Returns a String representing a boolean.
|
static String | valueOf(char c)
Returns a String representing a character.
|
static String | valueOf(int i)
Returns a String representing an integer.
|
static String | valueOf(long l)
Returns a String representing a long.
|
static String | valueOf(float f)
Returns a String representing a float.
|
static String | valueOf(double d)
Returns a String representing a double.
|
String.compareToIgnoreCase(String)
.
This comparator is {@link Serializable}. Note that it ignores Locale,
for that, you want a Collator.
Since: 1.2
See Also: Collator
""
instead.Parameters: str String to copy
Throws: NullPointerException if value is null
Parameters: data char array to copy
Throws: NullPointerException if data is null
Parameters: data char array to copy offset position (base 0) to start copying out of data count the number of characters from data to copy
Throws: NullPointerException if data is null IndexOutOfBoundsException if (offset < 0 || count < 0 || offset + count < 0 (overflow) || offset + count > data.length) (while unspecified, this is a StringIndexOutOfBoundsException)
Deprecated: use {@link #String(byte[], int, int, String)} to perform correct encoding
Creates a new String using an 8-bit array of integer values, starting at an offset, and copying up to the count. Each character c, using corresponding byte b, is created in the new String as if by performing:c = (char) (((hibyte & 0xff) << 8) | (b & 0xff))
Parameters: ascii array of integer values hibyte top byte of each Unicode character offset position (base 0) to start copying out of ascii count the number of characters from ascii to copy
Throws: NullPointerException if ascii is null IndexOutOfBoundsException if (offset < 0 || count < 0 || offset + count < 0 (overflow) || offset + count > ascii.length) (while unspecified, this is a StringIndexOutOfBoundsException)
See Also: (byte[])
(byte[], String)
(byte[], int, int)
(byte[], int, int, String)
Deprecated: use {@link #String(byte[], String)} to perform correct encoding
Creates a new String using an 8-bit array of integer values. Each character c, using corresponding byte b, is created in the new String as if by performing:c = (char) (((hibyte & 0xff) << 8) | (b & 0xff))
Parameters: ascii array of integer values hibyte top byte of each Unicode character
Throws: NullPointerException if ascii is null
See Also: (byte[])
(byte[], String)
(byte[], int, int)
(byte[], int, int, String)
(byte[], int, int, int)
Parameters: data byte array to copy offset the offset to start at count the number of bytes in the array to use encoding the name of the encoding to use
Throws: NullPointerException if data or encoding is null IndexOutOfBoundsException if offset or count is incorrect (while unspecified, this is a StringIndexOutOfBoundsException) UnsupportedEncodingException if encoding is not found Error if the decoding fails
Since: 1.1
Parameters: data byte array to copy encoding the name of the encoding to use
Throws: NullPointerException if data or encoding is null UnsupportedEncodingException if encoding is not found Error if the decoding fails
Since: 1.1
See Also: (byte[], int, int, String)
Parameters: data byte array to copy offset the offset to start at count the number of bytes in the array to use
Throws: NullPointerException if data is null IndexOutOfBoundsException if offset or count is incorrect Error if the decoding fails
Since: 1.1
See Also: (byte[], int, int, String)
Parameters: data byte array to copy
Throws: NullPointerException if data is null Error if the decoding fails
Since: 1.1
See Also: (byte[], int, int)
(byte[], int, int, String)
Parameters: buffer StringBuffer to copy
Throws: NullPointerException if buffer is null
Parameters: buffer StringBuilder to copy
Throws: NullPointerException if buffer is null
Parameters: codePoints the entire array of code points offset the start of the subarray count the length of the subarray
Throws: IllegalArgumentException if an invalid code point is found in the codePoints array IndexOutOfBoundsException if offset is negative or offset + count is greater than the length of the array.
Parameters: index position of character to return (base 0)
Returns: character located at position index
Throws: IndexOutOfBoundsException if index < 0 || index >= length() (while unspecified, this is a StringIndexOutOfBoundsException)
Parameters: index the index of the codepoint to get, starting at 0
Returns: the codepoint at the specified index
Throws: IndexOutOfBoundsException if index is negative or >= length()
Since: 1.5
index-1
and
index-2
to see if they form a supplementary code point.Parameters: index the index just past the codepoint to get, starting at 0
Returns: the codepoint at the specified index
Throws: IndexOutOfBoundsException if index is negative or >= length() (while unspecified, this is a StringIndexOutOfBoundsException)
Since: 1.5
String
. An unpaired surrogate counts as a
code point for this purpose. Characters outside the indicated
range are not examined, even if the range ends in the middle of a
surrogate pair.
Parameters: start the starting index end one past the ending index
Returns: the number of code points
Since: 1.5
this.charAt(k) - anotherString.charAt(k)
if both strings
have characters remaining, or
this.length() - anotherString.length()
if one string is
a subsequence of the other.
Parameters: anotherString the String to compare against
Returns: the comparison
Throws: NullPointerException if anotherString is null
Character.toLowerCase(Character.toUpperCase(c))
on each
character of the string. This is unsatisfactory for locale-based
comparison, in which case you should use {@link java.text.Collator}.
Parameters: str the string to compare against
Returns: the comparison
Since: 1.2
See Also: Collator
Parameters: str String to append to this String
Returns: newly concatenated String
Throws: NullPointerException if str is null
Parameters: s the CharSequence
Returns: true iff this String contains s
Since: 1.5
Parameters: buffer the StringBuffer to compare to
Returns: true if StringBuffer has the same character sequence
Throws: NullPointerException if the given StringBuffer is null
Since: 1.4
Parameters: seq the CharSequence to compare to
Returns: true if CharSequence has the same character sequence
Throws: NullPointerException if the given CharSequence is null
Since: 1.5
Parameters: data character array offset position (base 0) to start copying out of data count the number of characters from data to copy
Returns: String containing the chars from data[offset..offset+count]
Throws: NullPointerException if data is null IndexOutOfBoundsException if (offset < 0 || count < 0 || offset + count < 0 (overflow) || offset + count < 0 (overflow) || offset + count > data.length) (while unspecified, this is a StringIndexOutOfBoundsException)
See Also: (char[], int, int)
Parameters: data the character array
Returns: a String containing the same character sequence as data
Throws: NullPointerException if data is null
See Also: (char[], int, int)
(char[])
Parameters: suffix String to compare
Returns: true if this String ends with the suffix
Throws: NullPointerException if suffix is null
See Also: String
Parameters: anObject the object to compare
Returns: true if anObject is semantically equal to this
See Also: compareTo equalsIgnoreCase
c1 == c2
Character.toUpperCase(c1)
== Character.toUpperCase(c2)
Character.toLowerCase(c1)
== Character.toLowerCase(c2)
Parameters: anotherString String to compare to this String
Returns: true if anotherString is equal, ignoring case
Since: 1.5
Since: 1.5
Deprecated: use {@link #getBytes()}, which uses a char to byte encoder
Copies the low byte of each character from this String starting at a specified start index, ending at a specified stop index, to a byte array starting at a specified destination begin index.Parameters: srcBegin index to being copying characters from this String srcEnd index after the last character to be copied from this String dst byte array which each low byte of this String is copied into dstBegin index to start writing characters into dst
Throws: NullPointerException if dst is null and copy length is non-zero IndexOutOfBoundsException if any indices are out of bounds (while unspecified, source problems cause a StringIndexOutOfBoundsException, and dst problems cause an ArrayIndexOutOfBoundsException)
Parameters: enc encoding name
Returns: the resulting byte array
Throws: NullPointerException if enc is null UnsupportedEncodingException if encoding is not supported
Since: 1.1
Returns: the resulting byte array, or null on a problem
Since: 1.1
Parameters: srcBegin index to begin copying characters from this String srcEnd index after the last character to be copied from this String dst character array which this String is copied into dstBegin index to start writing characters into dst
Throws: NullPointerException if dst is null IndexOutOfBoundsException if any indices are out of bounds (while unspecified, source problems cause a StringIndexOutOfBoundsException, and dst problems cause an ArrayIndexOutOfBoundsException)
s[0]*31**(n-1) + s[1]*31**(n-2) + ... + s[n-1]
.
Returns: hashcode value of this String
Parameters: ch character to find
Returns: location (base 0) of the character, or -1 if not found
Parameters: ch character to find fromIndex index to start the search
Returns: location (base 0) of the character, or -1 if not found
Parameters: str String to find
Returns: location (base 0) of the String, or -1 if not found
Throws: NullPointerException if str is null
Parameters: str String to find fromIndex index to start the search
Returns: location (base 0) of the String, or -1 if not found
Throws: NullPointerException if str is null
Returns: the interned String
0
.
Returns: true if the length of the string is zero.
Since: 1.6
Parameters: ch character to find
Returns: location (base 0) of the character, or -1 if not found
Parameters: ch character to find fromIndex index to start the search
Returns: location (base 0) of the character, or -1 if not found
Parameters: str String to find
Returns: location (base 0) of the String, or -1 if not found
Throws: NullPointerException if str is null
Parameters: str String to find fromIndex index to start the search
Returns: location (base 0) of the String, or -1 if not found
Throws: NullPointerException if str is null
Returns: the length of this String
{@link Pattern}.matches(regex, this)
.
Parameters: regex the pattern to match
Returns: true if the pattern matches
Throws: NullPointerException if regex is null PatternSyntaxException if regex is invalid
Since: 1.4
See Also: Pattern
codePointOffset
code points.Parameters: index the index at which to start codePointOffset the number of code points to offset
Returns: the index into this String that is codePointOffset
code points offset from index
.
Throws: IndexOutOfBoundsException if index is negative or larger than the length of this string. IndexOutOfBoundsException if codePointOffset is positive and the substring starting with index has fewer than codePointOffset code points. IndexOutOfBoundsException if codePointOffset is negative and the substring ending with index has fewer than (-codePointOffset) code points.
Since: 1.5
Parameters: toffset index to start comparison at for this String other String to compare region to this String ooffset index to start comparison at for other len number of characters to compare
Returns: true if regions match (case sensitive)
Throws: NullPointerException if other is null
Character.toLowerCase()
and
Character.toUpperCase()
, not on multi-character
capitalization expansions.
Parameters: ignoreCase true if case should be ignored in comparision toffset index to start comparison at for this String other String to compare region to this String ooffset index to start comparison at for other len number of characters to compare
Returns: true if regions match, false otherwise
Throws: NullPointerException if other is null
Parameters: oldChar the old character to replace newChar the new character
Returns: new String with all instances of oldChar replaced with newChar
target
replaced by the sequence in
replacement
.Parameters: target the sequence to be replaced replacement the sequence used as the replacement
Returns: the string constructed as above
{@link Pattern}
.compile(regex).matcher(this).replaceAll(replacement)
.
Parameters: regex the pattern to match replacement the replacement string
Returns: the modified string
Throws: NullPointerException if regex or replacement is null PatternSyntaxException if regex is invalid
Since: 1.4
See Also: String compile matcher replaceAll
{@link Pattern}
.compile(regex).matcher(this).replaceFirst(replacement)
.
Parameters: regex the pattern to match replacement the replacement string
Returns: the modified string
Throws: NullPointerException if regex or replacement is null PatternSyntaxException if regex is invalid
Since: 1.4
See Also: String compile matcher replaceFirst
The limit affects the length of the array. If it is positive, the array will contain at most n elements (n - 1 pattern matches). If negative, the array length is unlimited, but there can be trailing empty entries. if 0, the array length is unlimited, and trailing empty entries are discarded.
For example, splitting "boo:and:foo" yields:
Regex | Limit | Result |
":" | 2 | { "boo", "and:foo" } |
":" | t | { "boo", "and", "foo" } |
":" | -2 | { "boo", "and", "foo" } |
"o" | 5 | { "b", "", ":and:f", "", "" } |
"o" | -2 | { "b", "", ":and:f", "", "" } |
"o" | 0 | { "b", "", ":and:f" } |
This is shorthand for
{@link Pattern}.compile(regex).split(this, limit)
.
Parameters: regex the pattern to match limit the limit threshold
Returns: the array of split strings
Throws: NullPointerException if regex or replacement is null PatternSyntaxException if regex is invalid
Since: 1.4
split(regex, 0)
.
Parameters: regex the pattern to match
Returns: the array of split strings
Throws: NullPointerException if regex or replacement is null PatternSyntaxException if regex is invalid
Since: 1.4
this.substring(toffset).startsWith(prefix)
.
Parameters: prefix String to compare toffset offset for this String where comparison starts
Returns: true if this String starts with prefix
Throws: NullPointerException if prefix is null
See Also: String
Parameters: prefix String to compare
Returns: true if this String starts with the prefix
Throws: NullPointerException if prefix is null
See Also: String
Parameters: begin index to start substring (base 0)
Returns: new String which is a substring of this String
Throws: IndexOutOfBoundsException if begin < 0 || begin > length() (while unspecified, this is a StringIndexOutOfBoundsException)
Parameters: beginIndex index to start substring (inclusive, base 0) endIndex index to end at (exclusive)
Returns: new String which is a substring of this String
Throws: IndexOutOfBoundsException if begin < 0 || end > length() || begin > end (while unspecified, this is a StringIndexOutOfBoundsException)
substring(begin, end)
.
Parameters: begin index to start substring (inclusive, base 0) end index to end at (exclusive)
Returns: new String which is a substring of this String
Throws: IndexOutOfBoundsException if begin < 0 || end > length() || begin > end
Since: 1.4
Returns: character array copying the String
Parameters: loc locale to use
Returns: new lowercased String, or this if no characters were lowercased
Throws: NullPointerException if loc is null
Since: 1.1
See Also: toUpperCase
Returns: new lowercased String, or this if no characters were lowercased
See Also: toLowerCase toUpperCase
Returns: this
Parameters: loc locale to use
Returns: new uppercased String, or this if no characters were uppercased
Throws: NullPointerException if loc is null
Since: 1.1
See Also: toLowerCase
Returns: new uppercased String, or this if no characters were uppercased
See Also: toUpperCase toLowerCase
' '
(' '
) from the beginning and end of this String. This
includes many, but not all, ASCII control characters, and all
{@link Character#isWhitespace(char)}.
Returns: new trimmed String, or this if nothing trimmed
obj.toString()
(which
can be null).
Parameters: obj the Object
Returns: the string conversion of obj
Parameters: data the character array
Returns: a String containing the same character sequence as data
Throws: NullPointerException if data is null
See Also: (char[], int, int)
(char[])
Parameters: data character array offset position (base 0) to start copying out of data count the number of characters from data to copy
Returns: String containing the chars from data[offset..offset+count]
Throws: NullPointerException if data is null IndexOutOfBoundsException if (offset < 0 || count < 0 || offset + count > data.length) (while unspecified, this is a StringIndexOutOfBoundsException)
See Also: (char[], int, int)
Parameters: b the boolean
Returns: "true" if b is true, else "false"
Parameters: c the character
Returns: String containing the single character c
Parameters: i the integer
Returns: String containing the integer in base 10
See Also: Integer
Parameters: l the long
Returns: String containing the long in base 10
See Also: Long
Parameters: f the float
Returns: String containing the float
See Also: Float
Parameters: d the double
Returns: String containing the double
See Also: Double