所有类


java.lang
类 String

java.lang.Object
  继承者 java.lang.String
所有已实现的接口:
Serializable, CharSequence, Comparable<String>

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 Standard 版本。

Java 语言提供对字符串串联符号("+")和其他对象到字符串的转换的特殊支持。字符串串联是通过 StringBuilder(或 StringBuffer)类及其 append 方法实现的。字符串转换是通过 toString 方法实现的,该方法由 Object 类定义,并可被 Java 中所有类继承。有关字符串串联和转换的更多信息,请参阅 Gosling、Joy 和 Steele 合著的《The Java Language Specification》。

除非另行说明,否则将 null 参数传递给此类中的构造方法或方法都会抛出 NullPointerException

String 表示一个 UTF-16 格式的字符串,其中的增补字符代理项对 表示(有关详细信息,请参阅 Character 类中的 Unicode 字符表示形式)。索引值是指 char 代码单元,因此增补字符在 String 中占用两个位置。

String 类提供处理 Unicode 代码点(即字符)和 Unicode 代码单元(即 char 值)的方法。

从以下版本开始:
JDK1.0
另请参见:
Object.toString(), StringBuffer, StringBuilder, Charset, 序列化表格

字段摘要
static Comparator<String>CASE_INSENSITIVE_ORDER
          一个排序 String 对象的 Comparator,它的作用与 compareToIgnoreCase 相同。
 
构造方法摘要
String()
          初始化一个新创建的 String 对象,它表示一个空字符序列。
String(byte[] bytes)
          构造一个新的 String,方法是使用平台的默认字符集解码字节的指定数组。
String(byte[] ascii, int hibyte)
          已过时。 该方法无法将字节正确转换为字符。从 JDK 1.1 起,完成该转换的首选方法是通过 String 构造方法,该方法接受一个字符集名称或使用平台的默认字符集。
String(byte[] bytes, int offset, int length)
          构造一个新的 String,方法是使用指定的字符集解码字节的指定子数组。
String(byte[] ascii, int hibyte, int offset, int count)
          已过时。 该方法无法将字节正确转换为字符。从 JDK 1.1 开始,完成该转换的首选方法是通过 String 构造方法,它接受一个字符集名称,或者使用平台默认的字符集。
String(byte[] bytes, int offset, int length, String charsetName)
          构造一个新的 String,方法是使用指定的字符集解码字节的指定子数组。
String(byte[] bytes, String charsetName)
          构造一个新的 String,方法是使用指定的字符集解码指定的字节数组。
String(char[] value)
          分配一个新的 String,它表示当前字符数组参数中包含的字符序列。
String(char[] value, int offset, int count)
          分配一个新的 String,它包含来自该字符数组参数的一个子数组的字符。
String(int[] codePoints, int offset, int count)
          分配一个新的 String,它包含该 Unicode 代码点数组参数的一个子数组的字符。
String(String original)
          初始化一个新创建的 String 对象,表示一个与该参数相同的字符序列;换句话说,新创建的字符串是该参数字符串的一个副本。
String(StringBuffer buffer)
          分配一个新的字符串,它包含当前包含在字符串缓冲区参数中的字符序列。
String(StringBuilder builder)
          分配一个新的字符串,它包含当前包含在字符串生成器参数中的字符序列。
 
方法摘要
 charcharAt(int index)
          返回指定索引处的 char 值。
 intcodePointAt(int index)
          返回指定索引处的字符(Unicode 代码点)。
 intcodePointBefore(int index)
          返回指定索引之前的字符(Unicode 代码点)。
 intcodePointCount(int beginIndex, int endIndex)
          返回此 String 的指定文本范围中的 Unicode 代码点数。
 intcompareTo(String anotherString)
          按字典顺序比较两个字符串。
 intcompareToIgnoreCase(String str)
          不考虑大小写,按字典顺序比较两个字符串。
 Stringconcat(String str)
          将指定字符串联到此字符串的结尾。
 booleancontains(CharSequence s)
          当且仅当此字符串包含 char 值的指定序列时,才返回 true。
 booleancontentEquals(CharSequence cs)
          当且仅当此 String 表示与指定序列相同的 char 值时,才返回 true
 booleancontentEquals(StringBuffer sb)
          当且仅当此 String 表示与指定的 StringBuffer 相同的字符序列时,才返回 true
static StringcopyValueOf(char[] data)
          返回指定数组中表示该字符序列的字符串。
static StringcopyValueOf(char[] data, int offset, int count)
          返回指定数组中表示该字符序列的字符串。
 booleanendsWith(String suffix)
          测试此字符串是否以指定的后缀结束。
 booleanequals(Object anObject)
          比较此字符串与指定的对象。
 booleanequalsIgnoreCase(String anotherString)
          将此 String 与另一个 String 进行比较,不考虑大小写。
