forked from Zero-zhou/iOS-Extensions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
String+Ex.swift
178 lines (128 loc) · 4.7 KB
/
String+Ex.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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
//
// String+Extension.swift
//
// Created by Augus on 6/21/16.
// Copyright © 2016 iAugus. All rights reserved.
//
import Foundation
public extension String {
var isBlank : Bool {
let s = self
let cset = CharacterSet.newlines.inverted
let r = s.rangeOfCharacter(from: cset)
let ok = s.isEmpty || r == nil
return ok
}
}
public extension String {
public init<Subject>(_ instance: Subject) {
self.init(describing: instance)
}
// public init<Subject>(_ subject: Subject) {
// self.init(reflecting: instance)
// }
}
// MARK: -
extension String {
func slice(from: String, to: String) -> String? {
return (range(of: from)?.upperBound).flatMap { sInd in
(range(of: to, range: sInd..<endIndex)?.lowerBound).map { eInd in
substring(with: sInd..<eInd)
}
}
}
}
extension String {
subscript(i: Int) -> String {
guard i >= 0 && i < characters.count else { return "" }
return String(self[index(startIndex, offsetBy: i)])
}
subscript(range: Range<Int>) -> String {
let lowerIndex = index(startIndex, offsetBy: max(0,range.lowerBound), limitedBy: endIndex) ?? endIndex
return substring(with: lowerIndex..<(index(lowerIndex, offsetBy: range.upperBound - range.lowerBound, limitedBy: endIndex) ?? endIndex))
}
subscript(range: ClosedRange<Int>) -> String {
let lowerIndex = index(startIndex, offsetBy: max(0,range.lowerBound), limitedBy: endIndex) ?? endIndex
return substring(with: lowerIndex..<(index(lowerIndex, offsetBy: range.upperBound - range.lowerBound + 1, limitedBy: endIndex) ?? endIndex))
}
}
extension String {
/// Remove last character
///
/// - returns: the character which has been removed.
@discardableResult
mutating func removeLast() -> Character? {
guard endIndex != startIndex else { return nil }
let index = self.index(before: endIndex)
return remove(at: index)
}
/// Remove first character
///
/// - returns: the character which has been removed.
@discardableResult
mutating func removeFirst() -> Character? {
guard endIndex != startIndex else { return nil }
return remove(at: startIndex)
}
/// Remove last character
///
/// - returns: characters which are left
func dropLast() -> String {
guard startIndex != endIndex else { return self }
return substring(to: index(before: endIndex))
}
/// Remove fisrt character
///
/// - returns: characters which are left
func dropFirst() -> String {
guard startIndex != endIndex else { return self }
return substring(from: index(after: startIndex))
}
@available(*, deprecated, message: "Please use `dropLast()`")
var lastRemoved: String {
guard startIndex != endIndex else { return self }
return substring(to: index(before: endIndex))
}
@available(*, deprecated, message: "Please use `dropFirst()`")
var firstRemoved: String {
guard startIndex != endIndex else { return self }
return substring(from: index(after: startIndex))
}
}
// MARK: -
extension String {
func replacingOccurrences(of: [String], with: String) -> String {
var str = self
of.forEach() {
str = str.replacingOccurrences(of: $0, with: with)
}
return str
}
}
extension String {
func encodeURLComponent() -> String {
return self.addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed) ?? self
}
func decodeURLComponent() -> String {
return self.components(separatedBy: "+").joined(separator: " ").removingPercentEncoding ?? self
}
}
// MARK: -
extension String {
var isValidEmail: Bool {
let emailRegEx = "^[a-zA-Z0-9.!#$%&'*+/=?^_`{|}~-]+@[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(?:\\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)*$"
let emailTest = NSPredicate(format:"SELF MATCHES %@", emailRegEx)
return emailTest.evaluate(with: self)
}
}
// MARK: -
extension String {
static let yes = NSLocalizedString("YES", comment: "")
static let no = NSLocalizedString("NO", comment: "")
static let ok = NSLocalizedString("OK", comment: "")
static let error = NSLocalizedString("Error", comment: "")
static let confirm = NSLocalizedString("Confirm", comment: "")
static let cancel = NSLocalizedString("Cancel", comment: "")
static let delete = NSLocalizedString("Delete", comment: "")
static let deleteAll = NSLocalizedString("Delete All", comment: "")
}