去除冗余空行
This commit is contained in:
@ -52,7 +52,6 @@ Swift 的`String`和`Character`类型提供了快速和兼容 Unicode 的方式
|
||||
> Swift 的`String`类型与 Foundation `NSString`类进行了无缝桥接。Foundation 也可以对`String`进行扩展,暴露在`NSString`中定义的方法。 这意味着,如果你在`String`中调用这些`NSString`的方法,将不用进行转换。
|
||||
> 更多关于在 Foundation 和 Cocoa 中使用`String`的信息请查看 *[Using Swift with Cocoa and Objective-C (Swift 4)](https://developer.apple.com/library/content/documentation/Swift/Conceptual/BuildingCocoaApps/WorkingWithCocoaDataTypes.html#//apple_ref/doc/uid/TP40014216-CH6)*。
|
||||
|
||||
|
||||
<a name="string_literals"></a>
|
||||
## 字符串字面量
|
||||
|
||||
@ -168,7 +167,6 @@ if emptyString.isEmpty {
|
||||
// 打印输出:"Nothing to see here"
|
||||
```
|
||||
|
||||
|
||||
<a name="string_mutability"></a>
|
||||
## 字符串可变性
|
||||
|
||||
@ -187,7 +185,6 @@ constantString += " and another Highlander"
|
||||
> 注意:
|
||||
在 Objective-C 和 Cocoa 中,您需要通过选择两个不同的类(`NSString`和`NSMutableString`)来指定字符串是否可以被修改。
|
||||
|
||||
|
||||
<a name="strings_are_value_types"></a>
|
||||
## 字符串是值类型
|
||||
|
||||
@ -202,7 +199,6 @@ Swift 默认字符串拷贝的方式保证了在函数/方法中传递的是字
|
||||
|
||||
在实际编译时,Swift 编译器会优化字符串的使用,使实际的复制只发生在绝对必要的情况下,这意味着您将字符串作为值类型的同时可以获得极高的性能。
|
||||
|
||||
|
||||
<a name="working_with_characters"></a>
|
||||
## 使用字符
|
||||
|
||||
@ -317,7 +313,6 @@ let message = "\(multiplier) times 2.5 is \(Double(multiplier) * 2.5)"
|
||||
> 注意:
|
||||
> 插值字符串中写在括号中的表达式不能包含非转义反斜杠 (`\`),并且不能包含回车或换行符。不过,插值字符串可以包含其他字面量。
|
||||
|
||||
|
||||
<a name="unicode"></a>
|
||||
## Unicode
|
||||
|
||||
@ -325,7 +320,6 @@ let message = "\(multiplier) times 2.5 is \(Double(multiplier) * 2.5)"
|
||||
它使您可以用标准格式表示来自任意语言几乎所有的字符,并能够对文本文件或网页这样的外部资源中的字符进行读写操作。
|
||||
Swift 的`String`和`Character`类型是完全兼容 Unicode 标准的。
|
||||
|
||||
|
||||
<a name="unicode_scalars"></a>
|
||||
### Unicode 标量
|
||||
|
||||
@ -339,7 +333,6 @@ Unicode 标量是对应字符或者修饰符的唯一的21位数字,例如`U+0
|
||||
|
||||
<a name="special_characters_in_string_literals"></a>
|
||||
|
||||
|
||||
<a name="extended_grapheme_clusters"></a>
|
||||
### 可扩展的字形群集
|
||||
|
||||
@ -361,7 +354,6 @@ let combinedEAcute: Character = "\u{65}\u{301}" // e 后面加上 ́
|
||||
例如,来自朝鲜语字母表的韩语音节能表示为组合或分解的有序排列。
|
||||
在 Swift 都会表示为同一个单一的`Character`值:
|
||||
|
||||
|
||||
```swift
|
||||
let precomposed: Character = "\u{D55C}" // 한
|
||||
let decomposed: Character = "\u{1112}\u{1161}\u{11AB}" // ᄒ, ᅡ, ᆫ
|
||||
@ -377,7 +369,6 @@ let enclosedEAcute: Character = "\u{E9}\u{20DD}"
|
||||
|
||||
地域性指示符号的 Unicode 标量可以组合成一个单一的`Character`值,例如`REGIONAL INDICATOR SYMBOL LETTER U`(`U+1F1FA`)和`REGIONAL INDICATOR SYMBOL LETTER S`(`U+1F1F8`):
|
||||
|
||||
|
||||
```swift
|
||||
let regionalIndicatorForUS: Character = "\u{1F1FA}\u{1F1F8}"
|
||||
// regionalIndicatorForUS 是 🇺🇸
|
||||
@ -414,7 +405,6 @@ print("the number of characters in \(word) is \(word.count)")
|
||||
>
|
||||
> 另外需要注意的是通过`count`属性返回的字符数量并不总是与包含相同字符的`NSString`的`length`属性相同。`NSString`的`length`属性是利用 UTF-16 表示的十六位代码单元数字,而不是 Unicode 可扩展的字符群集。
|
||||
|
||||
|
||||
<a name="accessing_and_modifying_a_string"></a>
|
||||
## 访问和修改字符串
|
||||
|
||||
@ -569,7 +559,6 @@ if latinCapitalLetterA != cyrillicCapitalLetterA {
|
||||
> 注意:
|
||||
> 在 Swift 中,字符串和字符并不区分地域(not locale-sensitive)。
|
||||
|
||||
|
||||
<a name="prefix_and_suffix_equality"></a>
|
||||
### 前缀/后缀相等
|
||||
|
||||
@ -625,7 +614,6 @@ print("\(mansionCount) mansion scenes; \(cellCount) cell scenes")
|
||||
> 注意:
|
||||
> `hasPrefix(_:)`和`hasSuffix(_:)`方法都是在每个字符串中逐字符比较其可扩展的字符群集是否标准相等,详细描述在[字符串/字符相等](#string_and_character_equality)。
|
||||
|
||||
|
||||
<a name="unicode_representations_of_strings"></a>
|
||||
## 字符串的 Unicode 表示形式
|
||||
|
||||
@ -690,7 +678,6 @@ let dogString = "Dog‼🐶"
|
||||
</tr>
|
||||
</table>
|
||||
|
||||
|
||||
```swift
|
||||
for codeUnit in dogString.utf8 {
|
||||
print("\(codeUnit) ", terminator: "")
|
||||
@ -703,7 +690,6 @@ print("")
|
||||
接下来的三个10进制`codeUnit`值 (`226`, `128`, `188`) 是`DOUBLE EXCLAMATION MARK`的3字节 UTF-8 表示。
|
||||
最后的四个`codeUnit`值 (`240`, `159`, `144`, `182`) 是`DOG FACE`的4字节 UTF-8 表示。
|
||||
|
||||
|
||||
<a name="UTF-16_representation"></a>
|
||||
### UTF-16 表示
|
||||
|
||||
@ -739,7 +725,6 @@ print("")
|
||||
</tr>
|
||||
</table>
|
||||
|
||||
|
||||
```swift
|
||||
for codeUnit in dogString.utf16 {
|
||||
print("\(codeUnit) ", terminator: "")
|
||||
@ -764,7 +749,6 @@ print("")
|
||||
|
||||
每一个`UnicodeScalar`拥有一个`value`属性,可以返回对应的21位数值,用`UInt32`来表示:
|
||||
|
||||
|
||||
<table style='text-align:center'>
|
||||
<tr height="77">
|
||||
<td>Character</td>
|
||||
@ -792,7 +776,6 @@ print("")
|
||||
</tr>
|
||||
</table>
|
||||
|
||||
|
||||
```swift
|
||||
for scalar in dogString.unicodeScalars {
|
||||
print("\(scalar.value) ", terminator: "")
|
||||
|
||||
Reference in New Issue
Block a user