NSString+CNLiveExtensions.swift
3.62 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
//
// NSString+CNLiveExtensions.swift
// metaCode
//
// Created by open on 2023/6/6.
//
import Foundation
extension NSString {
/// 判断是不是纯数字
/// - Parameter string: 字符串
/// - Returns: 结果
@discardableResult
func isPurnInt() -> Bool {
let scan: Scanner = Scanner(string: self as String)
var val:Int = 0
return scan.scanInt(&val) && scan.isAtEnd
}
/// 当前字符串是否为空
/// - Returns: YES:不为空, NO为空
func isNotBlank()->Bool {
let blank:NSCharacterSet = NSCharacterSet.whitespacesAndNewlines as NSCharacterSet
let count = self.length
for i in (0..<count) {
let c: unichar = self.character(at: i)
if(!blank.characterIsMember(c)){
return true
}
}
return false;
}
/// 字符串转秒数
/// - Parameter dateFormat: 时间格式
/// - Returns: 返回
func timeStrChangeTotimeInterval(_ dateFormat:String? = "yyyy-MM-dd HH:mm:ss") -> String {
if !self.isNotBlank() {
return ""
}
let format = DateFormatter.init()
format.dateStyle = .medium
format.timeStyle = .short
format.locale = Locale(identifier: "zh_CN")
format.timeZone = TimeZone(abbreviation: "UTC+8:00")
if dateFormat == nil {
format.dateFormat = "yyyy-MM-dd HH:mm:ss"
}else{
format.dateFormat = dateFormat
}
let date = format.date(from: self as String)
return "\(Int(date!.timeIntervalSince1970))"
}
/// 字符串转date
/// - Parameter dateFormat: 日期格式
/// - Returns: 返回
func timeStrChangeTotimedate(_ dateFormat:String? = "yyyy-MM-dd HH:mm:ss") -> NSDate {
if !self.isNotBlank() {
return NSDate()
}
let format = DateFormatter.init()
format.dateStyle = .medium
format.timeStyle = .short
format.locale = Locale(identifier: "zh_CN")
format.timeZone = TimeZone(abbreviation: "UTC+8:00")
if dateFormat == nil {
format.dateFormat = "yyyy-MM-dd HH:mm:ss"
}else{
format.dateFormat = dateFormat
}
return format.date(from: self as String)! as NSDate
}
/// 保留2位小数
/// - Returns: 返回
func keepTwoDecimalPlaces() -> String {
let mSelf = NSMutableString.init(string: self)
if self.contains(".") {
let decimalLocation = mSelf.range(of: ".").location
var lastDecimalLocationLength = mSelf.length - decimalLocation - 1
if lastDecimalLocationLength > 2 {
while lastDecimalLocationLength != 2 {
mSelf.deleteCharacters(in: NSMakeRange(mSelf.length - 1, 1))
lastDecimalLocationLength-=1
}
} else if (lastDecimalLocationLength < 2) {
while lastDecimalLocationLength != 0 {
mSelf.append("0")
lastDecimalLocationLength-=1
}
}
} else {
mSelf.append(".00")
}
return mSelf.copy() as! String
}
/// 查看允不允许输入 例如 允许输入1、2时传入12
/// - Parameter string: 输入字符串
/// - Returns: 返回
func allowWriteString(string: String) -> Bool {
let cs = NSCharacterSet.init(charactersIn: string)
let filtered_array = components(separatedBy: cs as CharacterSet) as NSArray
let filtered = filtered_array.componentsJoined(by: "")
return self as String == filtered
}
}