static Stringformat(Locale l, String format, Object... args)
          使用指定的语言环境、格式字符串和参数返回一个格式化字符串。
static Stringformat(String format, Object... args)
          使用指定的格式字符串和参数返回一个格式化字符串。
 byte[]getBytes()
          使用平台默认的字符集将此 String 解码为字节序列,并将结果存储到一个新的字节数组中。
 voidgetBytes(int srcBegin, int srcEnd, byte[] dst, int dstBegin)
          已过时。 该方法无法将字符正确转换为字节。从 JDK 1.1 起,完成该转换的首选方法是通过 getBytes() 构造方法,该方法使用平台的默认字符集。
 byte[]getBytes(String charsetName)
          使用指定的字符集将此 String 解码为字节序列,并将结果存储到一个新的字节数组中。
 voidgetChars(int srcBegin, int srcEnd, char[] dst, int dstBegin)
          将字符从此字符串复制到目标字符数组。
 inthashCode()
          返回此字符串的哈希码。
 intindexOf(int ch)
          返回指定字符在此字符串中第一次出现处的索引。
 intindexOf(int ch, int fromIndex)
          从指定的索引开始搜索,返回在此字符串中第一次出现指定字符处的索引。
 intindexOf(String str)
          返回第一次出现的指定子字符串在此字符串中的索引。
 intindexOf(String str, int fromIndex)
          从指定的索引处开始,返回第一次出现的指定子字符串在此字符串中的索引。
 Stringintern()
          返回字符串对象的规范化表示形式。
 intlastIndexOf(int ch)
          返回最后一次出现的指定字符在此字符串中的索引。
 intlastIndexOf(int ch, int fromIndex)
          从指定的索引处开始进行后向搜索,返回最后一次出现的指定字符在此字符串中的索引。
 intlastIndexOf(String str)
          返回在此字符串中最右边出现的指定子字符串的索引。
 intlastIndexOf(String str, int fromIndex)
          从指定的索引处开始向后搜索,返回在此字符串中最后一次出现的指定子字符串的索引。
 intlength()
          返回此字符串的长度。
 booleanmatches(String regex)
          通知此字符串是否匹配给定的正则表达式
 intoffsetByCodePoints(int index, int codePointOffset)
          返回此 String 中从给定的 index 处偏移 codePointOffset 个代码点的索引。
 booleanregionMatches(boolean ignoreCase, int toffset, String other, int ooffset, int len)
          测试两个字符串区域是否相等。
 booleanregionMatches(int toffset, String other, int ooffset, int len)
          测试两个字符串区域是否相等。
 Stringreplace(char oldChar, char newChar)
          返回一个新的字符串,它是通过用 newChar 替换此字符串中出现的所有 oldChar 而生成的。
 Stringreplace(CharSequence target, CharSequence replacement)
          使用指定的字面值替换序列替换此字符串匹配字面值目标序列的每个子字符串。
 StringreplaceAll(String regex, String replacement)
          使用给定的 replacement 字符串替换此字符串匹配给定的正则表达式的每个子字符串。
 StringreplaceFirst(String regex, String replacement)
          使用给定的 replacement 字符串替换此字符串匹配给定的正则表达式的第一个子字符串。
 String[]split(String regex)
          根据给定的正则表达式的匹配来拆分此字符串。
 String[]split(String regex, int limit)
          根据匹配给定的正则表达式来拆分此字符串。
 booleanstartsWith(String prefix)
          测试此字符串是否以指定的前缀开始。
 booleanstartsWith(String prefix, int toffset)
          测试此字符串是否以指定前缀开始,该前缀以指定索引开始。
 CharSequencesubSequence(int beginIndex, int endIndex)
          返回一个新的字符序列,它是此序列的一个子序列。
 Stringsubstring(int beginIndex)
          返回一个新的字符串,它是此字符串的一个子字符串。
 Stringsubstring(int beginIndex, int endIndex)
          返回一个新字符串,它是此字符串的一个子字符串。
 char[]toCharArray()
          将此字符串转换为一个新的字符数组。
 StringtoLowerCase()
          使用默认语言环境的规则将此 String 中的所有字符都转换为小写。
 StringtoLowerCase(Locale locale)
          使用给定 Locale 的规则将此 String 中的所有字符都转换为小写。
 StringtoString()
          返回此对象本身(它已经是一个字符串!)。
 StringtoUpperCase()
          使用默认语言环境的规则将此 String 中的所有字符都转换为大写。
 StringtoUpperCase(Locale locale)
          使用给定的 Locale 规则将此 String 中的所有字符都转换为大写。
 Stringtrim()
          返回字符串的副本,忽略前导空白和尾部空白。
