GeezerVison is showing docs.oracle.com — String (Java Platform SE 8 )
Java™ Platform Standard Ed. 8
compact1, compact2, compact3
java.lang
Class String
-
-
Constructor Summary
Constructors
| Constructor |
Description |
String() |
Initializes a newly created String object so that it represents
an empty character sequence.
|
String(byte[] bytes) |
Constructs a new String by decoding the specified array of bytes
using the platform's default charset.
|
String(byte[] bytes,
Charset charset) |
Constructs a new String by decoding the specified array of
bytes using the specified charset.
|
String(byte[] ascii,
int hibyte) |
Deprecated.
This method does not properly convert bytes into
characters. As of JDK 1.1, the preferred way to do this is via the
String constructors that take a Charset, charset name, or that use the platform's
default charset.
|
String(byte[] bytes,
int offset,
int length) |
Constructs a new String by decoding the specified subarray of
bytes using the platform's default charset.
|
String(byte[] bytes,
int offset,
int length,
Charset charset) |
Constructs a new String by decoding the specified subarray of
bytes using the specified charset.
|
String(byte[] ascii,
int hibyte,
int offset,
int count) |
Deprecated.
This method does not properly convert bytes into characters.
As of JDK 1.1, the preferred way to do this is via the
String constructors that take a Charset, charset name, or that use the platform's
default charset.
|
String(byte[] bytes,
int offset,
int length,
String charsetName) |
Constructs a new String by decoding the specified subarray of
bytes using the specified charset.
|
String(byte[] bytes,
String charsetName) |
Constructs a new String by decoding the specified array of bytes
using the specified charset.
|
String(char[] value) |
Allocates a new String so that it represents the sequence of
characters currently contained in the character array argument.
|
String(char[] value,
int offset,
int count) |
Allocates a new String that contains characters from a subarray
of the character array argument.
|
String(int[] codePoints,
int offset,
int count) |
Allocates a new String that contains characters from a subarray
of the Unicode code point array
argument.
|
String(String original) |
Initializes a newly created String object so that it represents
the same sequence of characters as the argument; in other words, the
newly created string is a copy of the argument string.
|
String(StringBuffer buffer) |
Allocates a new string that contains the sequence of characters
currently contained in the string buffer argument.
|
String(StringBuilder builder) |
Allocates a new string that contains the sequence of characters
currently contained in the string builder argument.
|
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Deprecated Methods
| Modifier and Type |
Method |
Description |
char |
charAt(int index) |
Returns the char value at the
specified index.
|
int |
codePointAt(int index) |
Returns the character (Unicode code point) at the specified
index.
|
int |
codePointBefore(int index) |
Returns the character (Unicode code point) before the specified
index.
|
int |
codePointCount(int beginIndex,
int endIndex) |
Returns the number of Unicode code points in the specified text
range of this String.
|
int |
compareTo(String anotherString) |
Compares two strings lexicographically.
|
int |
compareToIgnoreCase(String str) |
Compares two strings lexicographically, ignoring case
differences.
|
String |
concat(String str) |
Concatenates the specified string to the end of this string.
|
boolean |
contains(CharSequence s) |
Returns true if and only if this string contains the specified
sequence of char values.
|
boolean |
contentEquals(CharSequence cs) |
Compares this string to the specified CharSequence.
|
boolean |
contentEquals(StringBuffer sb) |
Compares this string to the specified StringBuffer.
|
static String |
copyValueOf(char[] data) |
|
static String |
copyValueOf(char[] data,
int offset,
int count) |
|
boolean |
endsWith(String suffix) |
Tests if this string ends with the specified suffix.
|
boolean |
equals(Object anObject) |
Compares this string to the specified object.
|
boolean |
equalsIgnoreCase(String anotherString) |
Compares this String to another String, ignoring case
considerations.
|
static String |
format(Locale l,
String format,
Object... args) |
Returns a formatted string using the specified locale, format string,
and arguments.
|
static String |
format(String format,
Object... args) |
Returns a formatted string using the specified format string and
arguments.
|
byte[] |
getBytes() |
Encodes this String into a sequence of bytes using the
platform's default charset, storing the result into a new byte array.
|
byte[] |
getBytes(Charset charset) |
Encodes this String into a sequence of bytes using the given
charset, storing the result into a
new byte array.
|
void |
getBytes(int srcBegin,
int srcEnd,
byte[] dst,
int dstBegin) |
Deprecated.
This method does not properly convert characters into
bytes. As of JDK 1.1, the preferred way to do this is via the
getBytes() method, which uses the platform's default charset.
|
byte[] |
getBytes(String charsetName) |
Encodes this String into a sequence of bytes using the named
charset, storing the result into a new byte array.
|
void |
getChars(int srcBegin,
int srcEnd,
char[] dst,
int dstBegin) |
Copies characters from this string into the destination character
array.
|
int |
hashCode() |
Returns a hash code for this string.
|
int |
indexOf(int ch) |
Returns the index within this string of the first occurrence of
the specified character.
|
int |
indexOf(int ch,
int fromIndex) |
Returns the index within this string of the first occurrence of the
specified character, starting the search at the specified index.
|
int |
indexOf(String str) |
Returns the index within this string of the first occurrence of the
specified substring.
|
int |
indexOf(String str,
int fromIndex) |
Returns the index within this string of the first occurrence of the
specified substring, starting at the specified index.
|
String |
intern() |
Returns a canonical representation for the string object.
|
boolean |
isEmpty() |
Returns true if, and only if, length() is 0.
|
static String |
join(CharSequence delimiter,
CharSequence... elements) |
Returns a new String composed of copies of the
CharSequence elements joined together with a copy of
the specified delimiter.
|
static String |
join(CharSequence delimiter,
Iterable<? extends CharSequence> elements) |
Returns a new String composed of copies of the
CharSequence elements joined together with a copy of the
specified delimiter.
|
int |
lastIndexOf(int ch) |
Returns the index within this string of the last occurrence of
the specified character.
|
int |
lastIndexOf(int ch,
int fromIndex) |
Returns the index within this string of the last occurrence of
the specified character, searching backward starting at the
specified index.
|
int |
lastIndexOf(String str) |
Returns the index within this string of the last occurrence of the
specified substring.
|
int |
lastIndexOf(String str,
int fromIndex) |
Returns the index within this string of the last occurrence of the
specified substring, searching backward starting at the specified index.
|
int |
length() |
Returns the length of this string.
|
boolean |
matches(String regex) |
|
int |
offsetByCodePoints(int index,
int codePointOffset) |
Returns the index within this String that is
offset from the given index by
codePointOffset code points.
|
boolean |
regionMatches(boolean ignoreCase,
int toffset,
String other,
int ooffset,
int len) |
Tests if two string regions are equal.
|
boolean |
regionMatches(int toffset,
String other,
int ooffset,
int len) |
Tests if two string regions are equal.
|
String |
replace(char oldChar,
char newChar) |
Returns a string resulting from replacing all occurrences of
oldChar in this string with newChar.
|
String |
replace(CharSequence target,
CharSequence replacement) |
Replaces each substring of this string that matches the literal target
sequence with the specified literal replacement sequence.
|
String |
replaceAll(String regex,
String replacement) |
Replaces each substring of this string that matches the given regular expression with the
given replacement.
|
String |
replaceFirst(String regex,
String replacement) |
Replaces the first substring of this string that matches the given regular expression with the
given replacement.
|
String[] |
split(String regex) |
|
String[] |
split(String regex,
int limit) |
|
boolean |
startsWith(String prefix) |
Tests if this string starts with the specified prefix.
|
boolean |
startsWith(String prefix,
int toffset) |
Tests if the substring of this string beginning at the
specified index starts with the specified prefix.
|
CharSequence |
subSequence(int beginIndex,
int endIndex) |
Returns a character sequence that is a subsequence of this sequence.
|
String |
substring(int beginIndex) |
Returns a string that is a substring of this string.
|
String |
substring(int beginIndex,
int endIndex) |
Returns a string that is a substring of this string.
|
char[] |
toCharArray() |
Converts this string to a new character array.
|
String |
toLowerCase() |
Converts all of the characters in this String to lower
case using the rules of the default locale.
|
String |
toLowerCase(Locale locale) |
Converts all of the characters in this String to lower
case using the rules of the given Locale.
|
String |
toString() |
This object (which is already a string!) is itself returned.
|
String |
toUpperCase() |
Converts all of the characters in this String to upper
case using the rules of the default locale.
|
String |
toUpperCase(Locale locale) |
Converts all of the characters in this String to upper
case using the rules of the given Locale.
|
String |
trim() |
Returns a string whose value is this string, with any leading and trailing
whitespace removed.
|
static String |
valueOf(boolean b) |
Returns the string representation of the boolean argument.
|
static String |
valueOf(char c) |
Returns the string representation of the char
argument.
|
static String |
valueOf(char[] data) |
Returns the string representation of the char array
argument.
|
static String |
valueOf(char[] data,
int offset,
int count) |
Returns the string representation of a specific subarray of the
char array argument.
|
static String |
valueOf(double d) |
Returns the string representation of the double argument.
|
static String |
valueOf(float f) |
Returns the string representation of the float argument.
|
static String |
valueOf(int i) |
Returns the string representation of the int argument.
|
static String |
valueOf(long l) |
Returns the string representation of the long argument.
|
static String |
valueOf(Object obj) |
Returns the string representation of the Object argument.
|
-
-
Constructor Detail
-
String
public String(String original)
Initializes a newly created String object so that it represents
the same sequence of characters as the argument; in other words, the
newly created string is a copy of the argument string. Unless an
explicit copy of original is needed, use of this constructor is
unnecessary since Strings are immutable.
- Parameters:
original - A String
-
String
public String(char[] value)
Allocates a new String so that it represents the sequence of
characters currently contained in the character array argument. The
contents of the character array are copied; subsequent modification of
the character array does not affect the newly created string.
- Parameters:
value - The initial value of the string
-
String
public String(char[] value,
int offset,
int count)
Allocates a new String that contains characters from a subarray
of the character array argument. The offset argument is the
index of the first character of the subarray and the count
argument specifies the length of the subarray. The contents of the
subarray are copied; subsequent modification of the character array does
not affect the newly created string.
- Parameters:
value - Array that is the source of characters
offset - The initial offset
count - The length
- Throws:
IndexOutOfBoundsException - If the offset and count arguments index
characters outside the bounds of the value array
-
String
public String(byte[] bytes,
int offset,
int length,
String charsetName)
throws UnsupportedEncodingException
Constructs a new String by decoding the specified subarray of
bytes using the specified charset. The length of the new String
is a function of the charset, and hence may not be equal to the length
of the subarray.
The behavior of this constructor when the given bytes are not valid
in the given charset is unspecified. The CharsetDecoder class should be used when more control
over the decoding process is required.
- Parameters:
bytes - The bytes to be decoded into characters
offset - The index of the first byte to decode
length - The number of bytes to decode
charsetName - The name of a supported charset
- Throws:
UnsupportedEncodingException - If the named charset is not supported
IndexOutOfBoundsException - If the offset and length arguments index
characters outside the bounds of the bytes array
- Since:
- JDK1.1
-
String
public String(StringBuffer buffer)
Allocates a new string that contains the sequence of characters
currently contained in the string buffer argument. The contents of the
string buffer are copied; subsequent modification of the string buffer
does not affect the newly created string.
- Parameters:
buffer - A StringBuffer
-
Method Detail
-
length
public int length()
Returns the length of this string.
The length is equal to the number of Unicode
code units in the string.
- Specified by:
length in interface CharSequence
- Returns:
- the length of the sequence of characters represented by this
object.
-
isEmpty
public boolean isEmpty()
Returns true if, and only if, length() is 0.
- Returns:
true if length() is 0, otherwise
false
- Since:
- 1.6
-
codePointCount
public int codePointCount(int beginIndex,
int endIndex)
Returns the number of Unicode code points in the specified text
range of this String. The text range begins at the
specified beginIndex and extends to the
char at index endIndex - 1. Thus the
length (in chars) of the text range is
endIndex-beginIndex. Unpaired surrogates within
the text range count as one code point each.
- Parameters:
beginIndex - the index to the first char of
the text range.
endIndex - the index after the last char of
the text range.
- Returns:
- the number of Unicode code points in the specified text
range
- Throws:
IndexOutOfBoundsException - if the
beginIndex is negative, or endIndex
is larger than the length of this String, or
beginIndex is larger than endIndex.
- Since:
- 1.5
-
offsetByCodePoints
public int offsetByCodePoints(int index,
int codePointOffset)
Returns the index within this String that is
offset from the given index by
codePointOffset code points. Unpaired surrogates
within the text range given by index and
codePointOffset count as one code point each.
- Parameters:
index - the index to be offset
codePointOffset - the offset in code points
- Returns:
- the index within this
String
- Throws:
IndexOutOfBoundsException - if index
is negative or larger then the length of this
String, or if codePointOffset is positive
and the substring starting with index has fewer
than codePointOffset code points,
or if codePointOffset is negative and the substring
before index has fewer than the absolute value
of codePointOffset code points.
- Since:
- 1.5
-
getBytes
public byte[] getBytes()
Encodes this String into a sequence of bytes using the
platform's default charset, storing the result into a new byte array.
The behavior of this method when this string cannot be encoded in
the default charset is unspecified. The CharsetEncoder class should be used when more control
over the encoding process is required.
- Returns:
- The resultant byte array
- Since:
- JDK1.1
-
equals
public boolean equals(Object anObject)
Compares this string to the specified object. The result is true if and only if the argument is not null and is a String object that represents the same sequence of characters as this
object.
- Overrides:
equals in class Object
- Parameters:
anObject - The object to compare this String against
- Returns:
true if the given object represents a String
equivalent to this string, false otherwise
- See Also:
compareTo(String),
equalsIgnoreCase(String)
-
contentEquals
public boolean contentEquals(StringBuffer sb)
Compares this string to the specified StringBuffer. The result
is true if and only if this String represents the same
sequence of characters as the specified StringBuffer. This method
synchronizes on the StringBuffer.
- Parameters:
sb - The StringBuffer to compare this String against
- Returns:
true if this String represents the same
sequence of characters as the specified StringBuffer,
false otherwise
- Since:
- 1.4
-
contentEquals
public boolean contentEquals(CharSequence cs)
Compares this string to the specified CharSequence. The
result is true if and only if this String represents the
same sequence of char values as the specified sequence. Note that if the
CharSequence is a StringBuffer then the method
synchronizes on it.
- Parameters:
cs - The sequence to compare this String against
- Returns:
true if this String represents the same
sequence of char values as the specified sequence, false otherwise
- Since:
- 1.5
-
regionMatches
public boolean regionMatches(boolean ignoreCase,
int toffset,
String other,
int ooffset,
int len)
Tests if two string regions are equal.
A substring of this String object is compared to a substring
of the argument other. The result is true if these
substrings represent character sequences that are the same, ignoring
case if and only if ignoreCase is true. The substring of
this String object to be compared begins at index
toffset and has length len. The substring of
other to be compared begins at index ooffset and
has length len. The result is false if and only if
at least one of the following is true:
toffset is negative.
ooffset is negative.
toffset+len is greater than the length of this
String object.
ooffset+len is greater than the length of the other
argument.
ignoreCase is false and there is some nonnegative
integer k less than len such that:
this.charAt(toffset+k) != other.charAt(ooffset+k)
ignoreCase is true and there is some nonnegative
integer k less than len such that:
Character.toLowerCase(this.charAt(toffset+k)) !=
Character.toLowerCase(other.charAt(ooffset+k))
and:
Character.toUpperCase(this.charAt(toffset+k)) !=
Character.toUpperCase(other.charAt(ooffset+k))
- Parameters:
ignoreCase - if true, ignore case when comparing
characters.
toffset - the starting offset of the subregion in this
string.
other - the string argument.
ooffset - the starting offset of the subregion in the string
argument.
len - the number of characters to compare.
- 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.
-
startsWith
public boolean startsWith(String prefix,
int toffset)
Tests if the substring of this string beginning at the
specified index starts with the specified prefix.
- Parameters:
prefix - the prefix.
toffset - where to begin looking in this string.
- 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)
-
hashCode
public int hashCode()
Returns a hash code for this string. The hash code for a
String object is computed as
s[0]*31^(n-1) + s[1]*31^(n-2) + ... + s[n-1]
using int arithmetic, where s[i] is the
ith character of the string, n is the length of
the string, and ^ indicates exponentiation.
(The hash value of the empty string is zero.)
- Overrides:
hashCode in class Object
- Returns:
- a hash code value for this object.
- See Also:
Object.equals(java.lang.Object),
System.identityHashCode(java.lang.Object)
-
replace
public String replace(CharSequence target,
CharSequence replacement)
Replaces each substring of this string that matches the literal target
sequence with the specified literal replacement sequence. The
replacement proceeds from the beginning of the string to the end, for
example, replacing "aa" with "b" in the string "aaa" will result in
"ba" rather than "ab".
- Parameters:
target - The sequence of char values to be replaced
replacement - The replacement sequence of char values
- Returns:
- The resulting string
- Since:
- 1.5
-
trim
public String trim()
Returns a string whose value is this string, with any leading and trailing
whitespace removed.
If this String object represents an empty character
sequence, or the first and last characters of character sequence
represented by this String object both have codes
greater than '\u0020' (the space character), then a
reference to this String object is returned.
Otherwise, if there is no character with a code greater than
'\u0020' in the string, then a
String object representing an empty string is
returned.
Otherwise, let k be the index of the first character in the
string whose code is greater than '\u0020', and let
m be the index of the last character in the string whose code
is greater than '\u0020'. A String
object is returned, representing the substring of this string that
begins with the character at index k and ends with the
character at index m-that is, the result of
this.substring(k, m + 1).
This method may be used to trim whitespace (as defined above) from
the beginning and end of a string.
- Returns:
- A string whose value is this string, with any leading and trailing white
space removed, or this string if it has no leading or
trailing white space.
-
format
public static String format(Locale l,
String format,
Object... args)
Returns a formatted string using the specified locale, format string,
and arguments.
- Parameters:
l - The locale to apply during
formatting. If l is null then no localization
is applied.
format - A format string
args - Arguments 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
null argument depends on the
conversion.
- Returns:
- A formatted string
- Throws:
IllegalFormatException - If a format string contains an illegal syntax, a format
specifier that is incompatible with the given arguments,
insufficient arguments given the format string, or other
illegal conditions. For specification of all possible
formatting errors, see the Details section of the
formatter class specification
- Since:
- 1.5
- See Also:
Formatter
-
valueOf
public static String valueOf(char[] data)
Returns the string representation of the char array
argument. The contents of the character array are copied; subsequent
modification of the character array does not affect the returned
string.
- Parameters:
data - the character array.
- Returns:
- a
String that contains the characters of the
character array.
-
intern
public String intern()
Returns a canonical representation for the string object.
A pool of strings, initially empty, is maintained privately by the
class String.
When the intern method is invoked, if the pool already contains a
string equal to this String object as determined by
the equals(Object) method, then the string from the pool is
returned. Otherwise, this String object is added to the
pool and a reference to this String object is returned.
It follows that for any two strings s and t,
s.intern() == t.intern() is true
if and only if s.equals(t) is true.
All literal strings and string-valued constant expressions are
interned. String literals are defined in section 3.10.5 of the
The Java™ Language Specification.
- Returns:
- a string that has the same contents as this string, but is
guaranteed to be from a pool of unique strings.
Java™ Platform Standard Ed. 8
Submit a bug or feature For further API reference and developer documentation, see Java SE Documentation. That documentation contains more detailed, developer-targeted descriptions, with conceptual overviews, definitions of terms, workarounds, and working code examples. Copyright © 1993, 2026, Oracle and/or its affiliates. All rights reserved. Use is subject to license terms. Also see the documentation redistribution policy.
|