-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathString.swift
More file actions
executable file
·131 lines (87 loc) · 3.39 KB
/
Copy pathString.swift
File metadata and controls
executable file
·131 lines (87 loc) · 3.39 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
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
//
// String.swift
// Created by Yoel Lev on 5/2/17.
//
import Foundation
extension String {
//compute the length
var length: Int {
return self.characters.count
}
//formats a string to date format
var datevalue: Date! {
let stringFormatter = DateFormatter()
stringFormatter.dateFormat = "MM-dd-yyyy"
stringFormatter.locale = Locale(identifier: "en_US_POSIX")
//check for correct date format
if let d = stringFormatter.date(from: self) {
return Date(timeInterval: 0, since: d)
}
else {
return nil
}
}
//returns characters up to a specified integer
func substring(to: Int) -> String {
//define the range
let range = self.index(self.startIndex, offsetBy: to)
return self.substring(to: range)
}
//replace string content
func replace(element:String, replacement:String) -> String {
return self.replacingOccurrences(of: element, with: replacement)
}
//removes empty string content
func removingWhitespace() -> String {
return self.replace(element: " ", replacement: "")
}
//convert a string into a character array
func characters() ->Array<Character> {
return Array(self.characters)
}
//reverse string order
func reverse() -> String! {
/*
notes: While this operation would normally be done 'in-place', there are limited
functions for manipulating native Swift strings. Even there is a
native Array.reverse() method, this has been added as an example interview question.
*/
//convert to array
var characters = self.characters()
var findex: Int = 0
var bindex: Int = characters.count - 1
while findex < bindex {
//swap positions - inout parameters
swap(&characters[findex], &characters[bindex])
//update values
findex += 1
bindex -= 1
} //end while
return String(characters)
}
/*
Ex:
let str = "Test string"
let paddedStr = str.padding(toLength: 20, withPad: " ", startingAt: 0)
*/
//returns a padded string to the left
func leftPadding(toLength: Int, withPad character: Character) -> String {
let newLength = self.characters.count
if newLength < toLength {
return String(repeatElement(character, count: toLength - newLength)) + self
} else {
return self.substring(from: index(self.startIndex, offsetBy: newLength - toLength))
}
}
// Verifies if an string is a valid email format. Nice for login screens.
func checkIfValidEmail() -> Bool {
let emailFormat = "[A-Z0-9a-z._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,64}"
let emailPredicate = NSPredicate(format:"SELF MATCHES %@", emailFormat)
return emailPredicate.evaluate(with: self)
}
// It's common to get back server data with underscores in it. This removes them.
// Example: "new_movies" will change to "New Movies"
func titleize() -> String {
return self.replacingOccurrences(of: "_", with: " ").capitalized
}
}