public final class String extends Object implements Serializable, Comparable<String>, CharSequence
StringBuffer
,
Serialized FormModifier and Type | Field and Description |
---|---|
static Comparator<String> |
CASE_INSENSITIVE_ORDER
A Comparator which compares Strings ignoring the case of the characters.
|
Constructor and Description |
---|
String()
Answers an empty string.
|
String(byte[] data)
Converts the byte array to a String using the default encoding as specified by the file.encoding system property.
|
String(byte[] data,
Charset charset)
Converts the byte array to a String using the specified Charset.
|
String(byte[] data,
int high)
Deprecated.
Use String(byte[]) or String(byte[], String) instead
|
String(byte[] data,
int start,
int length)
Converts the byte array to a String using the default encoding as specified by the file.encoding system property.
|
String(byte[] data,
int start,
int length,
Charset charset)
Converts the byte array to a String using the specified Charset.
|
String(byte[] data,
int high,
int start,
int length)
Deprecated.
Use String(byte[], int, int) instead
|
String(byte[] data,
int start,
int length,
String encoding)
Converts the byte array to a String using the specified encoding.
|
String(byte[] data,
String encoding)
Converts the byte array to a String using the specified encoding.
|
String(char[] data)
Initializes this String to contain the characters in the specified character array.
|
String(char[] data,
int start,
int length)
Initializes this String to contain the specified characters in the character array.
|
String(int[] data,
int start,
int length) |
String(String string)
Creates a string that is a copy of another string
|
String(StringBuffer buffer)
Creates a string from the contents of a StringBuffer.
|
String(StringBuilder builder)
Creates a string from the contents of a StringBuilder.
|
Modifier and Type | Method and Description |
---|---|
char |
charAt(int index)
Answers the character at the specified offset in this String.
|
int |
codePointAt(int index)
Returns the Unicode character at the given point.
|
int |
codePointBefore(int index)
Returns the Unicode character before the given point.
|
int |
codePointCount(int start,
int end)
Returns the total Unicode values in the specified range.
|
int |
compareTo(String string)
Compares the specified String to this String using the Unicode values of the characters.
|
int |
compareToIgnoreCase(String string)
Compare the receiver to the specified String to determine the relative ordering when the case of the characters is ignored.
|
String |
concat(String string)
Concatenates this String and the specified string.
|
boolean |
contains(CharSequence sequence) |
boolean |
contentEquals(CharSequence sequence)
Compares the content of the character sequence to this String
|
boolean |
contentEquals(StringBuffer buffer)
Answers whether the characters in the StringBuffer buffer are the same as those in this String.
|
static String |
copyValueOf(char[] data)
Creates a new String containing the characters in the specified character array.
|
static String |
copyValueOf(char[] data,
int start,
int length)
Creates a new String containing the specified characters in the character array.
|
boolean |
endsWith(String suffix)
Compares the specified string to this String to determine if the specified string is a suffix.
|
boolean |
equals(Object object)
Compares the specified object to this String and answer if they are equal.
|
boolean |
equalsIgnoreCase(String string)
Compares the specified String to this String ignoring the case of the characters and answer if they are equal.
|
static String |
format(Locale locale,
String format,
Object... args)
Format the receiver using the specified local, format and args.
|
static String |
format(String format,
Object... args)
Format the receiver using the specified format and args.
|
byte[] |
getBytes()
Converts this String to a byte encoding using the default encoding as specified by the file.encoding system property.
|
byte[] |
getBytes(Charset charset)
Converts this String to a byte encoding using the specified Charset.
|
void |
getBytes(int start,
int end,
byte[] data,
int index)
Deprecated.
Use getBytes() or getBytes(String)
|
byte[] |
getBytes(String encoding)
Converts this String to a byte encoding using the specified encoding.
|
void |
getChars(int start,
int end,
char[] data,
int index)
Copies the specified characters in this String to the character array starting at the specified offset in the character array.
|
int |
hashCode()
Answers an integer hash code for the receiver.
|
int |
indexOf(int c)
Searches in this String for the first index of the specified character.
|
int |
indexOf(int c,
int start)
Searches in this String for the index of the specified character.
|
int |
indexOf(String string)
Searches in this String for the first index of the specified string.
|
int |
indexOf(String subString,
int start)
Searches in this String for the index of the specified string.
|
String |
intern()
Searches an internal table of strings for a string equal to this String.
|
boolean |
isEmpty()
Answers if this String has no characters, a length of zero.
|
static String |
join(CharSequence delimiter,
CharSequence... elements)
Creates a new String by putting each element together joined by the delimiter.
|
static String |
join(CharSequence delimiter,
Iterable<? extends CharSequence> elements)
Creates a new String by putting each element together joined by the delimiter.
|
int |
lastIndexOf(int c)
Searches in this String for the last index of the specified character.
|
int |
lastIndexOf(int c,
int start)
Searches in this String for the index of the specified character.
|
int |
lastIndexOf(String string)
Searches in this String for the last index of the specified string.
|
int |
lastIndexOf(String subString,
int start)
Searches in this String for the index of the specified string.
|
int |
length()
Answers the size of this String.
|
boolean |
matches(String expr)
Determines whether a this String matches a given regular expression.
|
int |
offsetByCodePoints(int start,
int codePointCount)
Returns the index of the code point that was offset by codePointCount.
|
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.
|
boolean |
regionMatches(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.
|
String |
replace(char oldChar,
char newChar)
Replaces occurrences of the specified character with another character.
|
String |
replace(CharSequence sequence1,
CharSequence sequence2) |
String |
replaceAll(String regex,
String substitute)
Replace any substrings within this String that match the supplied regular expression expr, with the String substitute.
|
String |
replaceFirst(String expr,
String substitute)
Replace any substrings within this String that match the supplied regular expression expr, with the String substitute.
|
String[] |
split(String regex)
Splits this string around matches of the given regular expression.
|
String[] |
split(String regex,
int max)
Splits this String using the given regular expression.
|
boolean |
startsWith(String prefix)
Compares the specified string to this String to determine if the specified string is a prefix.
|
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.
|
CharSequence |
subSequence(int start,
int end)
Has the same result as the substring function, but is present so that String may implement the CharSequence interface.
|
String |
substring(int start)
Copies a range of characters into a new String.
|
String |
substring(int start,
int end)
Copies a range of characters.
|
char[] |
toCharArray()
Copies the characters in this String to a character array.
|
String |
toLowerCase()
Converts the characters in this String to lowercase, using the default Locale.
|
String |
toLowerCase(Locale locale)
Converts the characters in this String to lowercase, using the specified Locale.
|
String |
toString()
Answers a string containing a concise, human-readable description of the receiver.
|
String |
toUpperCase()
Converts the characters in this String to uppercase, using the default Locale.
|
String |
toUpperCase(Locale locale)
Converts the characters in this String to uppercase, using the specified Locale.
|
String |
trim()
Removes white space characters from the beginning and end of the string.
|
static String |
valueOf(boolean value)
Converts the specified boolean to its string representation.
|
static String |
valueOf(char value)
Converts the specified character to its string representation.
|
static String |
valueOf(char[] data)
Returns a String containing the characters in the specified character array.
|
static String |
valueOf(char[] data,
int start,
int length)
Returns a String containing the specified characters in the character array.
|
static String |
valueOf(double value)
Converts the specified double to its string representation.
|
static String |
valueOf(float value)
Converts the specified float to its string representation.
|
static String |
valueOf(int value)
Converts the specified integer to its string representation.
|
static String |
valueOf(long value)
Converts the specified long to its string representation.
|
static String |
valueOf(Object value)
Converts the specified object to its string representation.
|
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
chars, codePoints
public static final Comparator<String> CASE_INSENSITIVE_ORDER
public String()
public String(byte[] data)
data
- the byte array to convert to a StringNullPointerException
- when data is nullgetBytes()
,
getBytes(int, int, byte[], int)
,
getBytes(String)
,
valueOf(boolean)
,
valueOf(char)
,
valueOf(char[])
,
valueOf(char[], int, int)
,
valueOf(double)
,
valueOf(float)
,
valueOf(int)
,
valueOf(long)
,
valueOf(Object)
@Deprecated public String(byte[] data, int high)
data
- the byte array to convert to a Stringhigh
- the high byte to useNullPointerException
- when data is nullpublic String(byte[] data, int start, int length)
data
- the byte array to convert to a Stringstart
- the starting offset in the byte arraylength
- the number of bytes to convertIndexOutOfBoundsException
- when length < 0, start < 0
or start + length > data.length
NullPointerException
- when data is nullgetBytes()
,
getBytes(int, int, byte[], int)
,
getBytes(String)
,
valueOf(boolean)
,
valueOf(char)
,
valueOf(char[])
,
valueOf(char[], int, int)
,
valueOf(double)
,
valueOf(float)
,
valueOf(int)
,
valueOf(long)
,
valueOf(Object)
@Deprecated public String(byte[] data, int high, int start, int length)
data
- the byte array to convert to a Stringhigh
- the high byte to usestart
- the starting offset in the byte arraylength
- the number of bytes to convertIndexOutOfBoundsException
- when length < 0, start < 0
or start + length > data.length
NullPointerException
- when data is nullpublic String(byte[] data, int start, int length, String encoding) throws UnsupportedEncodingException
data
- the byte array to convert to a Stringstart
- the starting offset in the byte arraylength
- the number of bytes to convertencoding
- the encodingIndexOutOfBoundsException
- when length < 0, start < 0
or start + length > data.length
UnsupportedEncodingException
- when encoding is not supportedNullPointerException
- when data is nullgetBytes()
,
getBytes(int, int, byte[], int)
,
getBytes(String)
,
valueOf(boolean)
,
valueOf(char)
,
valueOf(char[])
,
valueOf(char[], int, int)
,
valueOf(double)
,
valueOf(float)
,
valueOf(int)
,
valueOf(long)
,
valueOf(Object)
,
UnsupportedEncodingException
public String(byte[] data, String encoding) throws UnsupportedEncodingException
data
- the byte array to convert to a Stringencoding
- the encodingUnsupportedEncodingException
- when encoding is not supportedNullPointerException
- when data is nullgetBytes()
,
getBytes(int, int, byte[], int)
,
getBytes(String)
,
valueOf(boolean)
,
valueOf(char)
,
valueOf(char[])
,
valueOf(char[], int, int)
,
valueOf(double)
,
valueOf(float)
,
valueOf(int)
,
valueOf(long)
,
valueOf(Object)
,
UnsupportedEncodingException
public String(char[] data)
data
- the array of charactersNullPointerException
- when data is nullpublic String(char[] data, int start, int length)
data
- the array of charactersstart
- the starting offset in the character arraylength
- the number of characters to useIndexOutOfBoundsException
- when length < 0, start < 0
or start + length > data.length
NullPointerException
- when data is nullpublic String(String string)
string
- the String to copypublic String(StringBuffer buffer)
buffer
- the StringBufferpublic String(int[] data, int start, int length)
data
- the byte array to convert to a Stringstart
- the starting offset in the byte arraylength
- the number of bytes to convertpublic String(StringBuilder builder)
builder
- the StringBuilderpublic String(byte[] data, Charset charset)
data
- the byte array to convert to a Stringcharset
- the Charset to useNullPointerException
- when data is nullString(byte[], int, int, Charset)
,
getBytes(Charset)
public String(byte[] data, int start, int length, Charset charset)
data
- the byte array to convert to a Stringstart
- the starting offset in the byte arraylength
- the number of bytes to convertcharset
- the Charset to useIndexOutOfBoundsException
- when length < 0, start < 0
or start + length > data.length
NullPointerException
- when data is nullString(byte[], Charset)
,
getBytes(Charset)
public char charAt(int index)
charAt
in interface CharSequence
index
- the zero-based index in this stringIndexOutOfBoundsException
- when index < 0
or index >= length()
public int compareTo(String string)
compareTo
in interface Comparable<String>
string
- the string to compareNullPointerException
- when string is nullpublic int compareToIgnoreCase(String string)
string
- a Stringint < 0
if this String is less than the specified String, 0 if they are equal, and > 0
if this String is greaterpublic String concat(String string)
string
- the string to concatenateNullPointerException
- if string is nullpublic static String copyValueOf(char[] data)
data
- the array of charactersNullPointerException
- if data is nullpublic static String copyValueOf(char[] data, int start, int length)
data
- the array of charactersstart
- the starting offset in the character arraylength
- the number of characters to useIndexOutOfBoundsException
- when length < 0, start < 0
or start + length > data.length
NullPointerException
- if data is nullpublic boolean endsWith(String suffix)
suffix
- the string to look forNullPointerException
- if suffix is nullpublic boolean equals(Object object)
equals
in class Object
object
- the object to comparehashCode()
public boolean equalsIgnoreCase(String string)
string
- the string to comparepublic byte[] getBytes()
String
@Deprecated public void getBytes(int start, int end, byte[] data, int index)
start
- the starting offset of characters to copyend
- the ending offset of characters to copydata
- the destination byte arrayindex
- the starting offset in the byte arrayNullPointerException
- when data is nullIndexOutOfBoundsException
- when start < 0, end > length(), index < 0, end - start > data.length - index
public byte[] getBytes(String encoding) throws UnsupportedEncodingException
encoding
- the encodingUnsupportedEncodingException
- when the encoding is not supportedString
,
UnsupportedEncodingException
public void getChars(int start, int end, char[] data, int index)
start
- the starting offset of characters to copyend
- the ending offset of characters to copydata
- the destination character arrayindex
- the starting offset in the character arrayIndexOutOfBoundsException
- when start < 0, end > length(), start > end, index < 0, end - start > buffer.length - index
NullPointerException
- when buffer is nullpublic int hashCode()
hashCode
in class Object
equals(java.lang.Object)
public int indexOf(int c)
c
- the character to findlastIndexOf(int)
,
lastIndexOf(int, int)
,
lastIndexOf(String)
,
lastIndexOf(String, int)
public int indexOf(int c, int start)
c
- the character to findstart
- the starting offsetlastIndexOf(int)
,
lastIndexOf(int, int)
,
lastIndexOf(String)
,
lastIndexOf(String, int)
public int indexOf(String string)
string
- the string to findNullPointerException
- when string is nulllastIndexOf(int)
,
lastIndexOf(int, int)
,
lastIndexOf(String)
,
lastIndexOf(String, int)
public int indexOf(String subString, int start)
subString
- the string to findstart
- the starting offsetNullPointerException
- when string is nulllastIndexOf(int)
,
lastIndexOf(int, int)
,
lastIndexOf(String)
,
lastIndexOf(String, int)
public String intern()
public int lastIndexOf(int c)
c
- the character to findlastIndexOf(int)
,
lastIndexOf(int, int)
,
lastIndexOf(String)
,
lastIndexOf(String, int)
public int lastIndexOf(int c, int start)
c
- the character to findstart
- the starting offsetlastIndexOf(int)
,
lastIndexOf(int, int)
,
lastIndexOf(String)
,
lastIndexOf(String, int)
public int lastIndexOf(String string)
string
- the string to findNullPointerException
- when string is nulllastIndexOf(int)
,
lastIndexOf(int, int)
,
lastIndexOf(String)
,
lastIndexOf(String, int)
public int lastIndexOf(String subString, int start)
subString
- the string to findstart
- the starting offsetNullPointerException
- when string is nulllastIndexOf(int)
,
lastIndexOf(int, int)
,
lastIndexOf(String)
,
lastIndexOf(String, int)
public int length()
length
in interface CharSequence
public boolean regionMatches(int thisStart, String string, int start, int length)
thisStart
- the starting offset in this Stringstring
- the string to comparestart
- the starting offset in stringlength
- the number of characters to compareNullPointerException
- when string is nullpublic boolean regionMatches(boolean ignoreCase, int thisStart, String string, int start, int length)
ignoreCase
- specifies if case should be ignoredthisStart
- the starting offset in this Stringstring
- the string to comparestart
- the starting offset in stringlength
- the number of characters to compareNullPointerException
- when string is nullpublic String replace(char oldChar, char newChar)
oldChar
- the character to replacenewChar
- the replacement characterpublic boolean startsWith(String prefix)
prefix
- the string to look forNullPointerException
- when prefix is nullpublic boolean startsWith(String prefix, int start)
prefix
- the string to look forstart
- the starting offsetNullPointerException
- when prefix is nullpublic String substring(int start)
start
- the offset of the first characterIndexOutOfBoundsException
- when start < 0
or start > length()
public String substring(int start, int end)
start
- the offset of the first characterend
- the offset one past the last characterIndexOutOfBoundsException
- when start < 0, start > end
or end > length()
public char[] toCharArray()
public String toLowerCase()
public String toLowerCase(Locale locale)
locale
- the Localepublic String toString()
toString
in interface CharSequence
toString
in class Object
public String toUpperCase()
public String toUpperCase(Locale locale)
locale
- the Localepublic String trim()
<= \\u0020
removed from the beginning and the endpublic static String valueOf(char[] data)
data
- the array of charactersNullPointerException
- when data is nullpublic static String valueOf(char[] data, int start, int length)
data
- the array of charactersstart
- the starting offset in the character arraylength
- the number of characters to useIndexOutOfBoundsException
- when length < 0, start < 0
or start + length > data.length
NullPointerException
- when data is nullpublic static String valueOf(char value)
value
- the characterpublic static String valueOf(double value)
value
- the doublepublic static String valueOf(float value)
value
- the floatpublic static String valueOf(int value)
value
- the integerpublic static String valueOf(long value)
value
- the longpublic static String valueOf(Object value)
"null"
, otherwise use
toString()
to get the string representation.value
- the objectpublic static String valueOf(boolean value)
"true"
, otherwise answer
"false"
.value
- the booleanpublic boolean contentEquals(StringBuffer buffer)
buffer
- the StringBuffer to compare this String toNullPointerException
- when buffer is nullpublic boolean matches(String expr)
expr
- the regular expression to be matchedPatternSyntaxException
- if the syntax of the supplied regular expression is not validNullPointerException
- if expr is nullpublic String replaceAll(String regex, String substitute)
regex
- the regular expression to matchsubstitute
- the string to replace the matching substring withNullPointerException
- if expr is nullpublic String replaceFirst(String expr, String substitute)
expr
- the regular expression to matchsubstitute
- the string to replace the matching substring withNullPointerException
- if expr is nullpublic String[] split(String regex)
regex
- Regular expression that is used as a delimiterPatternSyntaxException
- if the syntax of regex is invalidpublic String[] split(String regex, int max)
regex
- Regular expression that is used as a delimitermax
- The threshold of the returned arrayPatternSyntaxException
- if the syntax of regex is invalidpublic CharSequence subSequence(int start, int end)
subSequence
in interface CharSequence
start
- the offset the first characterend
- the offset of one past the last character to includeIndexOutOfBoundsException
- when start or end is less than zero, start is greater than end, or end is greater than the length of the String.CharSequence.subSequence(int, int)
public int codePointAt(int index)
index
- the character indexpublic int codePointBefore(int index)
index
- the character indexpublic int codePointCount(int start, int end)
start
- first indexend
- last indexpublic int offsetByCodePoints(int start, int codePointCount)
start
- the position to offsetcodePointCount
- the code point countpublic boolean contentEquals(CharSequence sequence)
sequence
- the character sequencetrue
if the content of this String is equal to the character sequence, false
otherwise.public boolean contains(CharSequence sequence)
sequence
- the sequence to compare totrue
if this String contains the sequence, false
otherwise.public String replace(CharSequence sequence1, CharSequence sequence2)
sequence1
- the old character sequencesequence2
- the new character sequencepublic static String format(String format, Object... args)
format
- the format to useargs
- the format arguments to useFormatter.format(String, Object...)
public static String format(Locale locale, String format, Object... args)
locale
- the locale used to create the Formatter, may be nullformat
- the format to useargs
- the format arguments to useFormatter.format(String, Object...)
public boolean isEmpty()
length()
public byte[] getBytes(Charset charset)
charset
- the Charset to usepublic static String join(CharSequence delimiter, CharSequence... elements)
delimiter
- Used as joiner to put elements togetherelements
- Elements to be joinedNullPointerException
- if one of the arguments is nullpublic static String join(CharSequence delimiter, Iterable<? extends CharSequence> elements)
delimiter
- Used as joiner to put elements togetherelements
- Elements to be joinedNullPointerException
- if one of the arguments is nullEclipse OpenJ9 website.
To raise a bug report or suggest an improvement create an Eclipse OpenJ9 issue.
Copyright © 1993, 2023 IBM Corp. and others.