-
Notifications
You must be signed in to change notification settings - Fork 0
/
String Extensions.swift
49 lines (40 loc) · 1.21 KB
/
String Extensions.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
//
// String Extenstions.swift
// InTheBlack
//
// Created by Nathan Mann on 11/24/15.
// Copyright © 2015 Nathan Mann. All rights reserved.
//
import UIKit
extension String {
func trimWhiteSpace() -> String {
return self.trimmingCharacters(in: .whitespacesAndNewlines)
}
func trimNewLines() -> String {
return self.trimmingCharacters(in: .newlines)
}
func trimCharactersFromFront(_ characters: [Character]) -> String {
var startingIndex: Index = self.startIndex
for letter in self.characters {
if characters.contains(letter) {
startingIndex = self.index(startingIndex, offsetBy: 1)
}
else {
break;
}
}
return self.substring(from: startingIndex)
}
func trimCharactersFromEnd(_ characters: [Character]) -> String {
var endingIndex: Index = self.endIndex
for letter in self.characters.reversed() {
if characters.contains(letter) {
endingIndex = self.index(endingIndex, offsetBy: -1)
}
else {
break;
}
}
return self.substring(to: endingIndex)
}
}