1 String类
String
class represents character strings. All"abc"
, are
* 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 {
* The Java language provides special support for the string
* concatenation operator ( + ), and for conversion of
* other objects to strings. String concatenation is implemented
* through the StringBuilder
(or StringBuffer
)
* class and its append
method.
* String conversions are implemented through the method
* toString
, defined by Object
and
* inherited by all classes in Java. For additional information on
* string concatenation and conversion, see Gosling, Joy, and Steele,
* The Java Language Specification.
*
*
Unless otherwise noted, passing a null argument to a constructor
* or method in this class will cause a {
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).
*
*
*TC_STRING
(utf String)
*
DataOutput.writeUTF
.String
object so that itString
object so that itoriginal
is needed, use of this String
.String
so that it represents theString
that contains characters fromoffset
count
argument specifies the length of theoffset
count
arguments index characters outsidevalue
array.String
that contains charactersoffset
argument is the index of the first codecount
argumentchar
s; subsequentint
array does not affect thecodePoints
offset
count
arguments index characters outsidecodePoints
array.String
constructed from a subarray
* The offset
argument is the index of the first byte
* of the subarray, and the count
argument specifies the
* length of the subarray.
*
* Each byte
in the subarray is converted to a
* char
as specified in the method above.
*
*
String
constructors that take a charset name or that useoffset
count
argument is invalid.String
containing characters
* c == (char)(((hibyte & 0xff) << 8)
* | (b & 0xff))
*
String
constructors that take a charset name or The behavior of this constructor when the given bytes are not valid
* in the given charset is unspecified. The {
}
*
@throws UnsupportedEncodingException The behavior of this constructor when the given bytes are not valid
* in the given charset is unspecified. The {
}
*
*
@exception UnsupportedEncodingException The behavior of this constructor when the given bytes are not valid
* in the default charset is unspecified. The {
offset
and the length
bytes
array The behavior of this constructor when the given bytes are not valid
* in the default charset is unspecified. The {
StringBuffer
.This constructor is provided to ease migration to
* StringBuilder
. Obtaining a string from a string builder
* via the toString
method is likely to run faster and is
* generally preferred.
*
*
StringBuilder
char
value at the0
tolength() - 1
. The first char
value of the sequence0
, the next at index 1
,If the char
value specified by the index is a
* surrogate, the surrogate
* value is returned.
*
*
char
value.char
value at the specified index of this string.char
value is at index 0
.index
char
values0
to - 1
. If the char
value specified at the given index
* is in the high-surrogate range, the following index is less
* than the length of this String
, and the
* char
value at the following index is in the
* low-surrogate range, then the supplementary code point
* corresponding to this surrogate pair is returned. Otherwise,
* the char
value at the given index is returned.
*
*
char
valuesindex
index
char
values1
to {@link If the char
value at (index - 1)
* is in the low-surrogate range, (index - 2)
is not
* negative, and the char
value at (index -
is in the high-surrogate range, then the
* 2)
* supplementary code point value of the surrogate pair is
* returned. If the char
value at index -
is an unpaired low-surrogate or a high-surrogate, the
* 1
* surrogate value is returned.
*
*
index
String
. The text range begins at thebeginIndex
and extends to thechar
at index endIndex - 1
. Thus thechar
s) of the text range isendIndex-beginIndex
. Unpaired surrogates withinchar
ofchar
ofbeginIndex
is negative, or endIndex
String
, orbeginIndex
is larger than endIndex
.String
that isindex
bycodePointOffset
code points. Unpaired surrogatesindex
andcodePointOffset
count as one code point each.String
index
String
, or if codePointOffset
is positiveindex
has fewercodePointOffset
code points,codePointOffset
is negative and the substringindex
has fewer than the absolute valuecodePointOffset
code points.
* The first character to be copied is at index srcBegin
;
* the last character to be copied is at index srcEnd-1
* (thus the total number of characters to be copied is
* srcEnd-srcBegin
). The characters are copied into the
* subarray of dst
starting at index dstBegin
* and ending at index:
*
* dstbegin + (srcEnd-srcBegin) - 1
*
srcBegin
is negative.srcBegin
is greater than srcEnd
srcEnd
is greater than the length of thisdstBegin
is negativedstBegin+(srcEnd-srcBegin)
is larger thandst.length
* The first character to be copied is at index srcBegin
;
* the last character to be copied is at index srcEnd-1
.
* The total number of characters to be copied is
* srcEnd-srcBegin
. The characters, converted to bytes,
* are copied into the subarray of dst
starting at index
* dstBegin
and ending at index:
*
* dstbegin + (srcEnd-srcBegin) - 1
*
getBytes()
method, which uses the platform's defaultsrcBegin
is negativesrcBegin
is greater than srcEnd
srcEnd
is greater than the length of thisdstBegin
is negativedstBegin+(srcEnd-srcBegin)
is larger thandst.length
The behavior of this method when this string cannot be encoded in
* the given charset is unspecified. The {
}
*
*
@return The resultant byte array The behavior of this method when this string cannot be encoded in
* the default charset is unspecified. The {
true
if and only if the argument is notnull
and is a String
object that representsString
true
if the String
are equal;false
otherwise.sb
is null
cs
is null
String
to another String
,
* Two characters c1
and c2
are considered
* the same, ignoring case if at least one of the following is true:
*
==
operator).String
to compare thisString
against.true
if the argument is not null
String
s are equal,false
otherwise.String
object is compared lexicographically to theString
objectString
object lexicographicallycompareTo
returns 0
exactly whentrue
.
* This is the definition of lexicographic ordering. If two strings are
* different, then either they have different characters at some index
* that is a valid index for both strings, or their lengths are different,
* or both. If they have different characters at one or more index
* positions, let k be the smallest such index; then the string
* whose character at position k has the smaller value, as
* determined by using the < operator, lexicographically precedes the
* other string. In this case, compareTo
returns the
* difference of the two character values at position k
in
* the two string -- that is, the value:
*
* this.charAt(k)-anotherString.charAt(k)
*
compareTo
returns the difference of the lengths of the
* this.length()-anotherString.length()
*
String
to be compared.0
if the argument string is equal to0
if this string0
if this string isString
objects as bycompareToIgnoreCase
. This comparator is serializable.
* Note that this Comparator does not take locale into account,
* and will result in an unsatisfactory ordering for certain locales.
* The java.text package provides Collators to allow
* locale-sensitive ordering.
*
*
compareTo
with normalized versions of the stringsCharacter.toLowerCase(Character.toUpperCase(character))
on
* Note that this method does not take locale into account,
* and will result in an unsatisfactory ordering for certain locales.
* The java.text package provides collators to allow
* locale-sensitive ordering.
*
*
String
to be compared.
* A substring of this String object is compared to a substring
* of the argument other. The result is true if these substrings
* represent identical character sequences. 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:
*
true
if the specified subregion of this stringfalse
otherwise.
* 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:
*
* this.charAt(toffset+k) != other.charAt(ooffset+k)
*
* Character.toLowerCase(this.charAt(toffset+k)) !=
Character.toLowerCase(other.charAt(ooffset+k))
*
* Character.toUpperCase(this.charAt(toffset+k)) !=
* Character.toUpperCase(other.charAt(ooffset+k))
*
true
, ignore case when comparingtrue
if the specified subregion of this stringfalse
otherwise. Whether the matching is exactignoreCase
true
if the character sequence represented by thetoffset
; false
otherwise.false
if toffset
isString
object; otherwise the result is the same
* this.substring(toffset).startsWith(prefix)
*
true
if the character sequence represented by thefalse
otherwise.true
will be returned if theString
object as determined by thetrue
if the character sequence represented by thefalse
otherwise. Note that thetrue
if the argument is theString
objectString
object is computed as
* s[0]*31^(n-1) + s[1]*31^(n-2) + ... + s[n-1]
*
int
arithmetic, where s[i]
is then
is the length of^
indicates exponentiation.ch
occurs in the character sequence represented byString
object, then the index (in Unicodech
in the range from 0 to 0xFFFF
* this.charAt(k) == ch
*
ch
, it is the
* this.codePointAt(k) == ch
*
-1
is returned.-1
if the character does not occur.
* If a character with value ch
occurs in the
* character sequence represented by this String
* object at an index no smaller than fromIndex
, then
* the index of the first such occurrence is returned. For values
* of ch
in the range from 0 to 0xFFFF (inclusive),
* this is the smallest value k such that:
*
* (this.charAt(k) == ch) && (k >= fromIndex)
*
ch
, it is the
* (this.codePointAt(k) == ch) && (k >= fromIndex)
*
fromIndex
, then-1
is returned.
* There is no restriction on the value of fromIndex
. If it
* is negative, it has the same effect as if it were zero: this entire
* string may be searched. If it is greater than the length of this
* string, it has the same effect as if it were equal to the length of
* this string: -1
is returned.
*
*
All indices are specified in char
values
* (Unicode code units).
*
*
fromIndex
, or -1
ch
in the
* this.charAt(k) == ch
*
ch
, it is the
* this.codePointAt(k) == ch
*
-1
is returned. TheString
is searched backwards starting at the last-1
if the character does not occur.ch
in the range
* (this.charAt(k) == ch) && (k <= fromIndex)
*
ch
, it is the
* (this.codePointAt(k) == ch) && (k <= fromIndex)
*
fromIndex
, then-1
is returned.All indices are specified in char
values
* (Unicode code units).
*
*
fromIndex
. If it isfromIndex
, or -1
* this.startsWith(str, k)
*
true
.-1
is returned.
* k >= Math.min(fromIndex, str.length()) && this.startsWith(str, k)
*
this.length()
.
* this.startsWith(str, k)
*
-1
is returned.
* k <= Math.min(fromIndex, str.length()) && this.startsWith(str, k)
*
* Examples:
*
* "unhappy".substring(2) returns "happy"
* "Harbison".substring(3) returns "bison"
* "emptiness".substring(9) returns "" (an empty string)
*
beginIndex
is negative or larger than theString
object.beginIndex
andendIndex - 1
.endIndex-beginIndex
.
* Examples:
*
* "hamburger".substring(4, 8) returns "urge"
* "smiles".substring(1, 5) returns "mile"
*
beginIndex
is negative, orendIndex
is larger than the length ofString
object, orbeginIndex
is larger thanendIndex
. An invocation of this method of the form
*
*
* str.subSequence(begin, end)
* str.substring(begin, end)
* If the length of the argument string is 0
, then this
* String
object is returned. Otherwise, a new
* String
object is created, representing a character
* sequence that is the concatenation of the character sequence
* represented by this String
object and the character
* sequence represented by the argument string.
* Examples:
*
* "cares".concat("s") returns "caress"
* "to".concat("get").concat("her") returns "together"
*
String
that is concatenated to the endString
.oldChar
in this string with newChar
.
* If the character oldChar
does not occur in the
* character sequence represented by this String
object,
* then a reference to this String
object is returned.
* Otherwise, a new String
object is created that
* represents a character sequence identical to the character sequence
* represented by this String
object, except that every
* occurrence of oldChar
is replaced by an occurrence
* of newChar
.
*
* Examples:
*
* "mesquite in your cellar".replace('e', 'o')
* returns "mosquito in your collar"
* "the war of baronets".replace('r', 'y')
* returns "the way of bayonets"
* "sparring with a purple porpoise".replace('p', 't')
* returns "starring with a turtle tortoise"
* "JonL".replace('q', 'x') returns "JonL" (no change)
*
oldChar
with newChar
. An invocation of this method of the form
* str.matches(regex) yields exactly the
* same result as the expression
*
*
{@link java.util.regex.Pattern}.{@link
s
, false otherwises
is null
An invocation of this method of the form
* str.replaceFirst(regex, repl)
* yields exactly the same result as the expression
*
*
@link java.util.regex.Pattern}.{@link java.util.regex.Pattern#compile
* {
An invocation of this method of the form
* str.replaceAll(regex, repl)
* yields exactly the same result as the expression
*
*
@link java.util.regex.Pattern}.{@link java.util.regex.Pattern#compile
* {
target
orreplacement
is null
. The array returned by this method contains each substring of this
* string that is terminated by another substring that matches the given
* expression or is terminated by the end of the string. The substrings in
* the array are in the order in which they occur in this string. If the
* expression does not match any part of the input then the resulting array
* has just one element, namely this string.
*
*
The limit parameter controls the number of times the
* pattern is applied and therefore affects the length of the resulting
* array. If the limit n is greater than zero then the pattern
* will be applied at most n - 1 times, the array's
* length will be no greater than n, and the array's last entry
* will contain all input beyond the last matched delimiter. If n
* is non-positive then the pattern will be applied as many times as
* possible and the array can have any length. If n is zero then
* the pattern will be applied as many times as possible, the array can
* have any length, and trailing empty strings will be discarded.
*
*
The string "boo:and:foo", for example, yields the
* following results with these parameters:
*
*
*
*Regex
*Limit
*Result
*
*:
*2
*{ "boo", "and:foo" }
*:
*5
*{ "boo", "and", "foo" }
*:
*-2
*{ "boo", "and", "foo" }
*o
*5
*{ "b", "", ":and:f", "", "" }
*o
*-2
*{ "b", "", ":and:f", "", "" }
*o
*0
*{ "b", "", ":and:f" }
*
An invocation of this method of the form
* str.split(regex, n)
* yields the same result as the expression
*
*
@link java.util.regex.Pattern}.{@link java.util.regex.Pattern#compile
* {
This method works as if by invoking the two-argument {
@link The string "boo:and:foo", for example, yields the following
* results with these expressions:
*
*
*
*Regex
*Result
*
*:
*{ "boo", "and", "foo" }
*o
*{ "b", "", ":and:f" }
*
String
to lowerLocale
. Case mapping is basedString
may be a different length than the original String
.
* Examples of lowercase mappings are in the following table:
*
Language Code of Locale | Upper Case | Lower Case | Description |
---|---|---|---|
tr (Turkish) | \u0130 | \u0069 | capital letter I with dot above -> small letter i |
tr (Turkish) | \u0049 | \u0131 | capital letter I -> small letter dotless i |
(all) | French Fries | french fries | lowercased all chars in String |
(all) | * * | * * | lowercased all chars in String |
String
, converted to lowercase.