@@ -15,7 +15,7 @@ import AGString
1515class ViewController : UIViewController {
1616
1717 // MARK: Properties
18-
18+
1919 /// The Label
2020 lazy var label : UILabel = {
2121 let label = UILabel ( )
@@ -26,18 +26,103 @@ class ViewController: UIViewController {
2626 label. textAlignment = . center
2727 return label
2828 } ( )
29-
29+
3030 // MARK: View-Lifecycle
31-
31+
3232 /// View did load
3333 override func viewDidLoad( ) {
3434 super. viewDidLoad ( )
3535 self . view. backgroundColor = . white
36+
37+ let str = " abcde "
38+ print ( str [ 1 ..< 3 ] . utf16. 1 )
39+ // print(type(of: str))
40+ // print(str[1]) // => b
41+ // print(str[1..<3]) // => bc
42+ // print(str[1...3]) // => bcd
43+ // print(str[1...]) // => bcde
44+ // print(str[...3]) // => abcd
45+ // print(str[..<3]) // => abc
46+ // print("")
47+
48+ // // With substrings:
49+ // let sub = str[0...]
50+ // print(type(of: sub))
51+ // print(sub[1]) // => b
52+ // print(sub[1..<3]) // => bc
53+ // print(sub[1...3]) // => bcd
54+ // print(sub[1...]) // => bcde
55+ // print(sub[...3]) // => abcd
56+ // print(sub[..<3]) // => abc
57+
3658 }
37-
59+
3860 /// LoadView
3961 override func loadView( ) {
4062 self . view = self . label
4163 }
4264
4365}
66+
67+ extension Substring {
68+ var aa : String {
69+ return String ( self )
70+ }
71+ }
72+
73+ extension String {
74+ subscript ( i: Int ) -> Character {
75+ return self [ index ( startIndex, offsetBy: i) ]
76+ }
77+ subscript ( bounds: CountableRange < Int > ) -> Substring {
78+ let start = index ( startIndex, offsetBy: bounds. lowerBound)
79+ let end = index ( startIndex, offsetBy: bounds. upperBound)
80+ return self [ start ..< end]
81+ }
82+ subscript ( bounds: CountableClosedRange < Int > ) -> Substring {
83+ let start = index ( startIndex, offsetBy: bounds. lowerBound)
84+ let end = index ( startIndex, offsetBy: bounds. upperBound)
85+ return self [ start ... end]
86+ }
87+ subscript ( bounds: CountablePartialRangeFrom < Int > ) -> Substring {
88+ let start = index ( startIndex, offsetBy: bounds. lowerBound)
89+ let end = index ( endIndex, offsetBy: - 1 )
90+ return self [ start ... end]
91+ }
92+ subscript ( bounds: PartialRangeThrough < Int > ) -> Substring {
93+ let end = index ( startIndex, offsetBy: bounds. upperBound)
94+ return self [ startIndex ... end]
95+ }
96+ subscript ( bounds: PartialRangeUpTo < Int > ) -> Substring {
97+ let end = index ( startIndex, offsetBy: bounds. upperBound)
98+ return self [ startIndex ..< end]
99+ }
100+ }
101+ extension Substring {
102+ subscript ( i: Int ) -> Character {
103+ return self [ index ( startIndex, offsetBy: i) ]
104+ }
105+ subscript ( bounds: CountableRange < Int > ) -> Substring {
106+ let start = index ( startIndex, offsetBy: bounds. lowerBound)
107+ let end = index ( startIndex, offsetBy: bounds. upperBound)
108+ return self [ start ..< end]
109+ }
110+ subscript ( bounds: CountableClosedRange < Int > ) -> Substring {
111+ let start = index ( startIndex, offsetBy: bounds. lowerBound)
112+ let end = index ( startIndex, offsetBy: bounds. upperBound)
113+ return self [ start ... end]
114+ }
115+ subscript ( bounds: CountablePartialRangeFrom < Int > ) -> Substring {
116+ let start = index ( startIndex, offsetBy: bounds. lowerBound)
117+ let end = index ( endIndex, offsetBy: - 1 )
118+ return self [ start ... end]
119+ }
120+ subscript ( bounds: PartialRangeThrough < Int > ) -> Substring {
121+ let end = index ( startIndex, offsetBy: bounds. upperBound)
122+ return self [ startIndex ... end]
123+ }
124+ subscript ( bounds: PartialRangeUpTo < Int > ) -> Substring {
125+ let end = index ( startIndex, offsetBy: bounds. upperBound)
126+ return self [ startIndex ..< end]
127+ }
128+ }
0 commit comments