-
Notifications
You must be signed in to change notification settings - Fork 127
Expand file tree
/
Copy pathContents.swift
More file actions
executable file
·75 lines (43 loc) · 1.49 KB
/
Copy pathContents.swift
File metadata and controls
executable file
·75 lines (43 loc) · 1.49 KB
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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
//: Playground - noun: a place where people can play
import UIKit
let string = "My name is Slim Shady"
//string.characters[5]
let startIndex = string.startIndex
let endIndex = string.endIndex
string.characters[startIndex]
string[string.startIndex]
let indexOfTwo = string.index(after: startIndex)
string.characters[indexOfTwo]
let indexOfTwenty = string.index(before: endIndex)
string.characters[indexOfTwenty]
let index_ = string.index(startIndex, offsetBy: 11)
string.characters[index_]
let index = string.index(startIndex, offsetBy: 11, limitedBy: endIndex)
if let negativeIndex = string.index(endIndex, offsetBy: -10, limitedBy: startIndex) {
string.characters[negativeIndex]
}
let range = index_ ..< endIndex
string[range]
extension String {
subscript (i: Int) -> String? {
if characters.count > i && i >= 0 {
return String(Array(self.characters)[i])
}
return nil
}
subscript (r: Range<Int>) -> String? {
guard r.lowerBound >= 0 && r.upperBound <= self.characters.count && r.upperBound >= r.lowerBound else { return nil }
let start = self.index(startIndex, offsetBy: r.lowerBound)
let end = self.index(startIndex, offsetBy: r.upperBound)
return self[Range(start ..< end)]
}
func indexAt(int: Int) -> String.Index? {
if characters.count > int {
return index(self.startIndex, offsetBy: int)
}
return nil
}
}
string[4]
string[3..<21]
string.indexAt(int: 10)