public final class String extends Object implements Serializable, Comparable<String>, CharSequence
String类代表字符串。
Java程序中的所有字符串文字(例如"abc" )都被实现为此类的实例。
字符串不变; 它们的值在创建后不能被更改。 字符串缓冲区支持可变字符串。 因为String对象是不可变的,它们可以被共享。 例如:
String str = "abc";
相当于:
char data[] = {'a', 'b', 'c'};
String str = new String(data);
以下是一些如何使用字符串的示例:
System.out.println("abc");
String cde = "cde";
System.out.println("abc" + cde);
String c = "abc".substring(2,3);
String d = cde.substring(1, 2);
String类包括用于检查序列的各个字符的方法,用于比较字符串,搜索字符串,提取子字符串以及创建将所有字符翻译为大写或小写的字符串的副本。 案例映射基于Character类指定的Unicode标准版本。
Java语言为字符串连接运算符(+)提供特殊支持,并为其他对象转换为字符串。 字符串连接是通过StringBuilder (或StringBuffer )类及其append方法实现的。 字符串转换是通过方法来实现toString ,由下式定义Object和继承由在Java中的所有类。 有关字符串连接和转换的其他信息,请参阅Gosling,Joy和Steele, Java语言规范 。
除非另有说明,否则传递null参数到此类中的构造函数或方法将导致抛出NullPointerException 。
A String表示UTF-16格式的字符串,其中补充字符由代理对表示 (有关详细信息,请参阅Character课程中的Character部分)。 索引值是指char代码单元,所以补充字符在String中使用两个String 。
String类提供处理Unicode代码点(即字符)的方法,以及用于处理Unicode代码单元(即char值)的方法。
Object.toString() , StringBuffer , StringBuilder , Charset , Serialized Form
| Modifier and Type | Field and Description |
|---|---|
static Comparator<String> |
CASE_INSENSITIVE_ORDER
一个比较器,按
compareToIgnoreCase订购
String对象。
|
| Constructor and Description |
|---|
String()
初始化新创建的
String对象,使其表示空字符序列。
|
String(byte[] bytes)
通过使用平台的默认字符集解码指定的字节数组来构造新的
String 。
|
String(byte[] bytes, Charset charset)
构造一个新的 String由指定用指定的字节的数组解码charset 。
|
String(byte[] ascii, int hibyte)
已弃用
此方法无法将字节正确转换为字符。
从JDK 1.1开始,首选的方法是通过
String构造函数获取Charset ,字符集名称,或者使用平台的默认字符集。
|
String(byte[] bytes, int offset, int length)
通过使用平台的默认字符集解码指定的字节子阵列来构造新的
String 。
|
String(byte[] bytes, int offset, int length, Charset charset)
构造一个新的 String通过使用指定的指定字节子阵列解码charset 。
|
String(byte[] ascii, int hibyte, int offset, int count)
已弃用
此方法无法将字节正确转换为字符。
从JDK 1.1开始,首选的方式是通过
String构造函数获取Charset ,字符集名称,或使用平台的默认字符集。
|
String(byte[] bytes, int offset, int length, String charsetName)
构造一个新的
String通过使用指定的字符集解码指定的字节子阵列。
|
String(byte[] bytes, String charsetName)
构造一个新的 String由指定用指定的字节的数组解码charset 。
|
String(char[] value)
分配一个新的
String ,以便它表示当前包含在字符数组参数中的字符序列。
|
String(char[] value, int offset, int count)
分配一个新的
String ,其中包含字符数组参数的子阵列中的字符。
|
String(int[] codePoints, int offset, int count)
|
String(String original)
初始化新创建的 String对象,使其表示与参数相同的字符序列;
换句话说,新创建的字符串是参数字符串的副本。
|
String(StringBuffer buffer)
分配一个新的字符串,其中包含当前包含在字符串缓冲区参数中的字符序列。
|
String(StringBuilder builder)
分配一个新的字符串,其中包含当前包含在字符串构建器参数中的字符序列。
|
| Modifier and Type | Method and Description |
|---|---|
char |
charAt(int index)
返回
char指定索引处的值。
|
int |
codePointAt(int index)
返回指定索引处的字符(Unicode代码点)。
|
int |
codePointBefore(int index)
返回指定索引之前的字符(Unicode代码点)。
|
int |
codePointCount(int beginIndex, int endIndex)
返回此
String指定文本范围内的Unicode代码点数。
|
int |
compareTo(String anotherString)
按字典顺序比较两个字符串。
|
int |
compareToIgnoreCase(String str)
按字典顺序比较两个字符串,忽略病例差异。
|
String |
concat(String str)
将指定的字符串连接到该字符串的末尾。
|
boolean |
contains(CharSequence s)
当且仅当此字符串包含指定的char值序列时才返回true。
|
boolean |
contentEquals(CharSequence cs)
将此字符串与指定的CharSequence进行
CharSequence 。
|
boolean |
contentEquals(StringBuffer sb)
将此字符串与指定的StringBuffer进行
StringBuffer 。
|
static String |
copyValueOf(char[] data)
相当于
valueOf(char[]) 。
|
static String |
copyValueOf(char[] data, int offset, int count)
|
boolean |
endsWith(String suffix)
测试此字符串是否以指定的后缀结尾。
|
boolean |
equals(Object anObject)
将此字符串与指定对象进行比较。
|
boolean |
equalsIgnoreCase(String anotherString)
将此
String与其他
String比较,忽略案例注意事项。
|
static String |
format(Locale l, String format, Object... args)
使用指定的区域设置,格式字符串和参数返回格式化的字符串。
|
static String |
format(String format, Object... args)
使用指定的格式字符串和参数返回格式化的字符串。
|
byte[] |
getBytes()
使用平台的默认字符集将此
String编码为字节序列,将结果存储到新的字节数组中。
|
byte[] |
getBytes(Charset charset)
使用给定的charset将该 String编码为字节序列,将结果存储到新的字节数组中。
|
void |
getBytes(int srcBegin, int srcEnd, byte[] dst, int dstBegin)
已弃用
此方法无法将字符正确转换为字节。
从JDK 1.1开始,首选的方法是通过
getBytes()方法,该方法使用平台的默认字符集。
|
byte[] |
getBytes(String charsetName)
使用命名的字符集将此
String编码为字节序列,将结果存储到新的字节数组中。
|
void |
getChars(int srcBegin, int srcEnd, char[] dst, int dstBegin)
将此字符串中的字符复制到目标字符数组中。
|
int |
hashCode()
返回此字符串的哈希码。
|
int |
indexOf(int ch)
返回指定字符第一次出现的字符串内的索引。
|
int |
indexOf(int ch, int fromIndex)
返回指定字符第一次出现的字符串内的索引,以指定的索引开始搜索。
|
int |
indexOf(String str)
返回指定子字符串第一次出现的字符串内的索引。
|
int |
indexOf(String str, int fromIndex)
返回指定子串的第一次出现的字符串中的索引,从指定的索引开始。
|
String |
intern()
返回字符串对象的规范表示。
|
boolean |
isEmpty()
|
static String |
join(CharSequence delimiter, CharSequence... elements)
返回一个新的字符串,由
CharSequence elements的副本组成,并附有指定的delimiter的
delimiter 。
|
static String |
join(CharSequence delimiter, Iterable<? extends CharSequence> elements)
返回一个新
String的副本组成
CharSequence elements与指定的副本一起加入
delimiter 。
|
int |
lastIndexOf(int ch)
返回指定字符的最后一次出现的字符串中的索引。
|
int |
lastIndexOf(int ch, int fromIndex)
返回指定字符的最后一次出现的字符串中的索引,从指定的索引开始向后搜索。
|
int |
lastIndexOf(String str)
返回指定子字符串最后一次出现的字符串中的索引。
|
int |
lastIndexOf(String str, int fromIndex)
返回指定子字符串的最后一次出现的字符串中的索引,从指定索引开始向后搜索。
|
int |
length()
返回此字符串的长度。
|
boolean |
matches(String regex)
告诉这个字符串是否匹配给定的
regular expression 。
|
int |
offsetByCodePoints(int index, int codePointOffset)
返回此
String内的指数,与
index
codePointOffset代码点。
|
boolean |
regionMatches(boolean ignoreCase, int toffset, String other, int ooffset, int len)
测试两个字符串区域是否相等。
|
boolean |
regionMatches(int toffset, String other, int ooffset, int len)
测试两个字符串区域是否相等。
|
String |
replace(char oldChar, char newChar)
返回从替换所有出现的导致一个字符串
oldChar在此字符串
newChar 。
|
String |
replace(CharSequence target, CharSequence replacement)
将与字面目标序列匹配的字符串的每个子字符串替换为指定的字面替换序列。
|
String |
replaceAll(String regex, String replacement)
用给定的替换替换与给定的
regular expression匹配的此字符串的每个子字符串。
|
String |
replaceFirst(String regex, String replacement)
用给定的替换替换与给定的
regular expression匹配的此字符串的第一个子字符串。
|
String[] |
split(String regex)
将此字符串分割为给定的
regular expression的匹配。
|
String[] |
split(String regex, int limit)
将这个字符串拆分为给定的
regular expression的匹配。
|
boolean |
startsWith(String prefix)
测试此字符串是否以指定的前缀开头。
|
boolean |
startsWith(String prefix, int toffset)
测试在指定索引处开始的此字符串的子字符串是否以指定的前缀开头。
|
CharSequence |
subSequence(int beginIndex, int endIndex)
返回一个字符序列,该序列是该序列的子序列。
|
String |
substring(int beginIndex)
返回一个字符串,该字符串是此字符串的子字符串。
|
String |
substring(int beginIndex, int endIndex)
返回一个字符串,该字符串是此字符串的子字符串。
|
char[] |
toCharArray()
将此字符串转换为新的字符数组。
|
String |
toLowerCase()
将所有在此字符
String使用默认语言环境的规则,以小写。
|
String |
toLowerCase(Locale locale)
将所有在此字符
String ,以降低使用给定的规则情况下
Locale 。
|
String |
toString()
此对象(已经是字符串!)本身已被返回。
|
String |
toUpperCase()
将所有在此字符
String使用默认语言环境的规则大写。
|
String |
toUpperCase(Locale locale)
将所有在此字符
String使用给定的规则,大写
Locale 。
|
String |
trim()
返回一个字符串,其值为此字符串,并删除任何前导和尾随空格。
|
static String |
valueOf(boolean b)
返回
boolean参数的字符串
boolean形式。
|
static String |
valueOf(char c)
返回
char参数的字符串
char形式。
|
static String |
valueOf(char[] data)
返回
char数组参数的字符串
char形式。
|
static String |
valueOf(char[] data, int offset, int count)
返回
char数组参数的特定子阵列的字符串
char形式。
|
static String |
valueOf(double d)
返回
double参数的字符串
double形式。
|
static String |
valueOf(float f)
返回
float参数的字符串
float形式。
|
static String |
valueOf(int i)
返回
int参数的字符串
int形式。
|
static String |
valueOf(long l)
返回
long参数的字符串
long形式。
|
static String |
valueOf(Object obj)
返回
Object参数的字符串
Object形式。
|
clone, finalize, getClass, notify, notifyAll, wait, wait, waitchars, codePointspublic static final Comparator<String> CASE_INSENSITIVE_ORDER
compareToIgnoreCase命令String对象。
该比较器可串行化。
请注意,此比较器不会考虑区域设置,并将导致对某些区域设置的排序不满意。 java.text包提供了Collators来允许区域设置敏感的排序。
Collator.compare(String, String)
public String()
String对象,以使其表示空字符序列。
请注意,使用此构造函数是不必要的,因为Strings是不可变的。
public String(String original)
String对象,使其表示与参数相同的字符序列;
换句话说,新创建的字符串是参数字符串的副本。
除非需要original的显式副本, original使用此构造函数是不必要的,因为Strings是不可变的。
original - A
String
public String(char[] value)
String ,以便它表示当前包含在字符数组参数中的字符序列。
字符数组的内容被复制;
字符数组的后续修改不会影响新创建的字符串。
value - 字符串的初始值
public String(char[] value,
int offset,
int count)
String ,其中包含字符数组参数的子阵列中的字符。
offset参数是子阵列的第一个字符的索引, count参数指定子阵列的长度。
副本的内容被复制;
字符数组的后续修改不会影响新创建的字符串。
value - 作为字符源的数组
offset - 初始偏移量
count - 长度
IndexOutOfBoundsException - 如果
offset和
count参数的索引字符超出了
value数组的界限
public String(int[] codePoints,
int offset,
int count)
String ,其中包含Unicode code point数组参数的子阵列中的字符 。
offset参数是子阵列的第一个代码点的索引, count参数指定子阵列的长度。
子阵列的内容转换为char s;
int数组的int不影响新创建的字符串。
codePoints - 作为Unicode代码点的源的数组
offset - 初始偏移量
count - 长度
IllegalArgumentException - 如果在
codePoints中找到任何无效的Unicode代码点
IndexOutOfBoundsException - 如果
offset和
count参数的索引字符在
codePoints数组的边界之外
@Deprecated public String(byte[] ascii, int hibyte, int offset, int count)
String 。
offset参数是子阵列的第一个字节的索引, count参数指定子阵列的长度。
子阵列中的每个byte char为char,如上述方法中所述。
ascii - 要转换为字符的字节
hibyte - 每个16位Unicode代码单元的前8位
offset - 初始偏移量
count - 长度
IndexOutOfBoundsException - 如果
offset或
count参数无效
String(byte[], int) ,
String(byte[], int, int, java.lang.String) ,
String(byte[], int, int, java.nio.charset.Charset) ,
String(byte[], int, int) ,
String(byte[], java.lang.String) ,
String(byte[], java.nio.charset.Charset) ,
String(byte[])
@Deprecated public String(byte[] ascii, int hibyte)
String ,其中包含由8位整数值数组构成的字符。
结果字符串中的每个字符c由字节数组中的相应组件b构成,使得:
c == (char)(((hibyte & 0xff) << 8)
| (b & 0xff))
ascii - 要转换为字符的字节
hibyte - 每个16位Unicode代码单元的前8位
String(byte[], int, int, java.lang.String) ,
String(byte[], int, int, java.nio.charset.Charset) ,
String(byte[], int, int) ,
String(byte[], java.lang.String) ,
String(byte[], java.nio.charset.Charset) ,
String(byte[])
public String(byte[] bytes,
int offset,
int length,
String charsetName)
throws UnsupportedEncodingException
String通过使用指定的字符集解码指定的字节子阵列。
新的String的长度是字符集的函数,因此可能不等于子阵列的长度。
给定字节在给定字符集中无效时,此构造函数的行为是未指定的。 当需要更多的解码过程控制时,应使用CharsetDecoder类。
bytes - 要解码为字符的字节
offset - 要解码的第一个字节的索引
length - 要解码的字节数
charsetName - 支持的名称charset
UnsupportedEncodingException - 如果不支持命名的字符集
IndexOutOfBoundsException - 如果
offset和
length参数的索引字符在
bytes数组的边界之外
public String(byte[] bytes,
int offset,
int length,
Charset charset)
String通过使用指定的指定字节子阵列解码charset 。
新的String的长度是字符集的函数,因此可能不等于子数组的长度。
此方法总是用此字符集的默认替换字符串替换格式错误的输入和不可映射字符序列。 当需要更多的解码过程控制时,应使用CharsetDecoder类。
bytes - 要解码为字符的字节
offset - 要解码的第一个字节的索引
length - 要解码的字节数
charset -该charset要使用的解码bytes
IndexOutOfBoundsException - 如果
offset和
length参数在
bytes数组的范围之外的索引字符
public String(byte[] bytes,
String charsetName)
throws UnsupportedEncodingException
String由指定用指定的字节的数组解码charset 。
新的String的长度是字符集的函数,因此可能不等于字节数组的长度。
给定字节在给定字符集中无效时,此构造函数的行为是未指定的。 当需要更多的解码过程控制时,应使用CharsetDecoder类。
bytes - 要解码为字符的字节
charsetName - 支持的名称charset
UnsupportedEncodingException - 如果不支持命名的字符集
public String(byte[] bytes,
Charset charset)
String由指定用指定的字节的数组解码charset 。
新的String的长度是字符集的函数,因此可能不等于字节数组的长度。
此方法总是用此字符集的默认替换字符串替换格式错误的输入和不可映射字符序列。 当需要更多的解码过程控制时,应使用CharsetDecoder类。
bytes - 要解码为字符的字节
charset -该charset要使用的解码bytes
public String(byte[] bytes,
int offset,
int length)
String 。
新的String的长度是字符集的函数,因此可能不等于子数组的长度。
指定字节在默认字符集中无效时,此构造函数的行为是未指定的。 当需要更多的解码过程控制时,应使用CharsetDecoder类。
bytes - 要解码为字符的字节
offset - 要解码的第一个字节的索引
length - 要解码的字节数
IndexOutOfBoundsException - 如果
offset和
length参数的索引字符在
bytes数组的边界之外
public String(byte[] bytes)
String 。
新的String的长度是字符集的函数,因此可能不等于字节数组的长度。
指定字节在默认字符集中无效时,此构造函数的行为是未指定的。 当需要对解码过程进行更多的控制时,应使用CharsetDecoder类。
bytes - 要解码为字符的字节
public String(StringBuffer buffer)
buffer - A
StringBuffer
public String(StringBuilder builder)
提供此构造函数以便迁移到StringBuilder 。 通过toString方法从字符串构建器获取字符串可能运行速度更快,通常是首选。
builder - A
StringBuilder
public int length()
length在界面
CharSequence
public boolean isEmpty()
true如果
length()是
0 ,否则
false
public char charAt(int index)
char指定索引处的值。
指数范围为0至length() - 1 。
该序列的第一个char值在索引0 ,下一个索引为1 ,依此类推,与数组索引一样。
如果char由索引指定的值是surrogate ,则返回所述替代值。
charAt在界面
CharSequence
index -
char值的指数。
char此字符串的指定索引处的值。
第一个char值是索引0 。
IndexOutOfBoundsException - 如果
index参数为负数或不小于此字符串的长度。
public int codePointAt(int index)
char值(Unicode码单位),范围从0到length() - 1 。
如果在给定索引处指定的char值处于高代理范围内,则以下指数小char值String的长度,并且下列指数的char值处于低代理范围,则补码代码相应的这个代理对被退回。 否则,返回给定索引处的char值。
index - 指数为
char值
index
IndexOutOfBoundsException - 如果
index参数为负数或不小于此字符串的长度。
public int codePointBefore(int index)
char值(Unicode代码单位),范围从1到length 。
如果char的(index - 1)为(index - 1), (index - 2)为char为(index - 2)为char为(index - 2) ,为高代数范围,则返回替代对的补码。 如果char在值index - 1是未配对的低代理或一个高代理,则返回所述替代值。
index - 应该返回的代码点之后的索引
IndexOutOfBoundsException - 如果
index参数小于1或大于此字符串的长度。
public int codePointCount(int beginIndex,
int endIndex)
String指定文本范围内的Unicode代码点数。
文本范围始于指定beginIndex并延伸到char在索引endIndex - 1 。
因此,文本范围的长度(在char秒)为endIndex-beginIndex 。
文本范围内的非配对替代品,每一个代码点都是一样的。
beginIndex - 索引到第一个
char的文本范围。
endIndex - 最后一个
char之后的索引。
IndexOutOfBoundsException - 如果
beginIndex为负数,或
endIndex大于该
String的长度,或
beginIndex大于
endIndex 。
public int offsetByCodePoints(int index,
int codePointOffset)
String内的指数,该指数与index ( codePointOffset代码点相抵消。
由index和codePointOffset给出的文本范围内的非配对替代codePointOffset作为一个代码点。
index - 要偏移的索引
codePointOffset - 代码点的偏移量
String
IndexOutOfBoundsException -如果
index接着这个长度负或更大
String ,或如果
codePointOffset为正,并且开始与该子串
index有少于
codePointOffset代码点,或者如果
codePointOffset为负且前的子字符串
index具有比的绝对值较少的
codePointOffset代码点。
public void getChars(int srcBegin,
int srcEnd,
char[] dst,
int dstBegin)
要复制的第一个字符是索引srcBegin ; 要复制的最后一个字符在索引srcEnd-1 (因此要复制的srcEnd-srcBegin总数为srcEnd-srcBegin )。 字符被复制到的子阵列dst开始于索引dstBegin和在索引结束:
dstbegin + (srcEnd-srcBegin) - 1
srcBegin - 要复制的字符串中第一个字符的索引。
srcEnd - 要复制的字符串中最后一个字符后面的索引。
dst - 目标数组。
dstBegin - 目标数组中的起始偏移量。
IndexOutOfBoundsException - 如果满足以下条件:
srcBegin为负数。 srcBegin大于srcEnd srcEnd大于此字符串的长度 dstBegin为负数 dstBegin+(srcEnd-srcBegin)大于dst.length @Deprecated public void getBytes(int srcBegin, int srcEnd, byte[] dst, int dstBegin)
要复制的第一个字符是索引srcBegin ; 要复制的最后一个字符在索引srcEnd-1 。 要复制的srcEnd-srcBegin总数为srcEnd-srcBegin 。 转换为字节的字符被复制到dst的子阵列中,从索引dstBegin开始,以索引结尾:
dstbegin + (srcEnd-srcBegin) - 1
srcBegin - 要复制的字符串中第一个字符的索引
srcEnd - 要复制的字符串中最后一个字符后的索引
dst - 目的数组
dstBegin - 目标数组中的起始偏移量
IndexOutOfBoundsException - 如果满足以下条件:
srcBegin为负数 srcBegin大于srcEnd srcEnd大于此String的长度 dstBegin为负数 dstBegin+(srcEnd-srcBegin)大于dst.length public byte[] getBytes(String charsetName) throws UnsupportedEncodingException
String编码为字节序列,将结果存储到新的字节数组中。
此字符串不能在给定字符集中编码时,此方法的行为是未指定的。 当需要对编码过程进行更多控制时,应使用CharsetEncoder类。
charsetName - 支持的名称charset
UnsupportedEncodingException - 如果不支持命名的字符集
public byte[] getBytes(Charset charset)
String编码为一个字节序列,将结果存储到新的字节数组中。
此方法总是用此字符集的默认替换字节数组替换格式错误的输入和不可映射字符序列。 当需要对编码过程的更多控制时,应使用CharsetEncoder类。
charset -该Charset被用于编码String
public byte[] getBytes()
String编码为字节序列,将结果存储到新的字节数组中。
当该字符串不能在默认字符集中编码时,此方法的行为是未指定的。 当需要对编码过程进行更多控制时,应使用CharsetEncoder类。
public boolean equals(Object anObject)
true当且仅当该参数不是null并且是String对象,表示相同的字符序列作为该对象。
equals在
Object
anObject - 对比这个
String的对象
true如果给定的对象代表一个
String等效于这个字符串,
false否则
compareTo(String) ,
equalsIgnoreCase(String)
public boolean contentEquals(StringBuffer sb)
StringBuffer 。
其结果是true如果并且如果该只String表示的字符作为指定的相同序列StringBuffer 。
该方法在StringBuffer上StringBuffer 。
sb -
StringBuffer将此
String对比
true如果此
String表示相同的字符序列作为指定
StringBuffer ,
false否则
public boolean contentEquals(CharSequence cs)
CharSequence 。
其结果是true如果并且如果该只String表示char值的相同序列与指定序列。
请注意,如果CharSequence是StringBuffer那么该方法将同步它。
cs - 比较这个
String的序列
true如果这个
String表示与指定序列相同的char值序列,
false否则
public boolean equalsIgnoreCase(String anotherString)
String与其他String比较,忽略案例注意事项。
如果两个字符串的长度相同,并且两个字符串中的相应字符等于忽略大小写,则两个字符串被认为是相等的。
如果以下至少一个为真,则两个字符c1和c2被认为是相同的忽略情况:
==操作符相比) Character.toUpperCase(char)应用于每个字符产生相同的结果 Character.toLowerCase(char)应用于每个字符产生相同的结果 anotherString -
String将此
String对比
true如果参数不是null ,它代表等效的String忽略大小写;
false否则
equals(Object)
public int compareTo(String anotherString)
String对象表示的字符序列按字典顺序与由参数字符串表示的字符序列进行比较。
如果String对象按字典顺序排列在参数字符串之前,结果为负整数。
结果是一个正整数,如果String对象按字典顺序跟随参数字符串。
如果字符串相等,结果为零;
compareTo返回0 ,当equals(Object)方法将返回true 。
这是字典排序的定义。 如果两个字符串不同,则它们在某些索引处具有不同的字符,这两个字符串是两个字符串的有效索引,或者它们的长度不同,或两者都是不同的。 如果它们在一个或多个索引位置具有不同的字符,则令k为最小的索引; 那么在位置k处的字符具有较小值的字符串,如通过使用<运算符确定的,以字典顺序位于另一个字符串之前。 在这种情况下, compareTo返回两个字符串中位置k处的两个字符值的差值,即值:
如果没有它们不同的索引位置,则较短的字符串按字典顺序位于较长的字符串之前。 在这种情况下,this.charAt(k)-anotherString.charAt(k)
compareTo返回字符串长度的差异 - 即值:
this.length()-anotherString.length()
compareTo在界面
Comparable<String>
anotherString - 要比较的
String 。
0 ;
一个值小于0如果这个字符串的字典比字符串参数小;
如果此字符串的字典大小超过字符串参数,则值大于0 。
public int compareToIgnoreCase(String str)
compareTo与在不同的情况下已经被淘汰,通过调用字符串的规范化版本Character.toLowerCase(Character.toUpperCase(character))上的每一个字符。
请注意,此方法不场所考虑,并会导致特定的语言环境不满意的排序。 java.text包提供了整理器来允许区域设置敏感的排序。
str - 要比较的
String 。
Collator.compare(String, String)
public boolean regionMatches(int toffset,
String other,
int ooffset,
int len)
这个String对象的子字符串与其他参数的子字符串进行比较。 如果这些子串表示相同的字符序列,结果是真的。 要比较的String对象的子String从索引toffset开始,长度为len 。 其他要比较的ooffset始于索引ooffset ,长度为len 。 结果是false当且仅当以下至少有一个是真的:
toffset为负数。 ooffset是否定的。 toffset+len大于此String对象的长度。 ooffset+len大于另一个参数的长度。 len ,使得: this.charAt(toffset + k ) != other.charAt(ooffset + k ) toffset - 该字符串
toffset的起始偏移量。
other - 字符串参数。
ooffset - 字符串参数
ooffset的起始偏移量。
len - 要比较的字符数。
true如果此字符串的指定子区域与字符串参数的指定子区域完全匹配;
false否则。
public boolean regionMatches(boolean ignoreCase,
int toffset,
String other,
int ooffset,
int len)
此String对象的子字符串与参数other 。 如果这些子字符串表示相同的字符序列,则结果为true ,如果且仅当ignoreCase为真,忽略大小写。 此String对象的子String将从索引toffset开始,长度为len 。 要比较的other的other始于索引ooffset ,长度为len 。 结果是false当且仅当至少有以下一个是真的:
toffset为负数。 ooffset为负数。 toffset+len大于此String对象的长度。 ooffset+len大于另一个参数的长度。 ignoreCase是false ,并且有一些非负整数k小于len ,使得:
this.charAt(toffset+k) != other.charAt(ooffset+k)
ignoreCase是true ,有一些非负整数k小于len ,使得:
Character.toLowerCase(this.charAt(toffset+k)) !=
Character.toLowerCase(other.charAt(ooffset+k))
和:
Character.toUpperCase(this.charAt(toffset+k)) !=
Character.toUpperCase(other.charAt(ooffset+k))
ignoreCase - 如果
true ,
true时忽略大小写。
toffset - 该字符串
toffset的起始偏移量。
other - 字符串参数。
ooffset - 字符串参数
ooffset的起始偏移量。
len - 要比较的字符数。
true如果此字符串的指定子区域匹配字符串参数的指定子区域;
false否则。
匹配是否精确或不区分大小写取决于ignoreCase参数。
public boolean startsWith(String prefix, int toffset)
prefix - 前缀。
toffset - 在哪里开始查找这个字符串。
true如果由参数表示的字符序列是从索引toffset开始的此对象的子字符串的前缀;
false否则。
如果toffset为负数或大于此String对象的长度,则结果为false ;
否则结果与表达式的结果相同
this.substring(toffset).startsWith(prefix)
public boolean startsWith(String prefix)
prefix - 前缀。
true如果由参数表示的字符序列是由该字符串表示的字符序列的前缀;
false否则。
还需要注意的是true如果参数为空字符串或等于该将被返回String如由所确定的对象equals(Object)方法。
public boolean endsWith(String suffix)
suffix - 后缀。
true如果由参数表示的字符序列是由该对象表示的字符序列的后缀;
false否则。
注意,结果将是true如果参数是空字符串或等于该String如由所确定的对象equals(Object)方法。
public int hashCode()
String对象的哈希代码计算为
使用s[0]*31^(n-1) + s[1]*31^(n-2) + ... + s[n-1]
int算术,其中s[i]是字符串的第i个字符, n是字符串的长度, ^表示取幂。
(空字符串的哈希值为零)
hashCode在类别
Object
Object.equals(java.lang.Object) ,
System.identityHashCode(java.lang.Object)
public int indexOf(int ch)
ch在此表示的字符序列发生String第一事件发生之对象,则索引(在Unicode代码单元)被返回。
对于从0到0xFFFF(含)范围内的ch的值,这是最小值k ,使得:
是真的。 对于this.charAt(k) == ch
ch其他值,它是最小值k ,使得:
是真的。 在这两种情况下,如果此字符串中没有此类字符,则返回this.codePointAt(k) == ch
-1 。
ch - 一个字符(Unicode代码点)。
-1如果字符不会发生。
public int indexOf(int ch,
int fromIndex)
如果与值的字符ch在此表示的字符序列发生String的索引不小于在对象fromIndex ,则返回第一个这样的匹配项的索引。 对于从0到0xFFFF(含)范围内的ch的值,这是最小值k ,使得:
是真的。 对于(this.charAt(k) == ch)&&(k >= fromIndex)
ch其他值,它是最小值k ,使得:
是真的。 在这两种情况下,如果这个字符串在位置(this.codePointAt(k) == ch)&&(k >= fromIndex)
fromIndex之间或之后没有出现,则返回-1 。
fromIndex的价值没有fromIndex 。 如果它是否定的,它具有与零一样的效果:可以搜索整个字符串。 如果它大于此字符串的长度,它具有与该字符串的长度相等的效果: -1被返回。
所有索引在char值(Unicode代码单位)中char 。
ch - 一个字符(Unicode代码点)。
fromIndex - 开始搜索的索引。
fromIndex ,或
-1如果不发生的字符。
public int lastIndexOf(int ch)
ch的值, ch的索引(以Unicode代码为单位)是最大的值k ,使得:
是真的。 对于this.charAt(k) == ch
ch其他值,它是最大值k ,使得:
是真的。 在任何一种情况下,如果此字符串中没有此类字符,则返回this.codePointAt(k) == ch
-1 。
String从最后一个字符开始String搜索。
ch - 一个字符(Unicode代码点)。
-1如果字符不会发生。
public int lastIndexOf(int ch,
int fromIndex)
ch的值,返回的索引是最大值k ,使得:
是真的。 对于(this.charAt(k) == ch)&&(k <= fromIndex)
ch其他值,它是最大的值k ,使得:
是真的。 在这两种情况下,如果此字符串在位置(this.codePointAt(k) == ch)&&(k <= fromIndex)
fromIndex之前或之后没有出现这样的字符,则返回-1 。
所有索引在char值(Unicode代码单位)中char 。
ch - 一个字符(Unicode代码点)。
fromIndex - 开始搜索的索引。
对fromIndex的价值没有fromIndex 。
如果它大于或等于该字符串的长度,它具有与等于该字符串长度小于1的效果相同的效果:可以搜索整个字符串。
如果它是负的,它具有与-1:-1相同的效果。
fromIndex ,或
-1如果该字符不该点之前发生。
public int indexOf(String str)
返回的索引是其中的最小值k :
如果k的值不存在,则返回this.startsWith(str, k)
-1 。
str - 要搜索的子字符串。
-1如果不存在这样的发生。
public int indexOf(String str, int fromIndex)
返回的索引是其中的最小值k :
如果不存在这样的k值,则返回k >= fromIndex&&this.startsWith(str, k)
-1 。
str - 要搜索的子字符串。
fromIndex - 从
fromIndex开始搜索的索引。
-1如果不存在这样的发生。
public int lastIndexOf(String str)
this.length() 。
返回的索引是其中的最大值k :
如果没有k的值存在,则返回this.startsWith(str, k)
-1 。
str - 要搜索的子字符串。
-1如果不存在这样的发生。
public int lastIndexOf(String str, int fromIndex)
返回的索引是其中的最大值k :
如果k的值不存在,则返回k<=fromIndex&&this.startsWith(str, k)
-1 。
str - 要搜索的子字符串。
fromIndex - 开始搜索的索引。
-1如果不存在这样的发生。
public String substring(int beginIndex)
例子:
"unhappy".substring(2) returns "happy" "Harbison".substring(3) returns "bison" "emptiness".substring(9) returns "" (an empty string)
beginIndex - 开始索引(含)。
IndexOutOfBoundsException - 如果
beginIndex为负或大于此
String对象的长度。
public String substring(int beginIndex, int endIndex)
beginIndex并延伸到字符索引endIndex - 1 。
因此,子串的长度为endIndex-beginIndex 。
例子:
"hamburger".substring(4, 8) returns "urge" "smiles".substring(1, 5) returns "mile"
beginIndex - 开始索引,包括。
endIndex - 结束索引,独家。
IndexOutOfBoundsException - 如果
beginIndex为负数,或
endIndex大于该
String对象的长度,或
beginIndex大于
endIndex 。
public CharSequence subSequence(int beginIndex, int endIndex)
调用此方法的形式
行为与调用完全相同str.subSequence(begin, end)
str.substring(begin, end)
subSequence在界面
CharSequence
String类可以实现CharSequence接口。
beginIndex - 开始索引,包括。
endIndex - 结束索引,排他。
IndexOutOfBoundsException - 如果
beginIndex或
endIndex为负数,如果
endIndex大于
length() ,或者如果
beginIndex大于
endIndex
public String concat(String str)
如果参数字符串的长度为0 ,则返回此String对象。 否则,返回一个String对象,表示一个字符序列,该字符序列是由该String对象表示的字符序列与由参数字符串表示的字符序列的级联。
例子:
"cares".concat("s") returns "caress"
"to".concat("get").concat("her") returns "together"
str -
String被连接到这个
String 。
public String replace(char oldChar, char newChar)
oldChar在此字符串newChar 。
如果在String oldChar表示的字符序列中没有发生String ,则返回对该String对象的引用。 否则, String被返回对象,它表示一个字符序列与由本表示的字符序列String除了的每次出现对象, oldChar通过的发生替换newChar 。
例子:
"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 - 老字。
newChar - 新角色。
oldChar与
newChar 。
public boolean matches(String regex)
这种形式为str .matches( regex )方法的)产生与表达式完全相同的结果
Pattern.matches(regex, str)
regex - 要匹配此字符串的正则表达式
true如果,并且只有这个字符串与给定的正则表达式匹配
PatternSyntaxException - 如果正则表达式的语法无效
Pattern
public boolean contains(CharSequence s)
s - 搜索的顺序
s ,则为true,否则为false
public String replaceFirst(String regex, String replacement)
这种形式为str .replaceFirst( regex , repl )方法的)产生与表达式完全相同的结果
Pattern.compile(regex).matcher(str).replaceFirst(repl)
请注意,替换字符串中的反斜杠( \ )和美元符号( $ )可能会导致结果与被视为文字替换字符串时的结果不同; 见Matcher.replaceFirst(java.lang.String) 。 如果需要,使用Matcher.quoteReplacement(java.lang.String)来抑制这些字符的特殊含义。
regex - 要匹配此字符串的正则表达式
replacement - 要替换第一个匹配的字符串
String
PatternSyntaxException - 如果正则表达式的语法无效
Pattern
public String replaceAll(String regex, String replacement)
这种形式为str .replaceAll( regex , repl )方法的)产生与表达式完全相同的结果
Pattern.compile(regex).matcher(str).replaceAll(repl)
请注意,替换字符串中的反斜杠( \ )和美元符号( $ )可能会导致结果与被视为文字替换字符串时的结果不同; 见Matcher.replaceAll 。 如果需要,使用Matcher.quoteReplacement(java.lang.String)来抑制这些字符的特殊含义。
regex - 要匹配此字符串的正则表达式
replacement - 要替换每个匹配的字符串
String
PatternSyntaxException - 如果正则表达式的语法无效
Pattern
public String replace(CharSequence target, CharSequence replacement)
target - 要替换的char值序列
replacement - char值的替换顺序
public String[] split(String regex, int limit)
此方法返回的数组包含此字符串的每个子字符串,该字符串由与给定表达式匹配的另一个子字符串终止,或由字符串结尾终止。 数组中的子字符串按照它们在此字符串中的顺序排列。 如果表达式与输入的任何部分不匹配,则生成的数组只有一个元素,即这个字符串。
当在此字符串的开始处存在正宽度匹配时,在结果数组的开始处包含空的前导子字符串。 开始时的零宽度匹配不会产生这样的空的前导子串。
limit参数控制应用模式的次数,因此影响生成的数组的长度。 如果极限n大于0,则模式最多应用n -1次,数组的长度不大于n ,数组的最后一个条目将包含超出最后一个匹配分隔符的所有输入。 如果n是非正的,那么模式将被应用到尽可能多的次数,并且数组可以有任何长度。 如果n为0,则模式将被应用尽可能多次,数组可以有任何长度,并且尾随的空字符串将被丢弃。
例如,字符串"boo:and:foo"使用以下参数产生以下结果:
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" }
调用此方法的形式str。 split( 正则表达式 , n )产生与表达式相同的结果
Pattern.compile(regex).split(str, n)
regex - 分隔正则表达式
limit - 结果阈值,如上所述
PatternSyntaxException - 如果正则表达式的语法无效
Pattern
public String[] split(String regex)
该方法的工作原理是通过使用给定表达式和限制参数为零调用双参数split方法。 因此,尾随的空字符串不会包含在结果数组中。
例如,字符串"boo:and:foo"使用以下表达式得到以下结果:
Regex Result : { "boo", "and", "foo" }o { "b", "", ":and:f" }
regex - 分隔正则表达式
PatternSyntaxException - 如果正则表达式的语法无效
Pattern
public static String join(CharSequence delimiter, CharSequence... elements)
CharSequence elements的副本组成,并附有指定的delimiter的delimiter 。
For example,请注意,如果元素为空,则添加String message = String.join("-", "Java", "is", "cool"); // message returned is: "Java-is-cool"
"null" 。
delimiter - 分隔每个元素的分隔符
elements - 要加入的元素。
String ,其由所述的
elements由分离
delimiter
NullPointerException - 如果
delimiter或
elements是
null
StringJoiner
public static String join(CharSequence delimiter, Iterable<? extends CharSequence> elements)
String的副本组成CharSequence elements与指定的副本一起加入delimiter 。
For example,请注意,如果单个元素为List<String> strings = new LinkedList<>(); strings.add("Java");strings.add("is"); strings.add("cool"); String message = String.join(" ", strings); //message returned is: "Java is cool" Set<String> strings = new LinkedHashSet<>(); strings.add("Java"); strings.add("is"); strings.add("very"); strings.add("cool"); String message = String.join("-", strings); //message returned is: "Java-is-very-cool"
null ,则添加"null" 。
delimiter - 用于分离
elements中的每一个的
elements序列
String
elements - 一个
Iterable ,将其
elements连接在一起。
String由
elements参数组成
NullPointerException - 如果
delimiter或
elements是
null
join(CharSequence,CharSequence...) , StringJoiner
public String toLowerCase(Locale locale)
String ,以降低使用给定的规则情况下Locale 。
案例映射基于Character类指定的Unicode标准版本。
由于情况的映射并不总是1:1字符映射,将所得String可以是不同的长度比原来的String 。
小写映射示例如下表所示:
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
locale - 使用此区域设置的案例转换规则
String ,转换成小写。
toLowerCase() ,
toUpperCase() ,
toUpperCase(Locale)
public String toLowerCase()
String使用默认语言环境的规则,以小写。
这相当于打电话toLowerCase(Locale.getDefault()) 。
注意:此方法是区域设置敏感的,如果用于单独解释区域设置的字符串,可能会产生意外的结果。 示例是编程语言标识符,协议密钥和HTML标签。 例如, "TITLE".toLowerCase()在土耳其语言环境返回"t\u0131tle" ,其中“\ u0131”是拉丁小写字母无点我的性格。 要获取区域设置不敏感字符串的正确结果,请使用toLowerCase(Locale.ROOT) 。
String ,转换成小写。
toLowerCase(Locale)
public String toUpperCase(Locale locale)
String使用给定的规则,大写Locale 。
案例映射基于Character类指定的Unicode标准版本。
由于案例映射并不总是1:1的字符映射,所以String可能与原始的String 。
区域设置敏感和1:M情况映射的示例如下表所示。
Language Code of Locale Lower Case Upper Case Description tr (Turkish) \u0069 \u0130 small letter i -> capital letter I with dot above tr (Turkish) \u0131 \u0049 small letter dotless i -> capital letter I (all) \u00df \u0053 \u0053 small letter sharp s -> two letters: SS (all) Fahrvergnügen FAHRVERGNÜGENlocale - 使用此区域设置的案例转换规则
String ,转换成大写。
toUpperCase() ,
toLowerCase() ,
toLowerCase(Locale)
public String toUpperCase()
String中的所有字符转换为大写。
此方法相当于toUpperCase(Locale.getDefault()) 。
注意:此方法是区域设置敏感的,如果用于单独解释区域设置的字符串,可能会产生意外的结果。 示例是编程语言标识符,协议密钥和HTML标签。 例如, "title".toUpperCase()在土耳其语言环境返回"T\u0130TLE" ,其中“\ u0130”是拉丁文大写字母我带点上述特征。 要获得不区分大小写字符串的正确结果,请使用toUpperCase(Locale.ROOT) 。
String ,转换为大写。
toUpperCase(Locale)
public String trim()
如果此String对象表示一个空字符序列,或由该代表字符序列的第一个和最后一个字符String对象都具有代码大于'\u0020' (空格字符),则此参考String被返回对象。
否则,如果字符串中没有字符大于'\u0020'的字符,则返回一个表示空字符串的String对象。
否则,令k为代码大于'\u0020'的字符串中第一个字符的'\u0020' ,并且m为代码大于'\u0020'的字符串中最后一个字符的'\u0020' 。 将返回一个String对象,表示该字符串的子字符串,以索引k处的字符开头,以索引m为止的字符结束,结果为this.substring(k, m + 1) 。
此方法可用于从字符串的开始和结尾修剪空格(如上定义)。
public String toString()
toString在界面
CharSequence
toString在类别
Object
public char[] toCharArray()
public static String format(String format, Object... args)
总是使用的区域是Locale.getDefault()返回的地方 。
format - A
format string
args - 格式字符串中格式说明符引用的参数。
如果比格式说明符更多的参数,额外的参数将被忽略。
参数的数量是可变的,可能为零。
参数的最大数量受限于The Java™ Virtual Machine Specification定义的Java数组的最大维数 。
一个null参数的行为取决于conversion 。
IllegalFormatException - 如果格式字符串包含非法语法,则与给定参数不兼容的格式说明符,给定格式字符串的参数不足或其他非法条件。
有关所有可能的格式化错误的规范 ,请参阅格式化程序类规范的Details部分。
Formatter
public static String format(Locale l, String format, Object... args)
l - 格式化期间应用的locale。
如果l是null ,则不应用本地化。
format - A
format string
args - 格式字符串中格式说明符引用的参数。
如果比格式说明符更多的参数,额外的参数将被忽略。
参数的数量是可变的,可能为零。
参数的最大数量受到The Java™ Virtual Machine Specification定义的Java数组的最大维度的限制 。
一个null参数的行为取决于conversion 。
IllegalFormatException - 如果格式字符串包含非法语法,则与给定参数不兼容的格式说明符,给定格式字符串的参数不足或其他非法条件。
有关所有可能的格式化错误的规范 ,请参阅格式化程序类规范的Details部分
Formatter
public static String valueOf(Object obj)
Object参数的字符串
Object形式。
obj - 一个
Object 。
null ,那么一个字符串等于"null" ;
否则返回值obj.toString() 。
Object.toString()
public static String valueOf(char[] data)
char数组参数的字符串char形式。
字符数组的内容被复制;
字符数组的后续修改不会影响返回的字符串。
data - 字符数组。
String ,其中包含字符数组的字符。
public static String valueOf(char[] data, int offset, int count)
char数组参数的特定子阵列的字符串char形式。
offset参数是子阵列的第一个字符的索引。 count参数指定子数组的长度。 副本的内容被复制; 字符数组的后续修改不会影响返回的字符串。
data - 字符数组。
offset - 子阵列的初始偏移量。
count - 子阵列的长度。
String ,其中包含字符数组的指定子阵列的字符。
IndexOutOfBoundsException - 如果
offset为负数,或
count为负数,或
offset+count大于
data.length 。
public static String copyValueOf(char[] data, int offset, int count)
data - 字符数组。
offset - 子阵列的初始偏移量。
count - 子阵列的长度。
String ,其中包含字符数组的指定子阵列的字符。
IndexOutOfBoundsException - 如果
offset为负数,或
count为负数,或
offset+count大于
data.length 。
public static String copyValueOf(char[] data)
valueOf(char[]) 。
data - 字符数组。
String包含字符阵列的字符。
public static String valueOf(boolean b)
boolean参数的字符串
boolean形式。
b - a
boolean 。
true ,则返回等于"true"的字符串;
否则,等于一个字符串"false"返回。
public static String valueOf(char c)
char参数的字符串
char形式。
c - a
char 。
1的字符串以其单个字符包含参数
c 。
public static String valueOf(int i)
int参数的字符串int形式。
该表示恰好是一个参数的Integer.toString方法返回的表示。
i -
int 。
int参数。
Integer.toString(int, int)
public static String valueOf(long l)
long参数的字符串long形式。
该表示恰好是一个参数的Long.toString方法返回的表示。
l - a
long 。
long参数。
Long.toString(long)
public static String valueOf(float f)
float参数的字符串float形式。
该表示恰好是一个参数的Float.toString方法返回的表示。
f - a
float 。
float参数的字符串表示。
Float.toString(float)
public static String valueOf(double d)
double参数的字符串double形式。
该表示恰好是一个参数的Double.toString方法返回的表示。
d - a
double 。
double参数的字符串
double形式。
Double.toString(double)
public String intern()
最初为空的字符串池由String类String 。
当调用intern方法时,如果池已经包含与equals(Object)方法确定的相当于此String对象的字符串,则返回来自池的字符串。 否则,此String对象将添加到池中,并返回对此String对象的引用。
由此可见,对于任何两个字符串s和t , s.intern() == t.intern()是true当且仅当s.equals(t)是true 。
所有文字字符串和字符串值常量表达式都被实体化。 字符串文字在The Java™ Language Specification的 3.10.5节中定义。
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, 2014, Oracle and/or its affiliates. All rights reserved.