Swift4 字符串

字符串截取

  • swift3
public extension UIColor {
    
    public class func colorWithString(hex:String) -> UIColor {
        
        var cString = hex.trimmingCharacters(in: CharacterSet.whitespacesAndNewlines).uppercased()
        if cString.hasPrefix("#"){
            
            let index   = cString.index(cString.startIndex, offsetBy: 1)
            cString     = cString.substring(from: index)
        }
        if cString.characters.count != 6 {
            
            return UIColor.red
        }
        
        let rIndex      = cString.index(cString.startIndex, offsetBy: 2)
        let rString     = cString.substring(to: rIndex)
        let otherString = cString.substring(from: rIndex)
        let gIndex      = otherString.index(otherString.startIndex, offsetBy: 2)
        let gString     = otherString.substring(to: gIndex)
        let bIndex      = cString.index(cString.endIndex, offsetBy: -2)
        let bString     = cString.substring(from: bIndex)
        
        var r:CUnsignedInt  = 0,g:CUnsignedInt = 0 ,b:CUnsignedInt = 0
        Scanner(string: rString).scanHexInt32(&r)
        Scanner(string: gString).scanHexInt32(&g)
        Scanner(string: bString).scanHexInt32(&b)
        return UIColor(red: CGFloat(r)/255.0, green: CGFloat(g)/255.0, blue: CGFloat(b)/255.0, alpha: CGFloat(1))
    }
}
  • swift 4
extension UIColor {
    
    var cString = hex.trimmingCharacters(in: CharacterSet.whitespacesAndNewlines).uppercased()
        if cString.hasPrefix("#"){
            
            let index   = cString.index(cString.startIndex, offsetBy: 1)
            cString     = String(cString.prefix(upTo: index))
        }
        if cString.count != 6 {
            return UIColor.red
        }
        
        let rIndex      = cString.index(cString.startIndex, offsetBy: 2)
        let rString     = cString.prefix(upTo: rIndex)
        
        let otherString = cString.suffix(from: rIndex)
        let gIndex      = otherString.index(otherString.startIndex, offsetBy: 2)
        let gString     = otherString.prefix(upTo: gIndex)
        let bIndex      = cString.index(cString.endIndex, offsetBy: -2)
        let bString     = cString.suffix(from: bIndex)
        
        var r:CUnsignedInt  = 0,g:CUnsignedInt = 0 ,b:CUnsignedInt = 0
        Scanner(string: String(rString)).scanHexInt32(&r)
        Scanner(string: String(gString)).scanHexInt32(&g)
        Scanner(string: String(bString)).scanHexInt32(&b)
        return UIColor(red: CGFloat(r)/255.0, green: CGFloat(g)/255.0, blue: CGFloat(b)/255.0, alpha: CGFloat(1))
   }
}

多行字符串

func printHelp() {
    print(
        """
        ??  Test Drive
        --------------
        Quickly try out any Swift pod or framework in a playground.

        Usage:
        - Simply pass a list of pod names or URLs that you want to test drive.
        - You can also specify a platform (iOS, macOS or tvOS) using the '-p' option
        - To use a specific version or branch, use the '-v' argument (or '-m' for master)

        Examples:
        - testdrive Unbox Wrap Files
        - testdrive https://github.com/johnsundell/unbox.git Wrap Files
        - testdrive Unbox -p tvOS
        - testdrive Unbox -v 2.3.0
        - testdrive Unbox -v swift3
        """
    )
}

待補(bǔ)充。。。

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 現(xiàn)有字符串Hello, World! 想截取前5個(gè)字符為子串,有兩種方法,一是使用字符串的集合特性: 二是使用字符...
    囧書閱讀 11,824評(píng)論 1 7
  • 取前3個(gè)字符 取后3個(gè)字符 遍歷字符串 插入字符串 替換字符串 字符串切片,獲取interesting 字符串切片...
    Roct閱讀 1,374評(píng)論 0 2
  • 發(fā)現(xiàn) 關(guān)注 消息 iOS 第三方庫、插件、知名博客總結(jié) 作者大灰狼的小綿羊哥哥關(guān)注 2017.06.26 09:4...
    肇東周閱讀 15,150評(píng)論 4 61
  • 轉(zhuǎn)載自:https://github.com/Tim9Liu9/TimLiu-iOS[https://github...
    香橙柚子閱讀 9,145評(píng)論 0 36
  • 隨著年齡的增長,愛情成了唯一的救贖
    卿卿一笑如斯閱讀 239評(píng)論 0 0

友情鏈接更多精彩內(nèi)容