Class String
- java.lang.Object
-
- java.lang.String
-
- All Implemented Interfaces:
Serializable,CharSequence,Comparable<String>
public final class String extends Object implements Serializable, Comparable<String>, CharSequence
An immutable sequence of UTF-16chars. SeeCharacterfor details about the relationship betweencharand Unicode code points.- Since:
- 1.0
- See Also:
StringBuffer,StringBuilder,Charset, Serialized Form
-
-
Field Summary
Fields Modifier and Type Field Description static Comparator<String>CASE_INSENSITIVE_ORDERCompares strings usingcompareToIgnoreCase(java.lang.String).
-
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.UseString(byte[])orString(byte[], String)instead.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 givenchars.String(char[] data, int offset, int charCount)Initializes this string to contain the givenchars.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
All Methods Static Methods Instance Methods Concrete Methods Deprecated Methods Modifier and Type Method Description charcharAt(int index)Returns thecharatindex.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 this string to the given string.intcompareToIgnoreCase(String string)Compares this string to the given string, ignoring case differences.Stringconcat(String string)Concatenates this string and the specified string.booleancontains(CharSequence cs)Returns true if this string contains thecharss from the givenCharSequence.booleancontentEquals(CharSequence cs)Returns true if thechars in the givenCharSequenceare the same as those in this string.booleancontentEquals(StringBuffer sb)Returns true if thechars in the givenStringBufferare the same as those in this string.static StringcopyValueOf(char[] data)Creates a new string by copying the givenchar[].static StringcopyValueOf(char[] data, int start, int length)Creates a new string by copying the given subsequence of the givenchar[].booleanendsWith(String suffix)Compares the specified string to this string to determine if the specified string is a suffix.booleanequals(Object other)Compares the given object to this string and returns true if they are equal.booleanequalsIgnoreCase(String string)Compares the given string to this string ignoring case.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 code points in this string encoded using the system'sdefault charset.voidgetBytes(int start, int end, byte[] data, int index)Deprecated.UsegetBytes()orgetBytes(String)byte[]getBytes(String charsetName)Returns a new byte array containing the code points of this string encoded using the named charset.byte[]getBytes(Charset charset)Returns a new byte array containing the code points of this string encoded using the given charset.voidgetChars(int start, int end, char[] buffer, int index)Copies the given subsequence of this string to the given array starting at the given offset.inthashCode()Returns an integer hash code for this object.intindexOf(int c)Returns the first index of the given code point, or -1.intindexOf(int c, int start)Returns the next index of the given code point, or -1.intindexOf(String string)Returns the first index of the given string, or -1.intindexOf(String subString, int start)Returns the next index of the given string in this string, or -1.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)Returns the index of the start of the last match for the given string in this string, or -1.intlastIndexOf(String subString, int start)Returns the index of the start of the previous match for the given string in this string, or -1.intlength()Returns the number ofchars 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)Returns true if the given subsequence of the given string matches this string starting at the given offset.booleanregionMatches(int thisStart, String string, int start, int length)Returns true if the given subsequence of the given string matches this string starting at the given offset.Stringreplace(char oldChar, char newChar)Returns a copy of this string after replacing occurrences of the givencharwith another.Stringreplace(CharSequence target, CharSequence replacement)Returns a copy of this string after replacing occurrences oftargetreplaced withreplacement.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)Equivalent tosubstring(int, int)but needed to implementCharSequence.Stringsubstring(int start)Returns a string containing a suffix of this string starting atstart.Stringsubstring(int start, int end)Returns a string containing the given subsequence of this string.char[]toCharArray()Returns a newchararray containing a copy of thechars 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()Returns a string with no code points<= \\u0020at the beginning or end.static StringvalueOf(boolean value)Converts the specified boolean to its string representation.static StringvalueOf(char value)Returns a new string of just the givenchar.static StringvalueOf(char[] data)Returns a new string containing the samechars as the given array.static StringvalueOf(char[] data, int start, int length)Returns a new string containing the samechars as the given subset of the given array.static StringvalueOf(double value)Returns the string representation of the given double.static StringvalueOf(float value)Returns the string representation of the given float.static StringvalueOf(int value)Returns the string representation of the given int.static StringvalueOf(long value)Returns the string representation of the given long.static StringvalueOf(Object value)Converts the specified object to its string representation.
-
-
-
Field Detail
-
CASE_INSENSITIVE_ORDER
public static final Comparator<String> CASE_INSENSITIVE_ORDER
Compares strings usingcompareToIgnoreCase(java.lang.String). This is not suitable for case-insensitive string comparison for all locales. Use aCollatorinstead.
-
-
Constructor Detail
-
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 public String(byte[] data, int high)
Deprecated.UseString(byte[])orString(byte[], String)instead.Converts the byte array to a string, setting the high byte of everycharto 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 public String(byte[] data, int high, int offset, int byteCount)
Deprecated.UseString(byte[], int, int)instead.Converts the byte array to a string, setting the high byte of everychartohigh.- 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
public String(byte[] data, 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.UnsupportedEncodingException- ifcharsetNameis not supported.
-
String
public String(byte[] data, int offset, int byteCount, Charset charset)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
CharsetDecoderfor more control.- Throws:
IndexOutOfBoundsException- ifbyteCount < 0 || offset < 0 || offset + byteCount > data.lengthNullPointerException- ifdata == null- Since:
- 1.6
-
String
public String(byte[] data, Charset charset)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 givenchars. Modifying the 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 givenchars. Modifying the 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
public String(String toCopy)
Constructs a new string with the same sequence of characters astoCopy.
-
String
public String(StringBuffer stringBuffer)
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
public String(StringBuilder stringBuilder)
Creates aStringfrom the contents of the specifiedStringBuilder.- Throws:
NullPointerException- ifstringBuilder == null.- Since:
- 1.5
-
-
Method Detail
-
charAt
public char charAt(int index)
Returns thecharatindex.- Specified by:
charAtin interfaceCharSequence- Throws:
IndexOutOfBoundsException- ifindex < 0orindex >= length().
-
compareTo
public int compareTo(String string)
Compares this string to the given string.The strings are compared one
charat a time. In the discussion of the return value below, note thatchardoes 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
chars 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 everycharis the same, the result is 0.- Specified by:
compareToin interfaceComparable<String>- Parameters:
string- the object to compare to this instance.- Returns:
- a negative integer if this instance is less than
another; a positive integer if this instance is greater thananother; 0 if this instance has the same order asanother. - Throws:
NullPointerException- ifstringisnull.
-
compareToIgnoreCase
public int compareToIgnoreCase(String string)
Compares this string to the given string, ignoring case differences.The strings are compared one
charat a time. This is not suitable for case-insensitive string comparison for all locales. Use aCollatorinstead.If there is an index at which the two strings differ, the result is the difference between the two
chars 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 everycharis the same, the result is 0.- Throws:
NullPointerException- ifstringisnull.
-
concat
public String concat(String string)
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
public static String copyValueOf(char[] data)
Creates a new string by copying the givenchar[]. Modifying the array after creating the string has no effect on the string.- Throws:
NullPointerException- ifdataisnull.
-
copyValueOf
public static String copyValueOf(char[] data, int start, int length)
Creates a new string by copying the given subsequence of the givenchar[]. Modifying the array after creating the string has no effect on the string.- Throws:
NullPointerException- ifdataisnull.IndexOutOfBoundsException- iflength < 0, start < 0orstart + length > data.length.
-
endsWith
public boolean endsWith(String suffix)
Compares the specified string to this string to determine if the specified string is a suffix.- Throws:
NullPointerException- ifsuffixisnull.
-
equals
public boolean equals(Object other)
Compares the given object to this string and returns true if they are equal. The object must be an instance ofStringwith the same length, where for every index,charAton each string returns the same value.- Overrides:
equalsin classObject- Parameters:
other- the object to compare this instance with.- Returns:
trueif the specified object is equal to thisObject;falseotherwise.- See Also:
Object.hashCode()
-
equalsIgnoreCase
public boolean equalsIgnoreCase(String string)
Compares the given string to this string ignoring case.The strings are compared one
charat a time. This is not suitable for case-insensitive string comparison for all locales. Use aCollatorinstead.
-
getBytes
@Deprecated public void getBytes(int start, int end, byte[] data, int index)
Deprecated.UsegetBytes()orgetBytes(String)Mangles a subsequence of this string into a byte array by stripping the high order bits from eachchar. UsegetBytes()orgetBytes(String)instead.- Parameters:
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.- 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 code points in 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
public byte[] getBytes(String charsetName) throws UnsupportedEncodingException
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
CharsetEncoderfor more control.- Throws:
UnsupportedEncodingException- if the charset is not supported
-
getBytes
public byte[] getBytes(Charset charset)
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
CharsetEncoderfor more control.- Since:
- 1.6
-
getChars
public void getChars(int start, int end, char[] buffer, int index)Copies the given subsequence of this string to the given array starting at the given offset.- Parameters:
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.- 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)
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.
-
indexOf
public int indexOf(int c, int start)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.
-
indexOf
public int indexOf(String string)
Returns the first index of the given string, or -1. The search starts at the beginning and moves towards the end of this string.- Throws:
NullPointerException- ifstringisnull.
-
indexOf
public int indexOf(String subString, int start)
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.- Throws:
NullPointerException- ifsubStringisnull.
-
intern
public String 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 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 starts at offsetstartand moves towards the beginning of this string.
-
lastIndexOf
public int lastIndexOf(String string)
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.- Throws:
NullPointerException- ifstringisnull.
-
lastIndexOf
public int lastIndexOf(String subString, int start)
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.- Throws:
NullPointerException- ifsubStringisnull.
-
length
public int length()
Returns the number ofchars in this string. If this string contains surrogate pairs, this is not the same as the number of code points.- Specified by:
lengthin interfaceCharSequence- Returns:
- the number of characters.
-
regionMatches
public boolean regionMatches(int thisStart, String string, int start, int length)Returns true if the given subsequence of the given string matches this string starting at the given offset.- Parameters:
thisStart- the start offset in this string.string- the other string.start- the start offset instring.length- the number ofchars to compare.- Throws:
NullPointerException- ifstringisnull.
-
regionMatches
public boolean regionMatches(boolean ignoreCase, int thisStart, String string, int start, int length)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
charat a time. This is not suitable for case-insensitive string comparison for all locales. Use aCollatorinstead.- Parameters:
ignoreCase- specifies if case should be ignored (useCollatorinstead for non-ASCII case insensitivity).thisStart- the start offset in this string.string- the other string.start- the start offset instring.length- the number ofchars to compare.- Throws:
NullPointerException- ifstringisnull.
-
replace
public String replace(char oldChar, char newChar)
Returns a copy of this string after replacing occurrences of the givencharwith another.
-
replace
public String replace(CharSequence target, CharSequence replacement)
Returns a copy of this string after replacing occurrences oftargetreplaced withreplacement. The string is processed from the beginning to the end.- Throws:
NullPointerException- iftargetorreplacementisnull.
-
startsWith
public boolean startsWith(String prefix)
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
public boolean startsWith(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.- 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
public String substring(int start)
Returns a string containing a suffix of this string starting atstart. The returned string shares this string's backing array.- Throws:
IndexOutOfBoundsException- ifstart < 0orstart > length().
-
substring
public String substring(int start, int end)
Returns a string containing the given subsequence of this string. The returned string shares this string's backing array.- Parameters:
start- the start offset.end- the end+1 offset.- Throws:
IndexOutOfBoundsException- ifstart < 0,start > endorend > length().
-
toCharArray
public char[] toCharArray()
Returns a newchararray containing a copy of thechars in this string. This is expensive and rarely useful. If you just want to iterate over thechars in the string, usecharAt(int)instead.
-
toLowerCase
public String 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
public String toLowerCase(Locale locale)
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
public String toString()
Returns this string.- Specified by:
toStringin interfaceCharSequence- Overrides:
toStringin classObject- Returns:
- a printable representation of this object.
-
toUpperCase
public String 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
public String toUpperCase(Locale locale)
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
public String trim()
Returns a string with no code points<= \\u0020at the beginning or end.
-
valueOf
public static String valueOf(char[] data)
Returns a new string containing the samechars as the given array. Modifying the array after creating the string has no effect on the string.- Throws:
NullPointerException- ifdataisnull.
-
valueOf
public static String valueOf(char[] data, int start, int length)
Returns a new string containing the samechars as the given subset of the given array. Modifying the array after creating the string has no effect on the string.- Throws:
IndexOutOfBoundsException- iflength < 0,start < 0orstart + length > data.lengthNullPointerException- ifdataisnull.
-
valueOf
public static String valueOf(char value)
Returns a new string of just the givenchar.
-
valueOf
public static String valueOf(double value)
Returns the string representation of the given double.
-
valueOf
public static String valueOf(float value)
Returns the string representation of the given float.
-
valueOf
public static String valueOf(int value)
Returns the string representation of the given int.
-
valueOf
public static String valueOf(long value)
Returns the string representation of the given long.
-
valueOf
public static String valueOf(Object value)
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
public static String valueOf(boolean value)
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
public boolean contentEquals(StringBuffer sb)
Returns true if thechars in the givenStringBufferare the same as those in this string.- Throws:
NullPointerException- ifsbisnull.- Since:
- 1.4
-
contentEquals
public boolean contentEquals(CharSequence cs)
Returns true if thechars in the givenCharSequenceare the same as those in this string.- Since:
- 1.5
-
matches
public boolean matches(String regularExpression)
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
public String replaceAll(String regularExpression, String replacement)
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
public String replaceFirst(String regularExpression, String replacement)
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
public String[] split(String regularExpression)
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
public String[] split(String regularExpression, int limit)
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
public CharSequence subSequence(int start, int end)
Equivalent tosubstring(int, int)but needed to implementCharSequence.- Specified by:
subSequencein interfaceCharSequence- Parameters:
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- Returns:
- the requested sub-sequence.
- 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
public boolean contains(CharSequence cs)
Returns true if this string contains thecharss from the givenCharSequence.- 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
public static String format(String format, Object... args)
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
public static String format(Locale locale, String format, Object... args)
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
-
-