所在包:java.lang.String
所有已实现的接口:Serializable, CharSequence, Comparable<String>
1 public final class String 2 extends Object 3 implements Serializable, Comparable<String>, CharSequence
The
String
class represents character strings. All string literals in Java programs, such as "abc"
, are implemented as instances of this class.
String类表示字符串。Java程序中的所有字符串文本,如“abc”,都是作为该类的实例实现的。
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. String concatenation is implemented through the StringBuilder
(or StringBuffer
) class and its append
method.
Java语言为字符串连接运算符(+)和其他对象到字符串的转换提供了特殊的支持。字符串连接是通过StringBuilder(或StringBuffer)类及其append方法实现的。
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.
字符串转换是通过toString方法实现的,该方法由Object定义,并由Java中的所有类继承。有关字符串连接和转换的更多信息,请参见Gosling、Joy和Steele,即Java语言规范。
Unless otherwise noted, passing a null argument to a constructor or method in this class will cause a NullPointerException
to be thrown.
除非另有说明,否则将null参数传递给该类中的构造函数或方法将导致抛出NullPointerException。
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).
字符串表示UTF-16格式的字符串,其中补充字符由代理对表示(有关更多信息,请参阅字符类中的Unicode字符表示部分)。
Index values refer to char
code units, so a supplementary character uses two positions in a String
.
索引值指的是char代码单元,所以补充字符使用字符串中的两个位置。
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).
String类提供了处理Unicode代码点的方法(例如,除了处理Unicode编码单位(即char值)。
Since从以下版本开始:
- JDK1.0
- See Also另请参见::
-
Object.toString()
,StringBuffer
,StringBuilder
,Charset
, Serialized Form
Field Summary字段汇总
Modifier and Type 修饰符和类型 |
Field and Description 字段和描述 |
---|---|
static Comparator<String> |
CASE_INSENSITIVE_ORDER
A Comparator that orders String objects as by compareToIgnoreCase .
一个比较器,它通过compareToIgnoreCase来排序字符串对象。
|
Constructor Summary构造函数的总结
Constructor and 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.
此方法不能正确地将字节转换为字符。从JDK 1.1开始,实现此目的的首选方法是通过字符串构造函数,这些构造函数接受Charset、Charset名称或使用平台的默认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.
此方法不能正确地将字节转换为字符。从JDK 1.1开始,实现此目的的首选方法是通过字符串构造函数,这些构造函数接受Charset、Charset名称或使用平台的默认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.
分配一个新字符串,该字符串包含Unicode代码点数组参数子数组中的字符。
|
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方法总结
Modifier and Type 修饰符和类型 |
Method and Description 字段和描述 |
---|---|
char |
charAt(int index)
Returns the char value at the specified index.
返回指定索引处的char值。
|
int |
codePointAt(int index)
Returns the character (Unicode code point) at the specified index.
返回指定索引处的字符(Unicode代码点)。
|
int |
codePointBefore(int index)
Returns the character (Unicode code point) before the specified index.
返回指定索引前的字符(Unicode编码点)。
|
int |
codePointCount(int beginIndex, int endIndex)
Returns the number of Unicode code points in the specified text range of this String .
返回此字符串的指定文本范围内的Unicode代码点的数量。
|
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.
当且仅当此字符串包含指定的char值序列时,返回true。
|
boolean |
contentEquals(CharSequence cs)
Compares this string to the specified CharSequence .
将此字符串与指定的CharSequence进行比较。
|
boolean |
contentEquals(StringBuffer sb)
Compares this string to the specified StringBuffer .
将此字符串与指定的StringBuffer进行比较。
|
static String |
copyValueOf(char[] data)
Equivalent to valueOf(char[]) .
相当于返回对象的值(char [])。
|
static String |
copyValueOf(char[] data, int offset, int count)
Equivalent to valueOf(char[], int, int) .
等价于valueOf(char[], int, int)。
|
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.
此方法不能正确地将字符转换为字节。从JDK 1.1开始,实现此目的的首选方法是通过getBytes()方法,该方法使用平台的默认字符集。
|
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 .
当且仅当length()为0时返回true。
|
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 .
返回一个由CharSequence元素的副本和指定分隔符的副本组成的新字符串。
|
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 .
返回一个由CharSequence元素的副本和指定分隔符的副本组成的新字符串。
|
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)
Tells whether or not this string matches the given regular expression.
指示此字符串是否与给定的正则表达式匹配。
|
int |
offsetByCodePoints(int index, int codePointOffset)
Returns the index within this String that is offset from the given index by codePointOffset code points.
返回该字符串中的索引,该索引由codePointOffset代码点从给定索引偏移。
|
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 .
返回一个字符串,该字符串是用newChar替换此字符串中出现的所有oldChar后得到的。
|
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)
Splits this string around matches of the given regular expression.
将这个字符串与给定正则表达式的匹配部分分开。
|
String[] |
split(String regex, int limit)
Splits this string around matches of the given regular expression.
将这个字符串与给定正则表达式的匹配部分分开。
|
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.
返回char参数的字符串表示形式。
|
static String |
valueOf(char[] data)
Returns the string representation of the char array argument.
返回char数组参数的字符串表示形式。
|
static String |
valueOf(char[] data, int offset, int count)
Returns the string representation of a specific subarray of the char array argument.
返回char数组参数的特定子数组的字符串表示形式。
|
static String |
valueOf(double d)
Returns the string representation of the double argument.
返回double双精度浮点参数的字符串表示形式。
|
static String |
valueOf(float f)
Returns the string representation of the float argument.
返回float浮点参数的字符串表示形式。
|
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.
返回int整型参数的字符串表示形式。
|
static String |
valueOf(Object obj)
Returns the string representation of the Object argument.
返回Object对象参数的字符串表示形式。
|
Methods inherited from class java.lang.Object
方法继承自java.lang.Object类
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
Methods inherited from interface java.lang.CharSequence
方法继承自接口java.lang.CharSequence
Field Detail字段详细信息
CASE_INSENSITIVE_ORDER不分大小写的顺序
A Comparator that orderspublic static final Comparator<String> CASE_INSENSITIVE_ORDER
String
objects as by compareToIgnoreCase
. This comparator is serializable.
一个比较器,它通过compareToIgnoreCase来排序字符串对象。这个比较器是可序列化的。
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.
注意,这个比较器没有考虑语言环境,并且会导致对某些语言环境的不满意的排序。java。文本包提供了排序器,允许对区域设置敏感的排序。
- Since:
- 1.2
- See Also:
Collator.compare(String, String)