static StringvalueOf(boolean b)
          返回 boolean 参数的字符串表示形式。
static StringvalueOf(char c)
          返回 char 参数的字符串表示形式。
static StringvalueOf(char[] data)
          返回 char 数组参数的字符串表示形式。
static StringvalueOf(char[] data, int offset, int count)
          返回 char 数组参数的特定子数组的字符串表示形式。
static StringvalueOf(double d)
          返回 double 参数的字符串表示形式。
static StringvalueOf(float f)
          返回 float 参数的字符串表示形式。
static StringvalueOf(int i)
          返回 int 参数的字符串表示形式。
static StringvalueOf(long l)
          返回 long 参数的字符串表示形式。
static StringvalueOf(Object obj)
          返回 Object 参数的字符串表示形式。
 
从类 java.lang.Object 继承的方法
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
 

字段详细信息

CASE_INSENSITIVE_ORDER

public static final Comparator<String> CASE_INSENSITIVE_ORDER
一个排序 String 对象的 Comparator,它的作用与 compareToIgnoreCase 相同。该比较器是可序列化的。

注意,Comparator 考虑到语言环境,因此可能导致在某些语言环境中排序效果不理想。java.text 包提供 Collators 来完成语言环境敏感的排序。

从以下版本开始:
1.2
另请参见:
Collator.compare(String, String)
构造方法详细信息

String

public String()
初始化一个新创建的 String 对象,它表示一个空字符序列。注意,由于 String 是不可变的,不必使用该构造方法。


String

public String(String original)
初始化一个新创建的 String 对象,表示一个与该参数相同的字符序列;换句话说,新创建的字符串是该参数字符串的一个副本。由于 String 是不可变的,不必使用该构造方法,除非需要 original 的显式副本。

参数:
original - 一个 String

String

public String(char[] value)
分配一个新的 String,它表示当前字符数组参数中包含的字符序列。该字符数组的内容已被复制;后续对字符数组的修改不会影响新创建的字符串。

参数:
value - 此字符串的初始值。

String

public String(char[] value,
              int offset,
              int count)
分配一个新的 String,它包含来自该字符数组参数的一个子数组的字符。offset 参数是子数组第一个字符的索引,count 参数指定子数组的长度。该子数组的内容已被复制;后续对字符数组的修改不会影响新创建的字符串。

参数:
value - 作为字符源的数组。
offset - 初始偏移量。
count - 长度。
抛出:
IndexOutOfBoundsException - 如果 offsetcount 参数索引字符超出 value 数组的范围。

String

public String(int[] codePoints,
              int offset,
              int count)
分配一个新的 String,它包含该 Unicode 代码点数组参数的一个子数组的字符。offset 参数是该子数组第一个代码点的索引,count 参数指定子数组的长度。将该子数组的内容转换为 char;后续对 int 数组的修改不会影响新创建的字符串。

参数:
codePoints - 作为 Unicode 代码点的源的数组。
offset - 初始偏移量。
count - 长度。
抛出:
IllegalArgumentException - 如果在 codePoints 中发现任何无效的 Unicode 代码点
IndexOutOfBoundsException - 如果 offsetcount 参数索引字符超出 codePoints 数组的范围。
从以下版本开始:
1.5

String

@Deprecated
public String(byte[] ascii,
                         int hibyte,
                         int offset,
                         int count)
已过时。 该方法无法将字节正确转换为字符。从 JDK 1.1 开始,完成该转换的首选方法是通过 String 构造方法,它接受一个字符集名称,或者使用平台默认的字符集。

分配一个新的 String,它根据一个 8 位整数值数组的子数组构造。

offset 参数是该子数组的第一个字节的索引,count 参数指定子数组的长度。

子数组中的每个 byte 都按照上述方法转换为 char

参数:
ascii - 要转换为字符的字节。
hibyte - 每个 16 位 Unicode 字符的前 8 位。
offset - 初始偏移量。
count - 长度。
抛出:
IndexOutOfBoundsException - 如果 offsetcount 参数无效。
另请参见:
String(byte[], int), String(byte[], int, int, java.lang.String), String(byte[], int, int), String(byte[], java.lang.String), String(byte[])

String

@Deprecated
public String(byte[] ascii,
                         int hibyte)
已过时。 该方法无法将字节正确转换为字符。从 JDK 1.1 起,完成该转换的首选方法是通过 String 构造方法,该方法接受一个字符集名称或使用平台的默认字符集。

分配一个新的 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), String(byte[], java.lang.String), String(byte[])