String objects represent character strings in Cocoa and Cocoa Touch frameworks. Representing strings as objects allows you to use strings wherever you use other objects. It also provides the benefits of encapsulation, so that string objects can use whatever encoding and storage is needed for efficiency while simply appearing as arrays of characters.
- String对象表示Cocoa和Cocoa Touch框架中的字符串。 将字符串表示为对象允许您在使用其他对象的任何位置使用字符串。 它还提供了封装的好处,因此字符串对象可以使用任何编码和存储来提高效率,同时只显示为字符数组。
A string object is implemented as an array of Unicode characters (in other words, a text string). An immutable string is a text string that is defined when it is created and subsequently cannot be changed. To create and manage an immutable string, use the NSString
class. To construct and manage a string that can be changed after it has been created, use NSMutableString
.
- 字符串对象实现为Unicode字符数组(换句话说,文本字符串)。 不可变字符串是在创建时定义的文本字符串,随后无法更改。 要创建和管理不可变字符串,请使用NSString类。 要构造和管理可在创建后更改的字符串,请使用NSMutableString。
The objects you create using NSString
and NSMutableString
are referred to as string objects (or, when no confusion will result, merely as strings). The term C stringrefers to the standard C char *
type.
- 使用NSString和NSMutableString创建的对象称为字符串对象(或者,当不会产生混淆时,仅称为字符串)。 术语C字符串是指标准C char *类型。
A string object presents itself as an array of Unicode characters. You can determine how many characters it contains with the length method and can retrieve a specific character with the characterAtIndex: method. These two “primitive” methods provide basic access to a string object. Most use of strings, however, is at a higher level, with the strings being treated as single entities: You compare strings against one another, search them for substrings, combine them into new strings, and so on. If you need to access string objects character-by-character, you must understand the Unicode character encoding—specifically, issues related to composed character sequences. For details see:
- 字符串对象将自身表示为Unicode字符数组。 您可以使用length方法确定它包含的字符数,并可以使用characterAtIndex:方法检索特定字符。 这两个“原始”方法提供对字符串对象的基本访问。 但是,大多数字符串的使用处于更高级别,字符串被视为单个实体:您将字符串相互比较,搜索子字符串,将它们组合成新字符串,依此类推。 如果需要逐个字符地访问字符串对象,则必须了解Unicode字符编码 - 特别是与组合字符序列相关的问题。 详情见:
The Unicode Standard, Version 4.0. The Unicode Consortium. Boston: Addison-Wesley, 2003. ISBN 0-321-18578-1.
The Unicode Consortium web site: http://www.unicode.org/.