Class String
- All Implemented Interfaces:
Serializable,CharSequence,Comparable<String>
public final class String extends Object implements Serializable, Comparable<String>, CharSequence
chars). A
String is represented by array of UTF-16 values, such that
Unicode supplementary characters (code points) are stored/encoded as
surrogate pairs via Unicode code units (char).
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 character arrays 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.
- Since:
- 1.0
- See Also:
StringBuffer,StringBuilder,Charset, Serialized Form
-
Field Summary
Fields Modifier and Type Field Description static Comparator<String>CASE_INSENSITIVE_ORDERA comparator ignoring the case of the characters. -
Constructor Summary
Constructors Constructor Description String()Creates an empty string.String(byte[] data)Converts the byte array to a string using the system'sdefault charset.String(byte[] data, int high)Deprecated.String(byte[] data, int offset, int byteCount)Converts a subsequence of the byte array to a string using the system'sdefault charset.String(byte[] data, int high, int offset, int byteCount)Deprecated.UseString(byte[], int, int)instead.String(byte[] data, int offset, int byteCount, String charsetName)Converts the byte array to a string using the named charset.String(byte[] data, int offset, int byteCount, Charset charset)Converts the byte array to a string using the given charset.String(byte[] data, String charsetName)Converts the byte array to a string using the named charset.String(byte[] data, Charset charset)Converts the byte array to a String using the given charset.String(char[] data)Initializes this string to contain the characters in the specified character array.String(char[] data, int offset, int charCount)Initializes this string to contain the specified characters in the character array.String(int[] codePoints, int offset, int count)Creates aStringfrom the sub-array of Unicode code points.String(String toCopy)Constructs a new string with the same sequence of characters astoCopy.String(StringBuffer stringBuffer)Creates aStringfrom the contents of the specifiedStringBuffer.String(StringBuilder stringBuilder)Creates aStringfrom the contents of the specifiedStringBuilder. -
Method Summary
Modifier and Type Method Description charcharAt(int index)Returns the character at the specified offset in this string.intcodePointAt(int index)Returns the Unicode code point at the givenindex.intcodePointBefore(int index)Returns the Unicode code point that precedes the givenindex.intcodePointCount(int start, int end)Calculates the number of Unicode code points betweenstartandend.intcompareTo(String string)Compares the specified string to this string using the Unicode values of the characters.intcompareToIgnoreCase(String string)Compares the specified string to this string using the Unicode values of the characters, ignoring case differences.Stringconcat(String string)Concatenates this string and the specified string.booleancontains(CharSequence cs)Determines if thisStringcontains the sequence of characters in theCharSequencepassed.booleancontentEquals(CharSequence cs)Compares aCharSequenceto thisStringto determine if their contents are equal.booleancontentEquals(StringBuffer strbuf)Returns whether the characters in the StringBufferstrbufare the same as those in this string.static StringcopyValueOf(char[] data)Creates a new string containing the characters in the specified character array.static StringcopyValueOf(char[] data, int start, int length)Creates a new string containing the specified characters in the character array.booleanendsWith(String suffix)Compares the specified string to this string to determine if the specified string is a suffix.booleanequals(Object object)Compares the specified object to this string and returns true if they are equal.booleanequalsIgnoreCase(String string)Compares the specified string to this string ignoring the case of the characters and returns true if they are equal.static Stringformat(String format, Object... args)Returns a localized formatted string, using the supplied format and arguments, using the user's default locale.static Stringformat(Locale locale, String format, Object... args)Returns a formatted string, using the supplied format and arguments, localized to the given locale.byte[]getBytes()Returns a new byte array containing the characters of this string encoded using the system'sdefault charset.voidgetBytes(int start, int end, byte[] data, int index)Deprecated.UsegetBytes()orgetBytes(String)instead.byte[]getBytes(String charsetName)Returns a new byte array containing the characters of this string encoded using the named charset.byte[]getBytes(Charset charset)Returns a new byte array containing the characters of this string encoded using the given charset.voidgetChars(int start, int end, char[] buffer, int index)Copies the specified characters in this string to the character array starting at the specified offset in the character array.inthashCode()Returns an integer hash code for this object.intindexOf(int c)Searches in this string for the first index of the specified character.intindexOf(int c, int start)Searches in this string for the index of the specified character.intindexOf(String string)Searches in this string for the first index of the specified string.intindexOf(String subString, int start)Searches in this string for the index of the specified string.Stringintern()Returns an interned string equal to this string.booleanisEmpty()Returns true if the length of this string is 0.intlastIndexOf(int c)Returns the last index of the code pointc, or -1.intlastIndexOf(int c, int start)Returns the last index of the code pointc, or -1.intlastIndexOf(String string)Searches in this string for the last index of the specified string.intlastIndexOf(String subString, int start)Searches in this string for the index of the specified string.intlength()Returns the number of characters in this string.booleanmatches(String regularExpression)Tests whether this string matches the givenregularExpression.intoffsetByCodePoints(int index, int codePointOffset)Returns the index within this object that is offset fromindexbycodePointOffsetcode points.booleanregionMatches(boolean ignoreCase, int thisStart, String string, int start, int length)Compares the specified string to this string and compares the specified range of characters to determine if they are the same.booleanregionMatches(int thisStart, String string, int start, int length)Compares the specified string to this string and compares the specified range of characters to determine if they are the same.Stringreplace(char oldChar, char newChar)Copies this string replacing occurrences of the specified character with another character.Stringreplace(CharSequence target, CharSequence replacement)Copies this string replacing occurrences of the specified target sequence with another sequence.StringreplaceAll(String regularExpression, String replacement)Replaces all matches forregularExpressionwithin this string with the givenreplacement.StringreplaceFirst(String regularExpression, String replacement)Replaces the first match forregularExpressionwithin this string with the givenreplacement.String[]split(String regularExpression)Splits this string using the suppliedregularExpression.String[]split(String regularExpression, int limit)Splits this string using the suppliedregularExpression.booleanstartsWith(String prefix)Compares the specified string to this string to determine if the specified string is a prefix.booleanstartsWith(String prefix, int start)Compares the specified string to this string, starting at the specified offset, to determine if the specified string is a prefix.CharSequencesubSequence(int start, int end)Has the same result as the substring function, but is present so that string may implement the CharSequence interface.Stringsubstring(int start)Returns a string containing a suffix of this string.Stringsubstring(int start, int end)Returns a string containing a subsequence of characters from this string.char[]toCharArray()Returns a newchararray containing a copy of the characters in this string.StringtoLowerCase()Converts this string to lower case, using the rules of the user's default locale.StringtoLowerCase(Locale locale)Converts this string to lower case, using the rules oflocale.StringtoString()Returns this string.StringtoUpperCase()Converts this this string to upper case, using the rules of the user's default locale.StringtoUpperCase(Locale locale)Converts this this string to upper case, using the rules oflocale.Stringtrim()Copies this string removing white space characters from the beginning and end of the string.static StringvalueOf(boolean value)Converts the specified boolean to its string representation.static StringvalueOf(char value)Converts the specified character to its string representation.static StringvalueOf(char[] data)Creates a new string containing the characters in the specified character array.static StringvalueOf(char[] data, int start, int length)Creates a new string containing the specified characters in the character array.static StringvalueOf(double value)Converts the specified double to its string representation.static StringvalueOf(float value)Converts the specified float to its string representation.static StringvalueOf(int value)Converts the specified integer to its string representation.static StringvalueOf(long value)Converts the specified long to its string representation.static StringvalueOf(Object value)Converts the specified object to its string representation.
-
Field Details
-
CASE_INSENSITIVE_ORDER
A comparator ignoring the case of the characters.
-
-
Constructor Details
-
String
public String()Creates an empty string. -
String
public String(byte[] data)Converts the byte array to a string using the system'sdefault charset. -
String
Deprecated.UseString(byte[])orString(byte[], String)instead.Converts the byte array to a string, setting the high byte of every character to the specified value.- Parameters:
data- the byte array to convert to a string.high- the high byte to use.- Throws:
NullPointerException- ifdata == null.
-
String
public String(byte[] data, int offset, int byteCount)Converts a subsequence of the byte array to a string using the system'sdefault charset.- Throws:
NullPointerException- ifdata == null.IndexOutOfBoundsException- ifbyteCount < 0 || offset < 0 || offset + byteCount > data.length.
-
String
Deprecated.UseString(byte[], int, int)instead.Converts the byte array to a string, setting the high byte of every character tohigh.- Throws:
NullPointerException- ifdata == null.IndexOutOfBoundsException- ifbyteCount < 0 || offset < 0 || offset + byteCount > data.length
-
String
public String(byte[] data, int offset, int byteCount, String charsetName) throws UnsupportedEncodingExceptionConverts 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
CharsetDecoderfor more control.- Throws:
NullPointerException- ifdata == null.IndexOutOfBoundsException- ifbyteCount < 0 || offset < 0 || offset + byteCount > data.length.UnsupportedEncodingException- if the named charset is not supported.
-
String
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
CharsetDecoderfor more control.- Throws:
NullPointerException- ifdata == null.UnsupportedEncodingException- ifcharsetNameis not supported.
-
String
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 characters with the charset's default replacement string. Use
CharsetDecoderfor more control.- Throws:
IndexOutOfBoundsException- ifbyteCount < 0 || offset < 0 || offset + byteCount > data.lengthNullPointerException- ifdata == null- Since:
- 1.6
-
String
Converts the byte array to a String using the given charset.- Throws:
NullPointerException- ifdata == null- Since:
- 1.6
-
String
public String(char[] data)Initializes this string to contain the characters in the specified character array. Modifying the character array after creating the string has no effect on the string.- Throws:
NullPointerException- ifdata == null
-
String
public String(char[] data, int offset, int charCount)Initializes this string to contain the specified characters in the character array. Modifying the character array after creating the string has no effect on the string.- Throws:
NullPointerException- ifdata == null.IndexOutOfBoundsException- ifcharCount < 0 || offset < 0 || offset + charCount > data.length
-
String
Constructs a new string with the same sequence of characters astoCopy. The returned string's backing array is no larger than necessary. -
String
Creates aStringfrom the contents of the specifiedStringBuffer. -
String
public String(int[] codePoints, int offset, int count)Creates aStringfrom the sub-array of Unicode code points.- Throws:
NullPointerException- ifcodePoints == null.IllegalArgumentException- if any of the elements ofcodePointsare not valid Unicode code points.IndexOutOfBoundsException- ifoffsetorcountare not within the bounds ofcodePoints.- Since:
- 1.5
-
String
Creates aStringfrom the contents of the specifiedStringBuilder.- Throws:
NullPointerException- ifstringBuilder == null.- Since:
- 1.5
-
-
Method Details
-
charAt
public char charAt(int index)Returns the character at the specified offset in this string.- Specified by:
charAtin interfaceCharSequence- Parameters:
index- the zero-based index in this string.- Returns:
- the character at the index.
- Throws:
IndexOutOfBoundsException- ifindex < 0orindex >= length().
-
compareTo
Compares the specified string to this string using the Unicode values of the characters. Returns 0 if the strings contain the same characters in the same order. Returns a negative integer if the first non-equal character in this string has a Unicode value which is less than the Unicode value of the character at the same position in the specified string, or if this string is a prefix of the specified string. Returns a positive integer if the first non-equal character in this string has a Unicode value which is greater than the Unicode value of the character at the same position in the specified string, or if the specified string is a prefix of this string.- Specified by:
compareToin interfaceComparable<String>- Parameters:
string- the string to compare.- Returns:
- 0 if the strings are equal, a negative integer if this string is before the specified string, or a positive integer if this string is after the specified string.
- Throws:
NullPointerException- ifstringisnull.
-
compareToIgnoreCase
Compares the specified string to this string using the Unicode values of the characters, ignoring case differences. Returns 0 if the strings contain the same characters in the same order. Returns a negative integer if the first non-equal character in this string has a Unicode value which is less than the Unicode value of the character at the same position in the specified string, or if this string is a prefix of the specified string. Returns a positive integer if the first non-equal character in this string has a Unicode value which is greater than the Unicode value of the character at the same position in the specified string, or if the specified string is a prefix of this string.- Parameters:
string- the string to compare.- Returns:
- 0 if the strings are equal, a negative integer if this string is before the specified string, or a positive integer if this string is after the specified string.
- Throws:
NullPointerException- ifstringisnull.
-
concat
Concatenates this string and the specified string.- Parameters:
string- the string to concatenate- Returns:
- a new string which is the concatenation of this string and the specified string.
-
copyValueOf
Creates a new string containing the characters in the specified character array. Modifying the character array after creating the string has no effect on the string.- Parameters:
data- the array of characters.- Returns:
- the new string.
- Throws:
NullPointerException- ifdataisnull.
-
copyValueOf
Creates a new string containing the specified characters in the character array. Modifying the character array after creating the string has no effect on the string.- Parameters:
data- the array of characters.start- the starting offset in the character array.length- the number of characters to use.- Returns:
- the new string.
- Throws:
NullPointerException- ifdataisnull.IndexOutOfBoundsException- iflength < 0, start < 0orstart + length > data.length.
-
endsWith
Compares the specified string to this string to determine if the specified string is a suffix.- Parameters:
suffix- the suffix to look for.- Returns:
trueif the specified string is a suffix of this string,falseotherwise.- Throws:
NullPointerException- ifsuffixisnull.
-
equals
Compares the specified object to this string and returns true if they are equal. The object must be an instance of string with the same characters in the same order. -
equalsIgnoreCase
Compares the specified string to this string ignoring the case of the characters and returns true if they are equal.- Parameters:
string- the string to compare.- Returns:
trueif the specified string is equal to this string,falseotherwise.
-
getBytes
Deprecated.UsegetBytes()orgetBytes(String)instead.Mangles this string into a byte array by stripping the high order bits from each character. UsegetBytes()orgetBytes(String)instead.- Parameters:
start- the starting offset of characters to copy.end- the ending offset of characters to copy.data- the destination byte array.index- the starting offset in the destination byte array.- Throws:
NullPointerException- ifdataisnull.IndexOutOfBoundsException- ifstart < 0,end > length(),index < 0orend - start > data.length - index.
-
getBytes
public byte[] getBytes()Returns a new byte array containing the characters of this string encoded using the system'sdefault 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.
-
getBytes
Returns a new byte array containing the characters of this string encoded using the named charset.The behavior when this string cannot be represented in the named charset is unspecified. Use
CharsetEncoderfor more control.- Throws:
UnsupportedEncodingException- if the charset is not supported
-
getBytes
Returns a new byte array containing the characters 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 characters with the charset's default replacement byte array. Use
CharsetEncoderfor more control.- Since:
- 1.6
-
getChars
public void getChars(int start, int end, char[] buffer, int index)Copies the specified characters in this string to the character array starting at the specified offset in the character array.- Parameters:
start- the starting offset of characters to copy.end- the ending offset of characters to copy.buffer- the destination character array.index- the starting offset in the character array.- Throws:
NullPointerException- ifbufferisnull.IndexOutOfBoundsException- ifstart < 0,end > length(),start > end,index < 0,end - start > buffer.length - index
-
hashCode
public int hashCode()Description copied from class:ObjectReturns an integer hash code for this object. By contract, any two objects for whichObject.equals(java.lang.Object)returnstruemust return the same hash code value. This means that subclasses ofObjectusually 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
hashCodemethod if you intend implementing your ownhashCodemethod.- Overrides:
hashCodein classObject- Returns:
- this object's hash code.
- See Also:
Object.equals(java.lang.Object)
-
indexOf
public int indexOf(int c)Searches in this string for the first index of the specified character. The search for the character starts at the beginning and moves towards the end of this string.- Parameters:
c- the character to find.- Returns:
- the index in this string of the specified character, -1 if the character isn't found.
-
indexOf
public int indexOf(int c, int start)Searches in this string for the index of the specified character. The search for the character starts at the specified offset and moves towards the end of this string.- Parameters:
c- the character to find.start- the starting offset.- Returns:
- the index in this string of the specified character, -1 if the character isn't found.
-
indexOf
Searches in this string for the first index of the specified string. The search for the string starts at the beginning and moves towards the end of this string.- Parameters:
string- the string to find.- Returns:
- the index of the first character of the specified string in this string, -1 if the specified string is not a substring.
- Throws:
NullPointerException- ifstringisnull.
-
indexOf
Searches in this string for the index of the specified string. The search for the string starts at the specified offset and moves towards the end of this string.- Parameters:
subString- the string to find.start- the starting offset.- Returns:
- the index of the first character of the specified string in this string, -1 if the specified string is not a substring.
- Throws:
NullPointerException- ifsubStringisnull.
-
intern
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 callinginternwon't lead to unwanted retention.Interning is typically used because it guarantees that for interned strings
aandb,a.equals(b)can be simplified toa == b. (This is not true of non-interned strings.)Many applications find it simpler and more convenient to use an explicit
HashMapto implement their own pools. -
isEmpty
public boolean isEmpty()Returns true if the length of this string is 0.- Since:
- 1.6
-
lastIndexOf
public int lastIndexOf(int c)Returns the last index of the code pointc, or -1. The search for the character starts at the end and moves towards the beginning of this string. -
lastIndexOf
public int lastIndexOf(int c, int start)Returns the last index of the code pointc, or -1. The search for the character starts at offsetstartand moves towards the beginning of this string. -
lastIndexOf
Searches in this string for the last index of the specified string. The search for the string starts at the end and moves towards the beginning of this string.- Parameters:
string- the string to find.- Returns:
- the index of the first character of the specified string in this string, -1 if the specified string is not a substring.
- Throws:
NullPointerException- ifstringisnull.
-
lastIndexOf
Searches in this string for the index of the specified string. The search for the string starts at the specified offset and moves towards the beginning of this string.- Parameters:
subString- the string to find.start- the starting offset.- Returns:
- the index of the first character of the specified string in this string , -1 if the specified string is not a substring.
- Throws:
NullPointerException- ifsubStringisnull.
-
length
public int length()Returns the number of characters in this string.- Specified by:
lengthin interfaceCharSequence- Returns:
- the number of characters.
-
regionMatches
Compares the specified string to this string and compares the specified range of characters to determine if they are the same.- Parameters:
thisStart- the starting offset in this string.string- the string to compare.start- the starting offset in the specified string.length- the number of characters to compare.- Returns:
trueif the ranges of characters are equal,falseotherwise- Throws:
NullPointerException- ifstringisnull.
-
regionMatches
public boolean regionMatches(boolean ignoreCase, int thisStart, String string, int start, int length)Compares the specified string to this string and compares the specified range of characters to determine if they are the same. When ignoreCase is true, the case of the characters is ignored during the comparison.- Parameters:
ignoreCase- specifies if case should be ignored.thisStart- the starting offset in this string.string- the string to compare.start- the starting offset in the specified string.length- the number of characters to compare.- Returns:
trueif the ranges of characters are equal,falseotherwise.- Throws:
NullPointerException- ifstringisnull.
-
replace
Copies this string replacing occurrences of the specified character with another character.- Parameters:
oldChar- the character to replace.newChar- the replacement character.- Returns:
- a new string with occurrences of oldChar replaced by newChar.
-
replace
Copies this string replacing occurrences of the specified target sequence with another sequence. The string is processed from the beginning to the end.- Parameters:
target- the sequence to replace.replacement- the replacement sequence.- Returns:
- the resulting string.
- Throws:
NullPointerException- iftargetorreplacementisnull.
-
startsWith
Compares the specified string to this string to determine if the specified string is a prefix.- Parameters:
prefix- the string to look for.- Returns:
trueif the specified string is a prefix of this string,falseotherwise- Throws:
NullPointerException- ifprefixisnull.
-
startsWith
Compares the specified string to this string, starting at the specified offset, to determine if the specified string is a prefix.- Parameters:
prefix- the string to look for.start- the starting offset.- Returns:
trueif the specified string occurs in this string at the specified offset,falseotherwise.- Throws:
NullPointerException- ifprefixisnull.
-
substring
Returns a string containing a suffix of this string. The returned string shares this string's backing array.- Parameters:
start- the offset of the first character.- Returns:
- a new string containing the characters from start to the end of the string.
- Throws:
IndexOutOfBoundsException- ifstart < 0orstart > length().
-
substring
Returns a string containing a subsequence of characters from this string. The returned string shares this string's backing array.- Parameters:
start- the offset of the first character.end- the offset one past the last character.- Returns:
- a new string containing the characters from start to end - 1
- Throws:
IndexOutOfBoundsException- ifstart < 0,start > endorend > length().
-
toCharArray
public char[] toCharArray()Returns a newchararray containing a copy of the characters in this string. This is expensive and rarely useful. If you just want to iterate over the characters in the string, usecharAt(int)instead. -
toLowerCase
Converts this string to lower case, using the rules of the user's default locale. See "Be wary of the default locale".- Returns:
- a new lower case string, or
thisif it's already all lower case.
-
toLowerCase
Converts this string to lower case, using the rules oflocale.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.
- Returns:
- a new lower case string, or
thisif it's already all lower case.
-
toString
Returns this string.- Specified by:
toStringin interfaceCharSequence- Overrides:
toStringin classObject- Returns:
- a printable representation of this object.
-
toUpperCase
Converts this this string to upper case, using the rules of the user's default locale. See "Be wary of the default locale".- Returns:
- a new upper case string, or
thisif it's already all upper case.
-
toUpperCase
Converts this this string to upper case, using the rules oflocale.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.
- Returns:
- a new upper case string, or
thisif it's already all upper case.
-
trim
Copies this string removing white space characters from the beginning and end of the string.- Returns:
- a new string with characters
<= \\u0020removed from the beginning and the end.
-
valueOf
Creates a new string containing the characters in the specified character array. Modifying the character array after creating the string has no effect on the string.- Parameters:
data- the array of characters.- Returns:
- the new string.
- Throws:
NullPointerException- ifdataisnull.
-
valueOf
Creates a new string containing the specified characters in the character array. Modifying the character array after creating the string has no effect on the string.- Parameters:
data- the array of characters.start- the starting offset in the character array.length- the number of characters to use.- Returns:
- the new string.
- Throws:
IndexOutOfBoundsException- iflength < 0,start < 0orstart + length > data.lengthNullPointerException- ifdataisnull.
-
valueOf
Converts the specified character to its string representation.- Parameters:
value- the character.- Returns:
- the character converted to a string.
-
valueOf
Converts the specified double to its string representation.- Parameters:
value- the double.- Returns:
- the double converted to a string.
-
valueOf
Converts the specified float to its string representation.- Parameters:
value- the float.- Returns:
- the float converted to a string.
-
valueOf
Converts the specified integer to its string representation.- Parameters:
value- the integer.- Returns:
- the integer converted to a string.
-
valueOf
Converts the specified long to its string representation.- Parameters:
value- the long.- Returns:
- the long converted to a string.
-
valueOf
Converts the specified object to its string representation. If the object is null return the string"null", otherwise usetoString()to get the string representation.- Parameters:
value- the object.- Returns:
- the object converted to a string, or the string
"null".
-
valueOf
Converts the specified boolean to its string representation. When the boolean istruereturn"true", otherwise return"false".- Parameters:
value- the boolean.- Returns:
- the boolean converted to a string.
-
contentEquals
Returns whether the characters in the StringBufferstrbufare the same as those in this string.- Parameters:
strbuf- the StringBuffer to compare this string to.- Returns:
trueif the characters instrbufare identical to those in this string. If they are not,falsewill be returned.- Throws:
NullPointerException- ifstrbufisnull.- Since:
- 1.4
-
contentEquals
Compares aCharSequenceto thisStringto determine if their contents are equal.- Parameters:
cs- the character sequence to compare to.- Returns:
trueif equal, otherwisefalse- Since:
- 1.5
-
matches
Tests whether this string matches the givenregularExpression. This method returns true only if the regular expression matches the entire input string. A common mistake is to assume that this method behaves likecontains(java.lang.CharSequence); if you want to match anywhere within the input string, you need to add.*to the beginning and end of your regular expression. SeePattern.matches(java.lang.String, java.lang.CharSequence).If the same regular expression is to be used for multiple operations, it may be more efficient to reuse a compiled
Pattern.- Throws:
PatternSyntaxException- if the syntax of the supplied regular expression is not valid.NullPointerException- ifregularExpression == null- Since:
- 1.4
-
replaceAll
Replaces all matches forregularExpressionwithin this string with the givenreplacement. SeePatternfor 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.- Throws:
PatternSyntaxException- if the syntax of the supplied regular expression is not valid.NullPointerException- ifregularExpression == null- Since:
- 1.4
- See Also:
Pattern
-
replaceFirst
Replaces the first match forregularExpressionwithin this string with the givenreplacement. SeePatternfor 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.- Throws:
PatternSyntaxException- if the syntax of the supplied regular expression is not valid.NullPointerException- ifregularExpression == null- Since:
- 1.4
- See Also:
Pattern
-
split
Splits this string using the suppliedregularExpression. Equivalent tosplit(regularExpression, 0). SeePattern.split(CharSequence, int)for an explanation oflimit. SeePatternfor 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.- Throws:
NullPointerException- ifregularExpression == nullPatternSyntaxException- if the syntax of the supplied regular expression is not valid.- Since:
- 1.4
- See Also:
Pattern
-
split
Splits this string using the suppliedregularExpression. SeePattern.split(CharSequence, int)for an explanation oflimit. SeePatternfor 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.- Throws:
NullPointerException- ifregularExpression == nullPatternSyntaxException- if the syntax of the supplied regular expression is not valid.- Since:
- 1.4
-
subSequence
Has the same result as the substring function, but is present so that string may implement the CharSequence interface.- Specified by:
subSequencein interfaceCharSequence- Parameters:
start- the offset the first character.end- the offset of one past the last character to include.- Returns:
- the subsequence requested.
- Throws:
IndexOutOfBoundsException- ifstart < 0,end < 0,start > endorend > length().- Since:
- 1.4
- See Also:
CharSequence.subSequence(int, int)
-
codePointAt
public int codePointAt(int index)Returns the Unicode code point at the givenindex.- Throws:
IndexOutOfBoundsException- ifindex < 0 || index >= length()- Since:
- 1.5
- See Also:
Character.codePointAt(char[], int, int)
-
codePointBefore
public int codePointBefore(int index)Returns the Unicode code point that precedes the givenindex.- Throws:
IndexOutOfBoundsException- ifindex < 1 || index > length()- Since:
- 1.5
- See Also:
Character.codePointBefore(char[], int, int)
-
codePointCount
public int codePointCount(int start, int end)Calculates the number of Unicode code points betweenstartandend.- Parameters:
start- the inclusive beginning index of the subsequence.end- the exclusive end index of the subsequence.- Returns:
- the number of Unicode code points in the subsequence.
- Throws:
IndexOutOfBoundsException- ifstart < 0 || end > length() || start > end- Since:
- 1.5
- See Also:
Character.codePointCount(CharSequence, int, int)
-
contains
Determines if thisStringcontains the sequence of characters in theCharSequencepassed.- Parameters:
cs- the character sequence to search for.- Returns:
trueif the sequence of characters are contained in this string, otherwisefalse.- Since:
- 1.5
-
offsetByCodePoints
public int offsetByCodePoints(int index, int codePointOffset)Returns the index within this object that is offset fromindexbycodePointOffsetcode points.- Parameters:
index- the index within this object to calculate the offset from.codePointOffset- the number of code points to count.- Returns:
- the index within this object that is the offset.
- Throws:
IndexOutOfBoundsException- ifindexis negative or greater thanlength()or if there aren't enough code points before or afterindexto matchcodePointOffset.- Since:
- 1.5
-
format
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 supplyLocale.US. See "Be wary of the default locale".- Parameters:
format- the format string (seeFormatter.format(java.lang.String, java.lang.Object...))args- the list of arguments passed to the formatter. If there are more arguments than required byformat, additional arguments are ignored.- Returns:
- the formatted string.
- Throws:
NullPointerException- ifformat == nullIllegalFormatException- if the format is invalid.- Since:
- 1.5
-
format
Returns a formatted string, using the supplied format and arguments, localized to the given locale.- Parameters:
locale- the locale to apply;nullvalue means no localization.format- the format string (seeFormatter.format(java.lang.String, java.lang.Object...))args- the list of arguments passed to the formatter. If there are more arguments than required byformat, additional arguments are ignored.- Returns:
- the formatted string.
- Throws:
NullPointerException- ifformat == nullIllegalFormatException- if the format is invalid.- Since:
- 1.5
-
String(byte[])orString(byte[], String)instead.