String
class represents character strings. All
string literals in Java programs, such as "abc"
, are
implemented as instances of this class.
Strings are constant; their values cannot be changed after they are created. String buffers support mutable strings. Because String objects are immutable they can be shared. For example:
String str = "abc";
is equivalent to:
char data[] = {'a', 'b', 'c'}; String str = new String(data);
Here are some more examples of how strings can be used:
System.out.println("abc"); String cde = "cde"; System.out.println("abc" + cde); String c = "abc".substring(2, 3); String d = cde.substring(1, 2);
The class String
includes methods for examining
individual characters of the sequence, for comparing strings, for
searching strings, for extracting substrings, and for creating a
copy of a string with all characters translated to uppercase or to
lowercase. Case mapping is based on the Unicode Standard version
specified by the Character
class.
The Java language provides special support for the string concatenation operator ( + ), and for conversion of other objects to strings. For additional information on string concatenation and conversion, see The Java Language Specification.
Unless otherwise noted, passing a null
argument to a constructor
or method in this class will cause a NullPointerException
to be
thrown.
A String
represents a string in the UTF-16 format
in which supplementary characters are represented by surrogate
pairs (see the section Unicode
Character Representations in the Character
class for
more information).
Index values refer to char
code units, so a supplementary
character uses two positions in a String
.
The String
class provides methods for dealing with
Unicode code points (i.e., characters), in addition to those for
dealing with Unicode code units (i.e., char
values).
Unless otherwise noted, methods for comparing Strings do not take locale
into account. The java.
class provides methods for
finer-grain, locale-sensitive String comparison.
Implementation Note
The implementation of the string concatenation operator is left to
the discretion of a Java compiler, as long as the compiler ultimately conforms
to The Java Language Specification. For example, the javac
compiler
may implement the operator with StringBuffer
, StringBuilder
,
or java.lang.invoke.StringConcatFactory
depending on the JDK version. The
implementation of string conversion is typically through the method toString
,
defined by Object
and inherited by all classes in Java.
java.lang.Object#toString()
, java.lang.StringBuffer
, java.lang.StringBuilder
, java.nio.charset.Charset
Modifier and Type | Class and Description |
---|---|
private static class | String.
CaseInsensitiveComparator for Strings. |
Modifier and Type | Field and Description |
---|---|
public static final Comparator | CASE_INSENSITIVE_ORDER
A Comparator that orders |
private final byte | coder
The identifier of the encoding used to encode the bytes in
|
pack-priv static final boolean | COMPACT_STRINGS
If String compaction is disabled, the bytes in |
private int | hash
Cache the hash code for the string |
private boolean | hashIsZero
Cache if the hash has been calculated as actually being zero, enabling us to avoid recalculating this. |
pack-priv static final byte | |
private static final char | |
private static final ObjectStreamField[] | serialPersistentFields
Class String is special cased within the Serialization Stream Protocol. |
private static final long | serialVersionUID
use serialVersionUID from JDK 1.0.2 for interoperability |
pack-priv static final byte | |
private final byte[] | value
The value is used for character storage. |
Access | Constructor and Description |
---|---|
public | String()
Initializes a newly created |
public | |
public | String(char[]
The initial value of the string value)Allocates a new |
public | String(char[]
Array that is the source of characters value, int The initial offset offset, int The length count)Allocates a new |
public | String(int[]
Array that is the source of Unicode code points codePoints, int The initial offset offset, int The length count)Allocates a new |
public | String(byte[]
The bytes to be converted to characters ascii, int The top 8 bits of each 16-bit Unicode code unit hibyte, int The initial offset offset, int The length count)
Deprecated
since 1.1. This method does not properly convert bytes into characters.
Allocates a new |
public | String(byte[]
The bytes to be converted to characters ascii, int The top 8 bits of each 16-bit Unicode code unit hibyte)
Deprecated
since 1.1. This method does not properly convert bytes into
characters.
Allocates a new |
public | |
public | String(byte[]
The bytes to be decoded into characters bytes, int The index of the first byte to decode offset, int The number of bytes to decode length, Charset The charset to be used to
decode the charset)bytes Constructs a new |
private | |
public | |
public | |
public | String(byte[]
The bytes to be decoded into characters bytes, int The index of the first byte to decode offset, int The number of bytes to decode length)Constructs a new |
public | String(byte[]
The bytes to be decoded into characters bytes)Constructs a new |
public | String(StringBuffer
A buffer)StringBuffer Allocates a new string that contains the sequence of characters currently contained in the string buffer argument. |
public | String(StringBuilder
A builder)StringBuilder Allocates a new string that contains the sequence of characters currently contained in the string builder argument. |
private | |
pack-priv | |
pack-priv |
Modifier and Type | Method and Description |
---|---|
pack-priv boolean | |
public char | Returns: thechar value at the specified index of this string.
The first char value is at index 0 .the index of the index)char value.Implements java. Returns the |
public IntStream | Returns: an IntStream of char values from this sequenceOverrides default java. Returns a stream of |
pack-priv static void | |
pack-priv static int | |
pack-priv static void | |
pack-priv static void | |
public int | Returns: the code point value of the character at theindex the index to the index)char valuesReturns the character (Unicode code point) at the specified index. |
public int | Returns: the Unicode code point value before the given index.the index following the code point that should be returned index)Returns the character (Unicode code point) before the specified index. |
public int | Returns: the number of Unicode code points in the specified text rangethe index to the first beginIndex, int char of
the text range.the index after the last endIndex)char of
the text range.Returns the number of Unicode code points in the specified text
range of this |
public IntStream | Returns: an IntStream of Unicode code points from this sequenceOverrides default java. Returns a stream of code point values from this sequence. |
pack-priv byte | |
public int | Returns: the value0 if the argument string is equal to
this string; a value less than 0 if this string
is lexicographically less than the string argument; and a
value greater than 0 if this string is
lexicographically greater than the string argument.the anotherString)String to be compared.Implements java. Compares two strings lexicographically. |
public int | Returns: a negative integer, zero, or a positive integer as the specified String is greater than, equal to, or less than this String, ignoring case considerations.the str)String to be compared.Compares two strings lexicographically, ignoring case differences. |
private static long | Returns: the exact size required to UTF_8 encode this UTF16 stringUTF16 encoded byte array val, boolean true to replace unmappable characters doReplace)Returns the exact size required to UTF_8 encode this UTF16 string. |
public String | |
public boolean | Returns: true if this string containss , false otherwisethe sequence to search for s)Returns true if and only if this string contains the specified sequence of char values. |
public boolean | Returns: true if this String represents the same
sequence of characters as the specified StringBuffer ,
false otherwiseThe sb)StringBuffer to compare this String againstCompares this string to the specified |
public boolean | Returns: true if this String represents the same
sequence of char values as the specified sequence, false otherwiseThe sequence to compare this cs)String againstCompares this string to the specified |
pack-priv void | |
public static String | Returns: aString that contains the characters of the
specified subarray of the character array.the character array. data, int initial offset of the subarray. offset, int length of the subarray. count)Equivalent to |
public static String | Returns: aString that contains the characters of the
character array.the character array. data)Equivalent to |
private static char | |
private static char | |
private static int | |
pack-priv static int | Returns: the number of bytes successfully decoded, at most lenDecodes ASCII from the source byte array into the destination char array. |
private static int | |
private static int | |
public Optional | describeConstable()
Implements java. Returns an |
private static byte[] | |
private static byte[] | |
private static byte[] | |
private static byte[] | |
private static byte[] | |
private static byte[] | |
private static byte[] | |
public boolean | Returns: true if the character sequence represented by the
argument is a suffix of the character sequence represented by
this object; false otherwise. Note that the
result will be true if the argument is the
empty string or is equal to this String object
as determined by the equals(Object) method.the suffix. suffix)Tests if this string ends with the specified suffix. |
public boolean | Returns: true if the given object represents a String
equivalent to this string, false otherwiseThe object to compare this anObject)String againstOverrides java. Compares this string to the specified object. |
public boolean | Returns: true if the argument is not null and it
represents an equivalent String ignoring case; false otherwiseThe anotherString)String to compare this String againstCompares this |
public static String | Returns: A formatted stringArguments referenced by the format specifiers in the format
string. If there are more arguments than format specifiers, the
extra arguments are ignored. The number of arguments is
variable and may be zero. The maximum number of arguments is
limited by the maximum dimension of a Java array as defined by
The Java Virtual Machine Specification.
The behaviour on a
args)null argument depends on the conversion.Returns a formatted string using the specified format string and arguments. |
public static String | Returns: A formatted stringArguments referenced by the format specifiers in the format
string. If there are more arguments than format specifiers, the
extra arguments are ignored. The number of arguments is
variable and may be zero. The maximum number of arguments is
limited by the maximum dimension of a Java array as defined by
The Java Virtual Machine Specification.
The behaviour on a
args)null argument depends on the
conversion.Returns a formatted string using the specified locale, format string, and arguments. |
public String | |
public void | getBytes(int
Index of the first character in the string to copy srcBegin, int Index after the last character in the string to copy srcEnd, byte[] The destination array dst, int The start offset in the destination array dstBegin)
Deprecated
since 1.1. This method does not properly convert characters into
bytes.
Copies characters from this string into the destination byte array. |
public byte[] | |
public byte[] | Returns: The resultant byte arrayThe java.nio.charset.Charset to be used to encode
the charset)String Encodes this |
public byte[] | Returns: The resultant byte arrayEncodes this |
pack-priv void | getBytes(byte[] dst, int
the char index, not offset of byte[] dstBegin, byte the coder of dst[] coder)Copy character bytes from this string into dst starting at dstBegin. |
pack-priv void | getBytes(byte[] dst, int
the char index, not offset of byte[] srcPos, int the char index to start from dstBegin, byte the coder of dst[] coder, int the amount of copied chars length)Copy character bytes from this string into dst starting at dstBegin. |
pack-priv static byte[] | |
private static byte[] | |
pack-priv static byte[] | |
public void | getChars(int
index of the first character in the string
to copy. srcBegin, int index after the last character in the string
to copy. srcEnd, char[] the destination array. dst, int the start offset in the destination array. dstBegin)Copies characters from this string into the destination character array. |
public int | Returns: a hash code value for this object.Overrides java. Returns a hash code for this string. |
public String | Returns: string with indentation adjusted and line endings normalizednumber of leading
white space characters
to add or remove n)Adjusts the indentation of each line of this string based on the value of
|
public int | Returns: the index of the first occurrence of the character in the character sequence represented by this object, or-1 if the character does not occur.a character (Unicode code point). ch)Returns the index within this string of the first occurrence of the specified character. |
public int | Returns: the index of the first occurrence of the character in the character sequence represented by this object that is greater than or equal tofromIndex , or -1
if the character does not occur.a character (Unicode code point). ch, int the index to start the search from. fromIndex)Returns the index within this string of the first occurrence of the specified character, starting the search at the specified index. |
public int | Returns: the index of the first occurrence of the character in the character sequence represented by this object that is greater than or equal tobeginIndex and less than endIndex ,
or -1 if the character does not occur.a character (Unicode code point). ch, int the index to start the search from (included). beginIndex, int the index to stop the search at (excluded). endIndex)Returns the index within this string of the first occurrence of the
specified character, starting the search at |
public int | |
public int | Returns: the index of the first occurrence of the specified substring, starting at the specified index, or-1 if there is no such occurrence.the substring to search for. str, int the index from which to start the search. fromIndex)Returns the index within this string of the first occurrence of the specified substring, starting at the specified index. |
public int | Returns: the index of the first occurrence of the specified substring within the specified index range, or-1 if there is no such occurrence.the substring to search for. str, int the index to start the search from (included). beginIndex, int the index to stop the search at (excluded). endIndex)Returns the index of the first occurrence of the specified substring
within the specified index range of |
pack-priv static int | indexOf(byte[]
the characters being searched. src, byte the coder of the source string. srcCoder, int last index (exclusive) in the source string. srcCount, String the characters being searched for. tgtStr, int the index to begin searching from. fromIndex)Code shared by String and AbstractStringBuilder to do searches. |
private int | |
public native String | Returns: a string that has the same contents as this string, but is guaranteed to be from a pool of unique strings.Returns a canonical representation for the string object. |
private static boolean | |
public boolean | isBlank()
Returns |
public boolean | isEmpty()
Overrides default java. Returns |
pack-priv boolean | |
private static boolean | |
private static boolean | |
private static boolean | |
private static boolean | |
private static boolean | |
private static boolean | |
public static String | Returns: a newString that is composed of the elements
separated by the delimiter the delimiter that separates each element delimiter, CharSequence... the elements to join together. elements)Returns a new String composed of copies of the
|
pack-priv static String | |
public static String | Returns: a newString that is composed from the elements
argumenta sequence of characters that is used to separate each
of the delimiter, Iterable<? extends CharSequence> elements in the resulting String an elements)Iterable that will have its elements
joined together.Returns a new |
public int | Returns: the index of the last occurrence of the character in the character sequence represented by this object, or-1 if the character does not occur.a character (Unicode code point). ch)Returns the index within this string of the last occurrence of the specified character. |
public int | Returns: the index of the last occurrence of the character in the character sequence represented by this object that is less than or equal tofromIndex , or -1
if the character does not occur before that point.a character (Unicode code point). ch, int the index to start the search from. There is no
restriction on the value of fromIndex)fromIndex . If it is
greater than or equal to the length of this string, it has
the same effect as if it were equal to one less than the
length of this string: this entire string may be searched.
If it is negative, it has the same effect as if it were -1:
-1 is returned.Returns the index within this string of the last occurrence of the specified character, searching backward starting at the specified index. |
public int | Returns: the index of the last occurrence of the specified substring, or-1 if there is no such occurrence.the substring to search for. str)Returns the index within this string of the last occurrence of the specified substring. |
public int | Returns: the index of the last occurrence of the specified substring, searching backward from the specified index, or-1 if there is no such occurrence.the substring to search for. str, int the index to start the search from. fromIndex)Returns the index within this string of the last occurrence of the specified substring, searching backward starting at the specified index. |
pack-priv static int | lastIndexOf(byte[]
the characters being searched. src, byte coder handles the mapping between bytes/chars srcCoder, int count of the source string. srcCount, String the characters being searched for. tgtStr, int the index to begin searching from. fromIndex)Code shared by String and AbstractStringBuilder to do searches. |
private int | |
public int | Returns: the length of the sequence of characters represented by this object.Implements java. Returns the length of this string. |
public Stream | Returns: the stream of lines extracted from this stringReturns a stream of lines extracted from this string, separated by line terminators. |
private static Charset | |
private static int | |
private static int | |
public boolean | Returns: true if, and only if, this string matches the
given regular expressionthe regular expression to which this string is to be matched regex)Tells whether or not this string matches the given regular expression. |
pack-priv static String | |
private static String | |
pack-priv static String | |
private boolean | |
public int | Returns: the index within thisString the index to be offset index, int the offset in code points codePointOffset)Returns the index within this |
private static int | |
private static Void | |
public boolean | Returns: true if the specified subregion of this string
exactly matches the specified subregion of the string argument;
false otherwise.the starting offset of the subregion in this string. toffset, String the string argument. other, int the starting offset of the subregion in the string
argument. ooffset, int the number of characters to compare. len)Tests if two string regions are equal. |
public boolean | Returns: true if the specified subregion of this string
matches the specified subregion of the string argument;
false otherwise. Whether the matching is exact
or case insensitive depends on the ignoreCase
argument.if ignoreCase, int true , ignore case when comparing
characters.the starting offset of the subregion in this
string. toffset, String the string argument. other, int the starting offset of the subregion in the string
argument. ooffset, int the number of characters (Unicode code units -
16bit len)char value) to compare.Tests if two string regions are equal. |
public String | Returns: A string composed of this string repeatedcount times or the empty string if this
string is empty or count is zeronumber of times to repeat count)Returns a string whose value is the concatenation of this
string repeated |
pack-priv static void | repeatCopyRest(byte[]
destination buffer buffer, int offset in the destination buffer offset, int total replicated including what is already in the buffer limit, int number of bytes that have already in the buffer copied)Used to perform copying after the initial insertion. |
public String | Returns: a string derived from this string by replacing every occurrence ofoldChar with newChar .the old character. oldChar, char the new character. newChar)Returns a string resulting from replacing all occurrences of
|
public String | Returns: The resulting stringThe sequence of char values to be replaced target, CharSequence The replacement sequence of char values replacement)Replaces each substring of this string that matches the literal target sequence with the specified literal replacement sequence. |
public String | Returns: The resultingString the regular expression to which this string is to be matched regex, String the string to be substituted for each match replacement)Replaces each substring of this string that matches the given regular expression with the given replacement. |
public String | Returns: The resultingString the regular expression to which this string is to be matched regex, String the string to be substituted for the first match replacement)Replaces the first substring of this string that matches the given regular expression with the given replacement. |
private static void | |
public String | Returns: the String instanceignored lookupImplements java. Resolves this instance as a |
private static byte[] | |
private static int | |
public String[] | Returns: the array of strings computed by splitting this string around matches of the given regular expressionthe delimiting regular expression regex, int the result threshold, as described above limit)Splits this string around matches of the given regular expression. |
private String[] | |
private String[] | |
public String[] | Returns: the array of strings computed by splitting this string around matches of the given regular expressionthe delimiting regular expression regex)Splits this string around matches of the given regular expression. |
public String[] | Returns: the array of strings computed by splitting this string around matches of the given regular expression, alternating substrings and matching delimitersthe delimiting regular expression regex, int the result threshold, as described above limit)Splits this string around matches of the given regular expression and returns both the strings and the matching delimiters. |
public boolean | Returns: true if the character sequence represented by the
argument is a prefix of the substring of this object starting
at index toffset ; false otherwise.
The result is false if toffset is
negative or greater than the length of this
String object; otherwise the result is the same
as the result of the expression
this.substring(toffset).startsWith(prefix) the prefix. prefix, int where to begin looking in this string. toffset)Tests if the substring of this string beginning at the specified index starts with the specified prefix. |
public boolean | Returns: true if the character sequence represented by the
argument is a prefix of the character sequence represented by
this string; false otherwise.
Note also that true will be returned if the
argument is an empty string or is equal to this
String object as determined by the
equals(Object) method.the prefix. prefix)Tests if this string starts with the specified prefix. |
public String | Returns: a string whose value is this string, with all leading and trailing white space removedReturns a string whose value is this string, with all leading and trailing white space removed. |
public String | Returns: string with incidental indentation removed and line terminators normalizedReturns a string whose value is this string, with incidental white space removed from the beginning and end of every line. |
public String | Returns: a string whose value is this string, with all leading white space removedReturns a string whose value is this string, with all leading white space removed. |
public String | Returns: a string whose value is this string, with all trailing white space removedReturns a string whose value is this string, with all trailing white space removed. |
public CharSequence | Returns: the specified subsequence.the begin index, inclusive. beginIndex, int the end index, exclusive. endIndex)Implements java. Returns a character sequence that is a subsequence of this sequence. |
public String | Returns: the specified substring.the beginning index, inclusive. beginIndex)Returns a string that is a substring of this string. |
public String | Returns: the specified substring.the beginning index, inclusive. beginIndex, int the ending index, exclusive. endIndex)Returns a string that is a substring of this string. |
private static void | |
private static void | |
private static void | |
private static void | |
public char[] | Returns: a newly allocated character array whose length is the length of this string and whose contents are initialized to contain the character sequence represented by this string.Converts this string to a new character array. |
public String | Returns: theString , converted to lowercase.use the case transformation rules for this locale locale)Converts all of the characters in this |
public String | Returns: theString , converted to lowercase.Converts all of the characters in this |
public String | Returns: the string itself.Overrides java. Implements java. This object (which is already a string!) is itself returned. |
public String | Returns: theString , converted to uppercase.use the case transformation rules for this locale locale)Converts all of the characters in this |
public String | Returns: theString , converted to uppercase.Converts all of the characters in this |
public < the type of the result R> R | |
public String | Returns: String with escape sequences translated.Returns a string whose value is this string, with escape sequences translated as if in a string literal. |
public String | Returns: a string whose value is this string, with all leading and trailing space removed, or this string if it has no leading or trailing space.Returns a string whose value is this string, with all leading
and trailing space removed, where space is defined
as any character whose codepoint is less than or equal to
|
pack-priv byte[] | |
public static String | |
public static String | Returns: aString that contains the characters of the
character array.the character array. data)Returns the string representation of the |
public static String | Returns: aString that contains the characters of the
specified subarray of the character array.the character array. data, int initial offset of the subarray. offset, int length of the subarray. count)Returns the string representation of a specific subarray of the
|
public static String | Returns: if the argument istrue , a string equal to
"true" is returned; otherwise, a string equal to
"false" is returned.a b)boolean .Returns the string representation of the |
public static String | Returns: a string of length1 containing
as its single character the argument c .a c)char .Returns the string representation of the |
public static String | Returns: a string representation of theint argument.an i)int .Returns the string representation of the |
public static String | Returns: a string representation of thelong argument.a l)long .Returns the string representation of the |
public static String | Returns: a string representation of thefloat argument.a f)float .Returns the string representation of the |
public static String | Returns: a string representation of thedouble argument.a d)double .Returns the string representation of the |
pack-priv static String | Returns: a string of length1 or 2 containing
as its single character the argument codePoint .a codePoint)codePoint .Returns the string representation of the |