-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathString+Utility.swift
97 lines (79 loc) · 3.7 KB
/
String+Utility.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
//
// NSString+Utility.swift
//
//
// Created by Tanmoy on 21/01/17.
// Copyright © 2017 Tanmoy. All rights reserved.
//
import Foundation
extension String {
func trimString() -> String {
var trimmedStr: String = ""
var str_Trimmed: String = (self as NSString).replacingCharacters(in: (self as NSString).range(of:"^\\s*", options: .regularExpression), with: "")
if str_Trimmed.characters.count > 0 {
trimmedStr = (str_Trimmed as NSString).replacingCharacters(in: (str_Trimmed as NSString).range(of:"\\s*$", options: .regularExpression), with: "")
}
return trimmedStr
}
func index(of string: String, options: String.CompareOptions = .literal) -> String.Index? {
return range(of: string, options: options)?.lowerBound
}
func indexes(of string: String, options: String.CompareOptions = .literal) -> [String.Index] {
var result: [String.Index] = []
var start = startIndex
while let range = range(of: string, options: options, range: start..<endIndex) {
result.append(range.lowerBound)
start = range.upperBound
}
return result
}
func ranges(of string: String, options: String.CompareOptions = .literal) -> [Range<String.Index>] {
var result: [Range<String.Index>] = []
var start = startIndex
while let range = range(of: string, options: options, range: start..<endIndex) {
result.append(range)
start = range.upperBound
}
return result
}
}
extension Character
{
func unicodeScalar() -> UnicodeScalar
{
let characterString = String(self)
let scalars = characterString.unicodeScalars
return scalars[scalars.startIndex]
}
}
extension TimeInterval {
func timeIntervalAsString(_ format : String = "dd days, hh hours, mm minutes, ss seconds, sss ms") -> String {
var asInt = NSInteger(self)
let ago = (asInt < 0)
if (ago) {
asInt = -asInt
}
let ms = Int(self.truncatingRemainder(dividingBy: 1) * (ago ? -1000 : 1000))
let s = asInt % 60
let m = (asInt / 60) % 60
let h = ((asInt / 3600))%24
let d = (asInt / 86400)
var value = format
value = value.replacingOccurrences(of: "hh", with: String(format: "%0.2d", h))
value = value.replacingOccurrences(of: "mm", with: String(format: "%0.2d", m))
value = value.replacingOccurrences(of: "sss", with: String(format: "%0.3d", ms))
value = value.replacingOccurrences(of: "ss", with: String(format: "%0.2d", s))
value = value.replacingOccurrences(of: "dd", with: String(format: "%d", d))
if (ago) {
value += " ago"
}
return value
}
}
func isValidEmail(testStr:String) -> Bool {
print("validate emilId: \(testStr)")
let emailRegEx = "^(?:(?:(?:(?: )*(?:(?:(?:\\t| )*\\r\\n)?(?:\\t| )+))+(?: )*)|(?: )+)?(?:(?:(?:[-A-Za-z0-9!#$%&’*+/=?^_'{|}~]+(?:\\.[-A-Za-z0-9!#$%&’*+/=?^_'{|}~]+)*)|(?:\"(?:(?:(?:(?: )*(?:(?:[!#-Z^-~]|\\[|\\])|(?:\\\\(?:\\t|[ -~]))))+(?: )*)|(?: )+)\"))(?:@)(?:(?:(?:[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])?)*)|(?:\\[(?:(?:(?:(?:(?:[0-9]|(?:[1-9][0-9])|(?:1[0-9][0-9])|(?:2[0-4][0-9])|(?:25[0-5]))\\.){3}(?:[0-9]|(?:[1-9][0-9])|(?:1[0-9][0-9])|(?:2[0-4][0-9])|(?:25[0-5]))))|(?:(?:(?: )*[!-Z^-~])*(?: )*)|(?:[Vv][0-9A-Fa-f]+\\.[-A-Za-z0-9._~!$&'()*+,;=:]+))\\])))(?:(?:(?:(?: )*(?:(?:(?:\\t| )*\\r\\n)?(?:\\t| )+))+(?: )*)|(?: )+)?$"
let emailTest = NSPredicate(format:"SELF MATCHES %@", emailRegEx)
let result = emailTest.evaluate(with: testStr)
return result
}