-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathString+CoreFoundation.swift
39 lines (31 loc) · 1.16 KB
/
String+CoreFoundation.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
import Foundation
public extension String {
var length: Int { return count }
var isPresent: Bool { return !isEmpty }
var digits: String {
return components(separatedBy: CharacterSet.decimalDigits.inverted)
.joined()
}
func replace(_ string: String, with withString: String) -> String {
return replacingOccurrences(of: string, with: withString)
}
func truncate(_ length: Int, suffix: String = "...") -> String {
return self.length > length
? substring(to: characters.index(startIndex, offsetBy: length)) + suffix
: self
}
func split(_ delimiter: String) -> [String] {
let components = self.components(separatedBy: delimiter)
return components != [""] ? components : []
}
func trim() -> String {
return trimmingCharacters(in: CharacterSet.whitespaces)
}
var uppercaseFirstLetter: String {
guard isPresent else { return self }
var string = self
string.replaceSubrange(string.startIndex...string.startIndex,
with: String(string[string.startIndex]).capitalized)
return string
}
}