Created
November 18, 2020 03:21
-
-
Save Andy0570/7d9c01b098cbb1e20a837fb2ae82c778 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Foundation | |
extension String { | |
// MARK: 计算字符串的 MD5 哈希值 | |
var md5: String? { | |
let length = Int(CC_MD5_DIGEST_LENGTH) | |
guard let data = self.data(using: String.Encoding.utf8) else { return nil } | |
let hash = data.withUnsafeBytes { (bytes: UnsafeRawBufferPointer) -> [UInt8] in | |
var hash: [UInt8] = [UInt8](repeating: 0, count: Int(CC_MD5_DIGEST_LENGTH)) | |
CC_MD5(bytes.baseAddress, CC_LONG(data.count), &hash) | |
return hash | |
} | |
return (0..<length).map { String(format: "%02x", hash[$0]) }.joined() | |
} | |
// MARK: 返回本地化字符串 | |
var localized: String { | |
NSLocalizedString(self, comment: "") | |
} | |
// MARK: 字符串截取 | |
subscript (i: Int) -> Character { | |
return self[index(startIndex, offsetBy: i)] | |
} | |
subscript (bounds: CountableRange<Int>) -> Substring { | |
let start = index(startIndex, offsetBy: bounds.lowerBound) | |
let end = index(startIndex, offsetBy: bounds.upperBound) | |
if end < start { return "" } | |
return self[start..<end] | |
} | |
subscript (bounds: CountableClosedRange<Int>) -> Substring { | |
let start = index(startIndex, offsetBy: bounds.lowerBound) | |
let end = index(startIndex, offsetBy: bounds.upperBound) | |
if end < start { return "" } | |
return self[start...end] | |
} | |
subscript (bounds: CountablePartialRangeFrom<Int>) -> Substring { | |
let start = index(startIndex, offsetBy: bounds.lowerBound) | |
let end = index(endIndex, offsetBy: -1) | |
if end < start { return "" } | |
return self[start...end] | |
} | |
subscript (bounds: PartialRangeThrough<Int>) -> Substring { | |
let end = index(startIndex, offsetBy: bounds.upperBound) | |
if end < startIndex { return "" } | |
return self[startIndex...end] | |
} | |
subscript (bounds: PartialRangeUpTo<Int>) -> Substring { | |
let end = index(startIndex, offsetBy: bounds.upperBound) | |
if end < startIndex { return "" } | |
return self[startIndex..<end] | |
} | |
// MARK: 内容校验 | |
var containsOnlyDigits: Bool { | |
let notDigits = NSCharacterSet.decimalDigits.inverted | |
return rangeOfCharacter(from: notDigits, options: String.CompareOptions.literal, range: nil) == nil | |
} | |
var containsOnlyLetters: Bool { | |
let notLetters = NSCharacterSet.letters.inverted | |
return rangeOfCharacter(from: notLetters, options: String.CompareOptions.literal, range: nil) == nil | |
} | |
var isAlphanumeric: Bool { | |
let notAlphanumeric = NSCharacterSet.decimalDigits.union(NSCharacterSet.letters).inverted | |
return rangeOfCharacter(from: notAlphanumeric, options: String.CompareOptions.literal, range: nil) == nil | |
} | |
// MARK: 校验电子邮件地址有效性 | |
// 用法:let approved = "[email protected]".isValidEmail // true | |
var isValidEmail: Bool { | |
let emailRegEx = "[A-Z0-9a-z._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,64}" | |
let emailTest = NSPredicate(format: "SELF MATCHES %@", emailRegEx) | |
return emailTest.evaluate(with: self) | |
} | |
// MARK: String 类型转换为 Int 类型 | |
func toInt() -> Int { | |
Int(self)! | |
} | |
func toIntOrNull() -> Int? { | |
Int(self) | |
} | |
/** | |
将字符分组 | |
用法: | |
var cardNumber = "1234567890123456" | |
cardNumber.insert(separator: " ", every: 4) | |
print(cardNumber) | |
// 1234 5678 9012 3456 | |
let pin = "7690" | |
let pinWithDashes = pin.inserting(separator: "-", every: 1) | |
print(pinWithDashes) | |
// 7-6-9-0 | |
*/ | |
mutating func insert(separator: String, every n: Int) { | |
self = inserting(separator: separator, every: n) | |
} | |
func inserting(separator: String, every n: Int) -> String { | |
var result: String = "" | |
let characters = Array(self) | |
stride(from: 0, to: count, by: n).forEach { | |
result += String(characters[$0..<min($0+n, count)]) | |
if $0+n < count { | |
result += separator | |
} | |
} | |
return result | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment