美文网首页
字符串扩展

字符串扩展

作者: 微凉初夏 | 来源:发表于2019-10-08 16:15 被阅读0次

求字符串大小

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..<endIndex])
}
}

字符串汉字处理

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..<count {
            let str = self.sub(i * j, j)
            arr.append(str)
        }
        
        return arr.joined(separator: ":").uppercased()
    }
    return self.uppercased()
}

func tabType() -> 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..<count {
            var str = ""
            switch type {
            case .Octal:
                str = String(format: "%o", bytes[i])
            case .Hex:
                str = String(format: "%02X", bytes[i])
            case .Decimal:
                str = String(format: "%d", bytes[i])
            }
            
            result += str
            if i < (count - 1) {
                result += capChar
            }
        }
    }
    
    return result
}

// byte数组转日期字符串
// bytes: byte数组
// range: 截取范围(元组)
static func bytesTpDateString(_ bytes: [UInt8],
                              _ range: (Int, Int)) -> String {
    var result = ""
    let count = range.0 + range.1
    
    if bytes.count > count {
        for i in range.0..<count {
            result += String(format: "%02d", bytes[i])
        }
    }
    
    return result
}

// 字符串转data
// type: 传入的字符串的进制类型
func stringToData(_ type: NumberType) -> 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..<count {
            let index = self.index(String.Index(utf16Offset: i * subCount, in: self), offsetBy: subCount)
            let subStr = String(self.prefix(upTo: index))
            result.append(subStr)
        }
    }
    
    return result
}

// 取字符数组
func characters() -> [Character] {
    var result = [Character]()
    
    if self.count > 0 {
        for char in self {
            result.append(char)
        }
    }
    
    return result
}
}

相关文章

  • ES6扩展

    字符串扩展 正则扩展 数值扩展 函数扩展 对象扩展 数组扩展 字符串扩展 模版字符串 部分新的方法 padStar...

  • 2017-12-26es6-函数扩展

    扩展 1、字符串扩展 2、模板字符串 **** 3、数值扩展 4、函数扩展用于分页居多 rest 参数 ...变量...

  • ES6扩展

    字符串的扩展正则的扩展数值的扩展数组的扩展函数的扩展对象的扩展

  • ES6基础-ES6的扩展

    进行对字符串扩展,正则扩展,数值扩展,函数扩展,对象扩展,数组扩展。 开发环境准备: 编辑器(VS Code, A...

  • JS基础系列(X): ES6入门

    字符串扩展 模板字符串模板字符串可以很方便的代替ES5字符串拼接,格式如下: 代码示例 API 函数扩展 函数默认...

  • Vue项目开发与规范--es6语法规范篇

    目录 简介 let const 解构赋值 正则的扩展 字符串的扩展 数值的扩展 数组的扩展 函数的扩展 对象的扩展...

  • ECMAScript 6 新增内容

    变量的扩展letconst变量的结构 字符串的扩展及新增方法模板字符串 (`${}`)matchAll 方法返回一...

  • iOS Emoji编解码(OBJ-C/Swift)

    编码EMOJI表情字符串OBJ-C:扩展NSString Swift: 解码EMOJI表情字符串OBJ-C扩展NS...

  • 十五、字符串转数字

    字符串转数组 字符串 *待扩展:传递与复制扩展 函数式编程和改变原始数据的编程方法

  • 2019-10-10

    今天写了字符串扩展代码

网友评论

      本文标题:字符串扩展

      本文链接:https://www.haomeiwen.com/subject/dkjoectx.html