String

The String class represents(代表,表示) character strings. All string literals(字面值,文字) in Java programs, such as "abc", are implemented as instances(实例) of this class.

Strings are constant(常量,常数); their values cannot be changed after they are created. String buffers support(支持,维持) mutable(易变的,可变的) strings. Because String objects areimmutable(不变的,不可变的) 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 stringconcatenation(串联,连接) operator(操作员,运算符) ( + ), and for conversion(转换) of other objects to strings. String concatenation is implemented through(通过,穿过) the StringBuilder(orStringBuffer) class and its append method. String conversions are implemented through the methodtoStringdefined by Object(是由Oeject定义) 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 nullargument to a constructor(构造函数) or method in this class will cause a NullPointerException to be thrown(抛出).

String represents a string in the UTF-16 format in which supplementary(补充,增补) charactersare represented by surrogate(代理) pairs (see the section Unicode Character Representations(表现形式) in the Character class for more information). Index values refer to(指的是,参考) charcode units(单位,单元), so a supplementary character uses two positions(占用两个位置) in aString.

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)