-
Notifications
You must be signed in to change notification settings - Fork 92
/
Contents.swift
46 lines (32 loc) · 833 Bytes
/
Contents.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
//: Playground - noun: a place where people can play
import UIKit
var str = "Hello, Swift"
// 不可以使用[]获取
//str[0]
//str.characters[0]
// startIndex
let startIndex = str.startIndex
startIndex
str[startIndex]
// advancedBy
str[startIndex.advancedBy(5)]
startIndex
let spaceIndex = startIndex.advancedBy(6)
spaceIndex
str[spaceIndex]
// predecessor 和 succesor
str[spaceIndex.predecessor()]
str[spaceIndex.successor()]
// endIndex
let endIndex = str.endIndex
//str[endIndex]
str[endIndex.predecessor()]
// Range
str[startIndex..<spaceIndex]
let range = startIndex..<spaceIndex.predecessor()
str.replaceRange(range, with: "Hi")
str.appendContentsOf("!!!")
str.insert("?", atIndex: str.endIndex)
str.removeAtIndex( str.endIndex.predecessor() )
str
str.removeRange( str.endIndex.advancedBy(-2)..<str.endIndex )