校对 Strings and Characters 章节内容

This commit is contained in:
chenmingbiao
2016-09-12 18:11:08 +08:00
parent f1b76b38ed
commit 49cdfb7347

View File

@ -13,7 +13,10 @@
> 校对:[shanks](http://codebuild.me), [Realank](https://github.com/Realank),
> 2.2
> 校对:[SketchK](https://github.com/SketchK) 2016-05-11
> 校对:[SketchK](https://github.com/SketchK)
> 3.0
> 校对:[CMB](https://github.com/chenmingbiao)
本页包含内容:
@ -30,11 +33,12 @@
- [比较字符串](#comparing_strings)
- [字符串的 Unicode 表示形式](#unicode_representations_of_strings)
`String`是例如"hello, world""albatross"这样的有序的`Character`(字符)类型的值的集合。通过`String`类型来表示。
一个`String`的内容可以用许多方式读取,它包括一个`Character`值的集合。
创建和操作字符串的语法与 C 语言中字符串操作相似,轻量并且易读。
字符串连接操作只需要简单地通过`+`符号将两个字符串相连即可。与 Swift 中其他值一样,能否更改字符串的值,取决于其被定义为常量还是变量。你也可以在字符串内插过程中使用字符串插入常量、变量、字面量表达成更长的字符串,这样可以很容易的创建自定义的字符串值,进行展示、存储以及打印。
尽管语法简易,但`String`类型是一种快速、现代化的字符串实现。
每一个字符串都是由编码无关的 Unicode 字符组成,并支持访问字符的多种 Unicode 表示形式representations
@ -43,7 +47,6 @@
> 更多关于在 Foundation 和 Cocoa 中使用`String`的信息请查看 *[Using Swift with Cocoa and Objective-C (Swift 2.1)](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/BuildingCocoaApps/index.html#//apple_ref/doc/uid/TP40014216)*。
<a name="string_literals"></a>
## 字符串字面量String Literals
@ -80,6 +83,7 @@ if emptyString.isEmpty {
// 打印输出:"Nothing to see here"
```
<a name="string_mutability"></a>
## 字符串可变性 (String Mutability)
@ -98,6 +102,7 @@ constantString += " and another Highlander"
> 注意:
在 Objective-C 和 Cocoa 中,您需要通过选择两个不同的类(`NSString``NSMutableString`)来指定字符串是否可以被修改。
<a name="strings_are_value_types"></a>
## 字符串是值类型Strings Are Value Types
@ -112,6 +117,7 @@ Swift 默认字符串拷贝的方式保证了在函数/方法中传递的是字
在实际编译时Swift 编译器会优化字符串的使用,使实际的复制只发生在绝对必要的情况下,这意味着您将字符串作为值类型的同时可以获得极高的性能。
<a name="working_with_characters"></a>
## 使用字符Working with Characters
@ -144,6 +150,7 @@ print(catString)
// 打印输出:"Cat!🐱"
```
<a name="concatenating_strings_and_characters"></a>
## 连接字符串和字符 (Concatenating Strings and Characters)
@ -206,6 +213,7 @@ Unicode 是一个国际标准,用于文本的编码和表示。
它使您可以用标准格式表示来自任意语言几乎所有的字符,并能够对文本文件或网页这样的外部资源中的字符进行读写操作。
Swift 的`String``Character`类型是完全兼容 Unicode 标准的。
<a name="unicode_scalars"></a>
### Unicode 标量Unicode Scalars
@ -326,31 +334,31 @@ print("the number of characters in \(word) is \(word.characters.count)")
使用`startIndex`属性可以获取一个`String`的第一个`Character`的索引。使用`endIndex`属性可以获取最后一个`Character`的后一个位置的索引。因此,`endIndex`属性不能作为一个字符串的有效下标。如果`String`是空串,`startIndex``endIndex`是相等的。
通过调用`String.Index``predecessor()`方法,可以立即得到前面一个索引,调用`successor()`方法可以立即得到后面一个索引。任何一个`String`的索引都可以通过锁链作用的这些方法来获取另一个索引,也可以调用`advancedBy(_:)`方法来获取。但如果尝试获取出界的字符串索引,就会抛出一个运行时错误
通过调用 `String``index(before:)``index(after:)` 方法可以立即得到前面或后面一个索引。您还可以通过调用 `index(_:offsetBy:)` 方法来获取对应偏移量的索引,这种方式可以避免多次调用 `index(before:)``index(after:)` 方法。
你可以使用下标语法来访问`String`特定索引的`Character`
你可以使用下标语法来访问 `String` 特定索引的 `Character`
```swift
let greeting = "Guten Tag!"
greeting[greeting.startIndex]
// G
greeting[greeting.endIndex.predecessor()]
greeting[greeting.index(before: greeting.endIndex)]
// !
greeting[greeting.startIndex.successor()]
greeting[greeting.index(after: greeting.startIndex)]
// u
let index = greeting.startIndex.advancedBy(7)
let index = greeting.index(greeting.startIndex, offsetBy: 7)
greeting[index]
// a
```
试图获取越界索引对应的`Character`,将引发一个运行时错误。
试图获取越界索引对应的 `Character`,将引发一个运行时错误。
```swift
greeting[greeting.endIndex] // error
greeting.endIndex.successor() // error
greeting.index(after: endIndex) // error
```
使用`characters`属性的`indices`属性会创建一个包含全部索引的范围(`Range`),用来在一个字符串中访问单个字符。
使用 `characters` 属性的 `indices` 属性会创建一个包含全部索引的范围(`Range`),用来在一个字符串中访问单个字符。
```swift
for index in greeting.characters.indices {
@ -359,39 +367,37 @@ for index in greeting.characters.indices {
// 打印输出 "G u t e n T a g ! "
```
> 注意:
> 您可以使用 `startIndex` 和 `endIndex` 属性或者 `index(before:)` 、`index(after:)` 和 `index(_:offsetBy:)` 方法在任意一个确认的 `Collection`协议类型里面,如上文所示是使用在 `String` 中,您也可以使用在 `Array``Dictionary` 和 `Set`。
<a name="inserting_and_removing"></a>
### 插入和删除 (Inserting and Removing)
调用`insert(_:atIndex:)`方法可以在一个字符串的指定索引插入一个字符。
调用 `insert(_:atIndex:)` 方法可以在一个字符串的指定索引插入一个字符,调用 `insert(contentsOf:at:)` 方法可以在一个字符串的指定索引插入一个段字符串
```swift
var welcome = "hello"
welcome.insert("!", atIndex: welcome.endIndex)
// welcome now 现在等于 "hello!"
welcome.insert("!", at: welcome.endIndex)
// welcome 变量现在等于 "hello!"
welcome.insert(contentsOf:" there".characters, at: welcome.index(before: welcome.endIndex))
// welcome 变量现在等于 "hello there!"
```
调用`insertContentsOf(_:at:)`方法可以在一个字符串的指定索引插入一个字符串。
调用 `remove(at:)` 方法可以在一个字符串的指定索引删除一个字符,调用 `removeSubrange(_:)` 方法可以在一个字符串的指定索引删除一个字符串。
```swift
welcome.insertContentsOf(" there".characters, at: welcome.endIndex.predecessor())
// welcome 现在等于 "hello there!"
```
调用`removeAtIndex(_:)`方法可以在一个字符串的指定索引删除一个字符。
```swift
welcome.removeAtIndex(welcome.endIndex.predecessor())
welcome.remove(at: welcome.index(before: welcome.endIndex))
// welcome 现在等于 "hello there"
```
调用`removeRange(_:)`方法可以在一个字符串的指定索引删除一个子字符串。
```swift
let range = welcome.endIndex.advancedBy(-6)..<welcome.endIndex
welcome.removeRange(range)
let range = welcome.index(welcome.endIndex, offsetBy: -6)..<welcome.endIndex
welcome.removeSubrange(range)
// welcome 现在等于 "hello"
```
> 注意:
> 您可以使用 `insert(_:at:)`、`insert(contentsOf:at:)`、`remove(at:)` 和 `removeSubrange(_:)` 方法在任意一个确认的 `RangeReplaceableCollection` 协议类型里面,如上文所示是使用在 `String` 中,您也可以使用在 `Array``Dictionary` 和 `Set`。
<a name="comparing_strings"></a>
## 比较字符串 (Comparing Strings)
@ -523,7 +529,6 @@ Swift 提供了几种不同的方式来访问字符串的 Unicode 表示形式
let dogString = "Dog‼🐶"
```
<a name="UTF-8_representation"></a>
### UTF-8 表示