java.lang.Object | |
↳ | java.lang.String |
An immutable sequence of UTF-16 char
s.
See Character
for details about the relationship between char
and
Unicode code points.
Backing Arrays
This class is implemented using a char[]
. The length of the array may exceed
the length of the string. For example, the string "Hello" may be backed by
the array ['H', 'e', 'l', 'l', 'o', 'W'. 'o', 'r', 'l', 'd']
with
offset 0 and length 5.
Multiple strings can share the same char[]
because strings are immutable.
The substring(int)
method always returns a string that
shares the backing array of its source string. Generally this is an
optimization: fewer char[]
s need to be allocated, and less copying
is necessary. But this can also lead to unwanted heap retention. Taking a
short substring of long string means that the long shared char[]
won't be
garbage until both strings are garbage. This typically happens when parsing
small substrings out of a large input. To avoid this where necessary, call
new String(longString.subString(...))
. The string copy constructor
always ensures that the backing array is no larger than necessary.
Fields | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
CASE_INSENSITIVE_ORDER |
Compares strings using compareToIgnoreCase(String) .
|
Public Constructors | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
Creates an empty string.
| |||||||||||
Converts the byte array to a string using the system's
default charset .
| |||||||||||
This constructor was deprecated
in API level 1.
Use
String(byte[]) or String(byte[], String) instead.
| |||||||||||
Converts a subsequence of the byte array to a string using the system's
default charset .
| |||||||||||
This constructor was deprecated
in API level 1.
Use
String(byte[], int, int) instead.
| |||||||||||
Converts the byte array to a string using the named charset.
| |||||||||||
Converts the byte array to a string using the named charset.
| |||||||||||
Converts the byte array to a string using the given charset.
| |||||||||||
Converts the byte array to a String using the given charset.
| |||||||||||
Initializes this string to contain the given
char s.
| |||||||||||
Initializes this string to contain the given
char s.
| |||||||||||
Constructs a copy of the given string.
| |||||||||||
Creates a
String from the contents of the specified
StringBuffer .
| |||||||||||
Creates a
String from the sub-array of Unicode code points.
| |||||||||||
Creates a
String from the contents of the specified StringBuilder .
|
Public Methods | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
Returns the
char at index .
| |||||||||||
Returns the Unicode code point at the given
index .
| |||||||||||
Returns the Unicode code point that precedes the given
index .
| |||||||||||
Calculates the number of Unicode code points between
start
and end .
| |||||||||||
Compares this string to the given string.
| |||||||||||
Compares this string to the given string, ignoring case differences.
| |||||||||||
Concatenates this string and the specified string.
| |||||||||||
Returns true if this string contains the
chars s from the given CharSequence .
| |||||||||||
Returns true if the
char s in the given CharSequence are the same
as those in this string.
| |||||||||||
Returns true if the
char s in the given StringBuffer are the same
as those in this string.
| |||||||||||
Creates a new string by copying the given subsequence of the given
char[] .
| |||||||||||
Creates a new string by copying the given
char[] .
| |||||||||||
Compares the specified string to this string to determine if the
specified string is a suffix.
| |||||||||||
Compares the given object to this string and returns true if they are
equal.
| |||||||||||
Compares the given string to this string ignoring case.
| |||||||||||
Returns a formatted string, using the supplied format and arguments,
localized to the given locale.
| |||||||||||
Returns a localized formatted string, using the supplied format and arguments,
using the user's default locale.
| |||||||||||
This method was deprecated
in API level 1.
Use
getBytes() or getBytes(String)
| |||||||||||
Returns a new byte array containing the code points of this string encoded using the
named charset.
| |||||||||||
Returns a new byte array containing the code points of this string encoded using the
given charset.
| |||||||||||
Returns a new byte array containing the code points in this string encoded using the
system's
default charset .
| |||||||||||
Copies the given subsequence of this string to the given array
starting at the given offset.
| |||||||||||
Returns an integer hash code for this object.
| |||||||||||
Returns the first index of the given code point, or -1.
| |||||||||||
Returns the next index of the given code point, or -1.
| |||||||||||
Returns the next index of the given string in this string, or -1.
| |||||||||||
Returns the first index of the given string, or -1.
| |||||||||||
Returns an interned string equal to this string.
| |||||||||||
Returns true if the length of this string is 0.
| |||||||||||
Returns the index of the start of the last match for the given string in this string, or -1.
| |||||||||||
Returns the last index of the code point
c , or -1.
| |||||||||||
Returns the last index of the code point
c , or -1.
| |||||||||||
Returns the index of the start of the previous match for the given string in this string,
or -1.
| |||||||||||
Returns the number of
char s in this string.
| |||||||||||
Tests whether this string matches the given
regularExpression .
| |||||||||||
Returns the index within this object that is offset from
index by
codePointOffset code points.
| |||||||||||
Returns true if the given subsequence of the given string matches this string starting
at the given offset.
| |||||||||||
Returns true if the given subsequence of the given string matches this string starting
at the given offset.
| |||||||||||
Returns a copy of this string after replacing occurrences of
target replaced
with replacement .
| |||||||||||
Returns a copy of this string after replacing occurrences of the given
char with another.
| |||||||||||
Replaces all matches for
regularExpression within this string with the given
replacement .
| |||||||||||
Replaces the first match for
regularExpression within this string with the given
replacement .
| |||||||||||
Splits this string using the supplied
regularExpression .
| |||||||||||
Splits this string using the supplied
regularExpression .
| |||||||||||
Compares the specified string to this string to determine if the
specified string is a prefix.
| |||||||||||
Compares the specified string to this string, starting at the specified
offset, to determine if the specified string is a prefix.
| |||||||||||
Equivalent to
substring(int, int) but needed to implement CharSequence .
| |||||||||||
Returns a string containing a suffix of this string starting at
start .
| |||||||||||
Returns a string containing the given subsequence of this string.
| |||||||||||
Returns a new
char array containing a copy of the char s in this string.
| |||||||||||
Converts this string to lower case, using the rules of
locale .
| |||||||||||
Converts this string to lower case, using the rules of the user's default locale.
| |||||||||||
Returns this string.
| |||||||||||
Converts this this string to upper case, using the rules of
locale .
| |||||||||||
Converts this this string to upper case, using the rules of the user's default locale.
| |||||||||||
Returns a string with no code points
<= \\u0020 at
the beginning or end.
| |||||||||||
Returns the string representation of the given long.
| |||||||||||
Converts the specified object to its string representation.
| |||||||||||
Returns a new string containing the same
char s as the given
array.
| |||||||||||
Returns the string representation of the given double.
| |||||||||||
Returns the string representation of the given int.
| |||||||||||
Returns the string representation of the given float.
| |||||||||||
Returns a new string containing the same
char s as the given
subset of the given array.
| |||||||||||
Converts the specified boolean to its string representation.
| |||||||||||
Returns a new string of just the given
char .
|
[Expand]
Inherited Methods | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
From class
java.lang.Object
| |||||||||||
From interface
java.lang.Comparable
| |||||||||||
From interface
java.lang.CharSequence
|
Compares strings using compareToIgnoreCase(String)
.
This is not suitable for case-insensitive string comparison for all locales.
Use a Collator
instead.
Converts the byte array to a string using the system's
default charset
.
This constructor was deprecated
in API level 1.
Use String(byte[])
or String(byte[], String)
instead.
Converts the byte array to a string, setting the high byte of every
char
to the specified value.
data | the byte array to convert to a string. |
---|---|
high | the high byte to use. |
NullPointerException | if data == null . |
---|
Converts a subsequence of the byte array to a string using the system's
default charset
.
NullPointerException | if data == null . |
---|---|
IndexOutOfBoundsException | if byteCount < 0 || offset < 0 || offset + byteCount > data.length .
|
This constructor was deprecated
in API level 1.
Use String(byte[], int, int)
instead.
Converts the byte array to a string, setting the high byte of every
char
to high
.
NullPointerException | if data == null . |
---|---|
IndexOutOfBoundsException | if byteCount < 0 || offset < 0 || offset + byteCount > data.length |
Converts the byte array to a string using the named charset.
The behavior when the bytes cannot be decoded by the named charset
is unspecified. Use CharsetDecoder
for more control.
NullPointerException | if data == null . |
---|---|
IndexOutOfBoundsException | if byteCount < 0 || offset < 0 || offset + byteCount > data.length . |
UnsupportedEncodingException | if the named charset is not supported. |
Converts the byte array to a string using the named charset.
The behavior when the bytes cannot be decoded by the named charset
is unspecified. Use CharsetDecoder
for more control.
NullPointerException | if data == null . |
---|---|
UnsupportedEncodingException | if charsetName is not supported.
|
Converts the byte array to a string using the given charset.
The behavior when the bytes cannot be decoded by the given charset
is to replace malformed input and unmappable code points with the charset's default
replacement string. Use CharsetDecoder
for more control.
IndexOutOfBoundsException | if byteCount < 0 || offset < 0 || offset + byteCount > data.length |
---|---|
NullPointerException | if data == null |
Converts the byte array to a String using the given charset.
NullPointerException | if data == null |
---|
Initializes this string to contain the given char
s.
Modifying the array after creating the string
has no effect on the string.
NullPointerException | if data == null
|
---|
Initializes this string to contain the given char
s.
Modifying the array after creating the string
has no effect on the string.
NullPointerException | if data == null . |
---|---|
IndexOutOfBoundsException | if charCount < 0 || offset < 0 || offset + charCount > data.length
|
Constructs a copy of the given string. The returned string's backing array is no larger than necessary.
Creates a String
from the contents of the specified
StringBuffer
.
Creates a String
from the sub-array of Unicode code points.
NullPointerException | if codePoints == null . |
---|---|
IllegalArgumentException | if any of the elements of codePoints are not valid
Unicode code points. |
IndexOutOfBoundsException | if offset or count are not within the bounds
of codePoints . |
Creates a String
from the contents of the specified StringBuilder
.
NullPointerException | if stringBuilder == null . |
---|
Returns the char
at index
.
IndexOutOfBoundsException | if index < 0 or index >= length() .
|
---|
Returns the Unicode code point at the given index
.
IndexOutOfBoundsException | if index < 0 || index >= length() |
---|
Returns the Unicode code point that precedes the given index
.
IndexOutOfBoundsException | if index < 1 || index > length() |
---|
Calculates the number of Unicode code points between start
and end
.
start | the inclusive beginning index of the subsequence. |
---|---|
end | the exclusive end index of the subsequence. |
IndexOutOfBoundsException | if start < 0 || end > length() || start > end |
---|
Compares this string to the given string.
The strings are compared one char
at a time.
In the discussion of the return value below, note that char
does not
mean code point, though this should only be visible for surrogate pairs.
If there is an index at which the two strings differ, the result is
the difference between the two char
s at the lowest such index.
If not, but the lengths of the strings differ, the result is the difference
between the two strings' lengths.
If the strings are the same length and every char
is the same, the result is 0.
NullPointerException | if string is null .
|
---|
Compares this string to the given string, ignoring case differences.
The strings are compared one char
at a time. This is not suitable
for case-insensitive string comparison for all locales.
Use a Collator
instead.
If there is an index at which the two strings differ, the result is
the difference between the two char
s at the lowest such index.
If not, but the lengths of the strings differ, the result is the difference
between the two strings' lengths.
If the strings are the same length and every char
is the same, the result is 0.
NullPointerException | if string is null .
|
---|
Concatenates this string and the specified string.
string | the string to concatenate |
---|
Returns true if this string contains the chars
s from the given CharSequence
.
Returns true if the char
s in the given CharSequence
are the same
as those in this string.
Returns true if the char
s in the given StringBuffer
are the same
as those in this string.
NullPointerException | if sb is null . |
---|
Creates a new string by copying the given subsequence of the given char[]
.
Modifying the array after creating the string has no
effect on the string.
NullPointerException | if data is null . |
---|---|
IndexOutOfBoundsException | if length < 0, start < 0 or start + length >
data.length .
|
Creates a new string by copying the given char[]
.
Modifying the array after creating the string has no
effect on the string.
NullPointerException | if data is null .
|
---|
Compares the specified string to this string to determine if the specified string is a suffix.
NullPointerException | if suffix is null .
|
---|
Compares the given object to this string and returns true if they are
equal. The object must be an instance of String
with the same length,
where for every index, charAt
on each string returns the same value.
other | the object to compare this instance with. |
---|
true
if the specified object is equal to this Object
; false
otherwise.Compares the given string to this string ignoring case.
The strings are compared one char
at a time. This is not suitable
for case-insensitive string comparison for all locales.
Use a Collator
instead.
Returns a formatted string, using the supplied format and arguments, localized to the given locale.
locale | the locale to apply; null value means no localization. |
---|---|
format | the format string (see format(String, Object...) ) |
args | the list of arguments passed to the formatter. If there are
more arguments than required by format ,
additional arguments are ignored. |
NullPointerException | if format == null |
---|---|
IllegalFormatException | if the format is invalid. |
Returns a localized formatted string, using the supplied format and arguments, using the user's default locale.
If you're formatting a string other than for human
consumption, you should use the format(Locale, String, Object...)
overload and supply Locale.US
. See
"Be wary of the default locale".
format | the format string (see format(String, Object...) ) |
---|---|
args | the list of arguments passed to the formatter. If there are
more arguments than required by format ,
additional arguments are ignored. |
NullPointerException | if format == null |
---|---|
IllegalFormatException | if the format is invalid. |
This method was deprecated
in API level 1.
Use getBytes()
or getBytes(String)
Mangles a subsequence of this string into a byte array by stripping the high order bits from
each char
. Use getBytes()
or getBytes(String)
instead.
start | the start offset in this string. |
---|---|
end | the end+1 offset in this string. |
data | the destination byte array. |
index | the start offset in the destination byte array. |
NullPointerException | if data is null . |
---|---|
IndexOutOfBoundsException | if start < 0 , end > length() , index <
0 or end - start > data.length - index . |
Returns a new byte array containing the code points of this string encoded using the named charset.
The behavior when this string cannot be represented in the named charset
is unspecified. Use CharsetEncoder
for more control.
UnsupportedEncodingException | if the charset is not supported |
---|
Returns a new byte array containing the code points of this string encoded using the given charset.
The behavior when this string cannot be represented in the given charset
is to replace malformed input and unmappable code points with the charset's default
replacement byte array. Use CharsetEncoder
for more control.
Returns a new byte array containing the code points in this string encoded using the
system's default charset
.
The behavior when this string cannot be represented in the system's default charset is unspecified. In practice, when the default charset is UTF-8 (as it is on Android), all strings can be encoded.
Copies the given subsequence of this string to the given array starting at the given offset.
start | the start offset in this string. |
---|---|
end | the end+1 offset in this string. |
buffer | the destination array. |
index | the start offset in the destination array. |
NullPointerException | if buffer is null . |
---|---|
IndexOutOfBoundsException | if start < 0 , end > length() , start >
end , index < 0 , end - start > buffer.length -
index
|
Returns an integer hash code for this object. By contract, any two
objects for which equals(Object)
returns true
must return
the same hash code value. This means that subclasses of Object
usually override both methods or neither method.
Note that hash values must not change over time unless information used in equals comparisons also changes.
See Writing a correct
hashCode
method
if you intend implementing your own hashCode
method.
Returns the first index of the given code point, or -1. The search starts at the beginning and moves towards the end of this string.
Returns the next index of the given code point, or -1. The search starts at the given offset and moves towards the end of this string.
Returns the next index of the given string in this string, or -1. The search for the string starts at the given offset and moves towards the end of this string.
NullPointerException | if subString is null .
|
---|
Returns the first index of the given string, or -1. The search starts at the beginning and moves towards the end of this string.
NullPointerException | if string is null .
|
---|
Returns an interned string equal to this string. The VM maintains an internal set of
unique strings. All string literals found in loaded classes'
constant pools are automatically interned. Manually-interned strings are only weakly
referenced, so calling intern
won't lead to unwanted retention.
Interning is typically used because it guarantees that for interned strings
a
and b
, a.equals(b)
can be simplified to
a == b
. (This is not true of non-interned strings.)
Many applications find it simpler and more convenient to use an explicit
HashMap
to implement their own pools.
Returns the index of the start of the last match for the given string in this string, or -1. The search for the string starts at the end and moves towards the beginning of this string.
NullPointerException | if string is null .
|
---|
Returns the last index of the code point c
, or -1.
The search starts at offset start
and moves towards
the beginning of this string.
Returns the last index of the code point c
, or -1.
The search starts at the end and moves towards the
beginning of this string.
Returns the index of the start of the previous match for the given string in this string, or -1. The search for the string starts at the given index and moves towards the beginning of this string.
NullPointerException | if subString is null .
|
---|
Returns the number of char
s in this string. If this string contains surrogate pairs,
this is not the same as the number of code points.
Tests whether this string matches the given regularExpression
. This method returns
true only if the regular expression matches the entire input string. A common mistake is
to assume that this method behaves like contains(CharSequence)
; if you want to match anywhere
within the input string, you need to add .*
to the beginning and end of your
regular expression. See matches(String, CharSequence)
.
If the same regular expression is to be used for multiple operations, it may be more
efficient to reuse a compiled Pattern
.
if the syntax of the supplied regular expression is not valid. | |
NullPointerException | if regularExpression == null |
Returns the index within this object that is offset from index
by
codePointOffset
code points.
index | the index within this object to calculate the offset from. |
---|---|
codePointOffset | the number of code points to count. |
IndexOutOfBoundsException | if index is negative or greater than length()
or if there aren't enough code points before or after index to match codePointOffset . |
---|
Returns true if the given subsequence of the given string matches this string starting at the given offset.
If ignoreCase is true, case is ignored during the comparison.
The strings are compared one char
at a time. This is not suitable
for case-insensitive string comparison for all locales.
Use a Collator
instead.
ignoreCase | specifies if case should be ignored (use Collator instead for
non-ASCII case insensitivity). |
---|---|
thisStart | the start offset in this string. |
string | the other string. |
start | the start offset in string . |
length | the number of char s to compare. |
NullPointerException | if string is null .
|
---|
Returns true if the given subsequence of the given string matches this string starting at the given offset.
thisStart | the start offset in this string. |
---|---|
string | the other string. |
start | the start offset in string . |
length | the number of char s to compare. |
NullPointerException | if string is null .
|
---|
Returns a copy of this string after replacing occurrences of target
replaced
with replacement
. The string is processed from the beginning to the
end.
NullPointerException | if target or replacement is null .
|
---|
Returns a copy of this string after replacing occurrences of the given char
with another.
Replaces all matches for regularExpression
within this string with the given
replacement
.
See Pattern
for regular expression syntax.
If the same regular expression is to be used for multiple operations, it may be more
efficient to reuse a compiled Pattern
.
if the syntax of the supplied regular expression is not valid. | |
NullPointerException | if regularExpression == null |
Replaces the first match for regularExpression
within this string with the given
replacement
.
See Pattern
for regular expression syntax.
If the same regular expression is to be used for multiple operations, it may be more
efficient to reuse a compiled Pattern
.
if the syntax of the supplied regular expression is not valid. | |
NullPointerException | if regularExpression == null |
Splits this string using the supplied regularExpression
.
Equivalent to split(regularExpression, 0)
.
See split(CharSequence, int)
for an explanation of limit
.
See Pattern
for regular expression syntax.
If the same regular expression is to be used for multiple operations, it may be more
efficient to reuse a compiled Pattern
.
NullPointerException | if regularExpression == null |
---|---|
if the syntax of the supplied regular expression is not valid. |
Splits this string using the supplied regularExpression
.
See split(CharSequence, int)
for an explanation of limit
.
See Pattern
for regular expression syntax.
If the same regular expression is to be used for multiple operations, it may be more
efficient to reuse a compiled Pattern
.
NullPointerException | if regularExpression == null |
---|---|
if the syntax of the supplied regular expression is not valid. |
Compares the specified string to this string to determine if the specified string is a prefix.
prefix | the string to look for. |
---|
true
if the specified string is a prefix of this string,
false
otherwiseNullPointerException | if prefix is null .
|
---|
Compares the specified string to this string, starting at the specified offset, to determine if the specified string is a prefix.
prefix | the string to look for. |
---|---|
start | the starting offset. |
true
if the specified string occurs in this string at the
specified offset, false
otherwise.NullPointerException | if prefix is null .
|
---|
Equivalent to substring(int, int)
but needed to implement CharSequence
.
start | the start offset of the sub-sequence. It is inclusive, that is, the index of the first character that is included in the sub-sequence. |
---|---|
end | the end offset of the sub-sequence. It is exclusive, that is, the index of the first character after those that are included in the sub-sequence |
IndexOutOfBoundsException | if start < 0 , end < 0 , start > end or
end > length() . |
---|
Returns a string containing a suffix of this string starting at start
.
The returned string shares this string's backing array.
IndexOutOfBoundsException | if start < 0 or start > length() .
|
---|
Returns a string containing the given subsequence of this string. The returned string shares this string's backing array.
start | the start offset. |
---|---|
end | the end+1 offset. |
IndexOutOfBoundsException | if start < 0 , start > end or end > length() .
|
---|
Returns a new char
array containing a copy of the char
s in this string.
This is expensive and rarely useful. If you just want to iterate over the char
s in
the string, use charAt(int)
instead.
Converts this string to lower case, using the rules of locale
.
Most case mappings are unaffected by the language of a Locale
. Exceptions include
dotted and dotless I in Azeri and Turkish locales, and dotted and dotless I and J in
Lithuanian locales. On the other hand, it isn't necessary to provide a Greek locale to get
correct case mapping of Greek characters: any locale will do.
See http://www.unicode.org/Public/UNIDATA/SpecialCasing.txt for full details of context- and language-specific special cases.
this
if it's already all lower case.
Converts this string to lower case, using the rules of the user's default locale. See "Be wary of the default locale".
this
if it's already all lower case.
Returns this string.
Converts this this string to upper case, using the rules of locale
.
Most case mappings are unaffected by the language of a Locale
. Exceptions include
dotted and dotless I in Azeri and Turkish locales, and dotted and dotless I and J in
Lithuanian locales. On the other hand, it isn't necessary to provide a Greek locale to get
correct case mapping of Greek characters: any locale will do.
See http://www.unicode.org/Public/UNIDATA/SpecialCasing.txt for full details of context- and language-specific special cases.
this
if it's already all upper case.
Converts this this string to upper case, using the rules of the user's default locale. See "Be wary of the default locale".
this
if it's already all upper case.
Returns a string with no code points <= \\u0020
at
the beginning or end.
Returns the string representation of the given long.
Converts the specified object to its string representation. If the object
is null return the string "null"
, otherwise use toString()
to get the string representation.
value | the object. |
---|
"null"
.
Returns a new string containing the same char
s as the given
array. Modifying the array after creating the string has no
effect on the string.
NullPointerException | if data is null .
|
---|
Returns the string representation of the given double.
Returns the string representation of the given int.
Returns the string representation of the given float.
Returns a new string containing the same char
s as the given
subset of the given array. Modifying the array after creating the string has no
effect on the string.
IndexOutOfBoundsException | if length < 0 , start < 0 or start + length > data.length |
---|---|
NullPointerException | if data is null .
|
Converts the specified boolean to its string representation. When the
boolean is true
return "true"
, otherwise return "false"
.
value | the boolean. |
---|
Returns a new string of just the given char
.