字符串处理

// 去除空格 字符串修剪 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!
}

}

你可能感兴趣的:(字符串处理)