美文网首页
字符串处理

字符串处理

作者: 小笨憨 | 来源:发表于2017-04-05 15:53 被阅读0次

    // 去除空格 字符串修剪 trim
    public extension NSString {

    public class func stringTrim(tring:String) -> String {
        
        let llll    = NSCharacterSet.init(charactersIn: "\n")
        let result  = tring.trimmingCharacters(in: llll as CharacterSet)
    
        return result
    }
    

    }
    // 是否包涵
    public extension NSString {

    public class func stringInclusion(string:String,inclusion:String) -> Bool{
        
        let range = string.range(of: inclusion)
        if range != nil{
            
            print("包涵")
            return true
        }else{
            
            print("不包涵")
            return false
        }
    }
    

    }
    // 替换
    public extension NSString {

    public class func stringReplace(string:String,replaceString:String,deleteString:String) -> String {
        
        let result = string.replacingOccurrences(of: deleteString, with: replaceString)
        print(result)
        return result
    }
    

    }
    // 删除指定字符串
    public extension NSString {

    public class func stringDelete(string:String,deleteString:String) ->String{
        
        let result  = string.replacingOccurrences(of: deleteString, with: "")
        print(result)
        return result
    }
    

    }
    // 首写字母大写
    public extension NSString {

    public class func stringCapitalizeTheFirstLetter(string:String) -> String{
        
        let result = string.capitalized
        print(result)
        return result
    }
    

    }
    // 字符串小写转大写
    public extension NSString {

    public class func stringUppercaseLowercase(string:String) -> String {
        
        let result = string.lowercased()
        print(result)
        return result
    }
    

    }
    // 字符串长度
    public extension NSString {

    public class func stringLength(string:String) -> NSInteger {
        
        let result  = string.characters.count  // 字符串中所有字符的数量
        return result
    }
    

    }
    // Unicode编码
    public extension NSString {

    public class func stringUnicode(string:String,u:NSInteger) -> Void {
        
        if u == 1 {
            
            for codeUTF8 in string.utf8 {
             
                print(codeUTF8)
            }
        }else{
            
            for codeUTF16 in string.utf16 {
                
                print(codeUTF16)
            }
        }
    }
    

    }
    // 非空判断
    public extension NSString {

    public class func stringEmpty(string:String) -> Bool {
        
        let result = string.isEmpty
        if result {
            print("string is nill")
        }else {
            print("string is no nill")
        }
        return result
    }
    

    }
    // 字符串前缀
    public extension NSString {

    public class func stringHasPrefix(string:String,prefix:String) -> Bool {
        
        let result  = string.hasPrefix(prefix)
        if result {
            
            print("有对应的前缀")
        }else {
            
            print("没有对应的前缀")
        }
        return result
    }
    

    }
    // 字符串后缀
    public extension NSString {

    public class func stringHasSuffix(string:String,suffix:String) -> Bool {
        
        let result  = string.hasSuffix(suffix)
        if result {
            print("有对应的后缀")
        }else{
            print("没有对应的后缀")
        }
        return result
    }
    

    }
    // 字符串转data
    public extension NSString {

    public class func stringChangedData(string:String) -> Data {
        
        let result  = string.data(using: .utf8, allowLossyConversion: true)
        return result!
    }
    

    }

    相关文章

      网友评论

          本文标题:字符串处理

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