字符串扩展

求字符串大小

extension String {
func getSize(_ font: UIFont) -> CGSize {
    let rect = NSString(string: self).boundingRect(with: CGSize(width: CGFloat(MAXFLOAT), height: CGFloat(MAXFLOAT)), options: .usesLineFragmentOrigin, attributes: [NSAttributedString.Key.font: font], context: nil)
    return rect.size
}

func getHeight(_ font: UIFont, _ width: CGFloat) -> CGFloat {
    let rect = NSString(string: self).boundingRect(with: CGSize(width: width, height: CGFloat(MAXFLOAT)), options: .usesLineFragmentOrigin, attributes: [NSAttributedString.Key.font: font], context: nil)
    return ceil(rect.height)
}

func getHeight(_ font: UIFont, _ width: CGFloat, _ maxHeight: CGFloat) -> CGFloat {
    let rect = NSString(string: self).boundingRect(with: CGSize(width: width, height: CGFloat(MAXFLOAT)), options: .usesLineFragmentOrigin, attributes: [NSAttributedString.Key.font: font], context: nil)
    return ceil(rect.height) > maxHeight ? maxHeight : ceil(rect.height)
}

func getWidth(_ font: UIFont, _ height: CGFloat) -> CGFloat {
    let rect = NSString(string: self).boundingRect(with: CGSize(width: CGFloat(MAXFLOAT), height: height), options: .usesLineFragmentOrigin, attributes: [NSAttributedString.Key.font: font], context: nil)
    return ceil(rect.width)
}

func getWidth(_ font: UIFont, _ height: CGFloat, _ maxWidth: CGFloat) -> CGFloat {
    let rect = NSString(string: self).boundingRect(with: CGSize(width: CGFloat(MAXFLOAT), height: height), options: .usesLineFragmentOrigin, attributes: [NSAttributedString.Key.font: font], context: nil)
    return ceil(rect.width) > maxWidth ? maxWidth : ceil(rect.width)
}
}

字符串解码、转换

extension String {
var unicodeStr: String {
    let tempStr1 = self.replacingOccurrences(of: "\\u", with: "\\U")
    let tempStr2 = tempStr1.replacingOccurrences(of: "\"", with: "\\\"")
    let tempStr3 = "\"".appending(tempStr2).appending("\"")
    let tempData = tempStr3.data(using: String.Encoding.utf8)
    var returnStr:String = ""
    do {
        returnStr = try PropertyListSerialization.propertyList(from: tempData!, options: [.mutableContainers], format: nil) as! String
    } catch {
        deprint(error)
    }
    return returnStr.replacingOccurrences(of: "\\r\\n", with: "\n")
}

func utf8Str() -> String {
    guard includeChinese() else {return self}
    return self.utf8HTMLStr()
}

func unicode() -> String {
    guard includeChinese() else {return self}
    
    
    return ""
}

func includeChinese() -> Bool {
    for (_, value) in self.enumerated() {
        if ("\u{4E00}" <= value  && value <= "\u{9FA5}") {
            return true
        }
    }
    
    return false
}

func toUTF8() -> String {
    var arr = [UInt8]()
    arr += self.utf8
    return String(bytes: arr, encoding: .utf8) ?? self
}

func GBK() -> String {
    let enc = CFStringConvertEncodingToNSStringEncoding(UInt32(CFStringEncodings.GB_18030_2000.rawValue))
    let str = String(data: data(using: .utf8)!, encoding: String.Encoding(rawValue: enc))
    return str ?? self
}

func utf8UrlStr() -> String {
    var set = CharacterSet()
    set.formUnion(CharacterSet.urlQueryAllowed)
    set.remove(charactersIn: "[].:/?&=;+!@#$()',*\"")
    return self.addingPercentEncoding(withAllowedCharacters: set) ?? self
}

func utf8HTMLStr() -> String {
    var result = ""
    
    for item  in self.utf16 {
        let str = String(format: "%04x", item)
        result.append("&#x" + str + ";")
    }
    
    return result
}
}

字符串截取

extension String {
/// 替换字符串最后一部分(例:AA>BB>CC,s 为:>,取:CC)
mutating func replaceLast(with str: String, s: String) {
    var arr = self.components(separatedBy: s)
    arr[arr.count - 1] = str
    self = arr.joined(separator: s)
}

/// 替换
mutating func replace(_ str: String, _ toStr: String) {
    var string = self
    
    if let range = string.range(of: str) {
        string.replaceSubrange(range, with: toStr)
    }
    
    self = string
}

mutating func removeLastSpace() {
    let str = self
    if str.last == " " {
        self = String(str.prefix(upTo: str.index(str.endIndex, offsetBy: -1)))
        self.removeLastSpace()
    }
}

static func isEmpty(_ string: String?) -> Bool {
    return string == nil || string == ""
}

func sub(_ start: Int, _ count: Int) -> String {
    let startIndex = self.index(self.startIndex, offsetBy: start)
    let endIndex =  self.index(self.startIndex, offsetBy: start + count)
    return String(self[startIndex..

字符串汉字处理

extension String { /// 列表排序
/// 判断字符串中是否有中文
func isIncludeChinese() -> Bool {
    for ch in self.unicodeScalars {
        if (0x4e00 < ch.value  && ch.value < 0x9fff) { return true } // 中文字符范围:0x4e00 ~ 0x9fff
    }
    return false
}

/// 获取大写首字母
func firstChar() -> String {
    guard self.count > 0 else {return ""}
    
    var firstString: String = ""
    
    //判断首字母是否为大写
    let regexA = "^[A-Z]$"
    let predA = NSPredicate(format: "SELF MATCHES %@", regexA)
    
    if isIncludeChinese() {
        let strPinYin = pinyinString()
        //截取大写首字母
        firstString = String(strPinYin.first!).uppercased()
    }else {
        firstString =  String(self.first!).uppercased()
    }
    
    return predA.evaluate(with: firstString) ? firstString : "#"
}

func pinyinString() -> String {
    //转变成可变字符串
    let mutableString = NSMutableString(string: self)
    
    //将中文转换成带声调的拼音
    CFStringTransform(mutableString as CFMutableString, nil, kCFStringTransformToLatin, false)
    
    //去掉声调
    let pinyinString = mutableString.folding(options: String.CompareOptions.diacriticInsensitive, locale: .current)
    
    //多音字
    let strPinYin = polyphoneStringHandle(nameString: self, pinyinString: pinyinString)
    
    return strPinYin
}

//多音字处理,根据需要添自行加
func polyphoneStringHandle(nameString: String, pinyinString: String) -> String {
    if nameString.hasPrefix("沈") {return "shen"}
    return pinyinString
}
}

字符串 Mac地址

extension String { // mac地址转换
func macType() -> String { /// 带 :的大写 Mac 地址
    if !self.contains(":") {
        var arr: [String] = []
        
        let j = 2
        let count = self.count / 2 + (self.count % 2 == 0 ? 0 : 1)
        for i in 0.. String { /// 不带 :的十六进制地址
    return removeText(":").lowercased()
}

func removeText(_ text: String) -> String {
    if self.contains(text) {
        return self.replacingOccurrences(of: text, with: "")
    }
    return self
}
}

字符串进制转换

enum NumberType: Int {
case Octal = 8
case Hex = 16
case Decimal = 10
}

extension String {

// 整形的 utf8 编码范围
static let intRange = 48...57
// 小写 a~f 的 utf8 的编码范围
static let lowercaseRange = 97...102
// 大写 A~F 的 utf8 的编码范围
static let uppercasedRange = 65...70

// byte数组转字符串(Mac)
// bytes: byte数组
// capChar: 插入的字符
// range: 截取范围(元组)
// type: 进制
static func bytesToString(_ bytes: [UInt8],
                          _ capChar: String,
                          _ range: (Int, Int),
                          _ type: NumberType) -> String
{
    var result = ""
    let count = range.0 + range.1
    
    if bytes.count > count {
        for i in range.0.. String {
    var result = ""
    let count = range.0 + range.1
    
    if bytes.count > count {
        for i in range.0.. Data {
    var bytes: [UInt8] = []
    
    switch type {
    case .Decimal:
        bytes = self.decStringToBytes()
    case .Hex:
        bytes = self.hexStringToBytes()
    case .Octal:
        break
    }
    
    return Data(bytes)
}

// 十进制字符串转byte数组
func decStringToBytes() -> [UInt8] {
    var bytes: [UInt8] = []
    
    if self.count > 0, self.count % 2 == 0 {
        var sum = 0
        // 整形的 utf8 编码范围
        let intRange = 48...57
        for (index, c) in self.utf8CString.enumerated() {
            var intC = Int(c.byteSwapped)
            if intC == 0 {
                break
            } else if intRange.contains(intC) {
                intC -= 48
            } else {
                print("字符串格式不对,每个字符都需要在0~9内")
                break
            }
            sum = sum * 10 + intC

            if index % 2 != 0 {
                bytes.append(UInt8(sum))
                sum = 0
            }
        }
    }
    
    return bytes
}

// 十六进制字符串转byte数组
func hexStringToBytes() -> [UInt8] {
    var bytes: [UInt8] = []
    
    if self.count > 0, self.count % 2 == 0 {
        var sum = 0

        for (index, c) in self.utf8CString.enumerated() {
            var intC = Int(c.byteSwapped)
            if intC == 0 {
                break
            } else if String.intRange.contains(intC) {
                intC -= 48
            } else if String.lowercaseRange.contains(intC) {
                intC -= 87
            } else if String.uppercasedRange.contains(intC) {
                intC -= 55
            } else {
                print("字符串格式不对,每个字符都需要在0~9,a~f,A~F内")
                break
            }
            sum = sum * 16 + intC
            // 每两个十六进制字母代表8位,即一个字节
            if index % 2 != 0 {
                bytes.append(UInt8(sum))
                sum = 0
            }
        }
    }
    
    return bytes
}

// 单个十六进制转十进制
func hexToDec() -> Int {
    var result = -1
    
    if self.count > 0 {
        for c in self.utf8 {
            var i = Int(c.byteSwapped)
            if i  == 0 {
                result = 0
            }else if String.intRange.contains(i) {
                i -= 48
            }else if String.lowercaseRange.contains(i) {
                i -= 87
            }else if String.uppercasedRange.contains(i) {
                i -= 55
            }else {
                break
            }
            
            result = result * 16 + i
        }
    }
    
    return result
}

// 取子字符串数组
func substrings(_ subCount: Int) -> [String] {
    var result = [String]()
    
    if self.count > 0 {
        // 分组
        var count = self.count / subCount
        let remainder = self.count % subCount
        if remainder > 0 {
            count += 1
        }
        for i in 0.. [Character] {
    var result = [Character]()
    
    if self.count > 0 {
        for char in self {
            result.append(char)
        }
    }
    
    return result
}
}

你可能感兴趣的:(字符串扩展)