Swift_3.0_初级篇14_拼接字符串_格式化字符串

        func demo () -> () {
        let name = "老王"
        let age = 18
        let title: String? = "BOSS"
        let point = CGPoint (x: 100, y: 100)
        
        //字符串拼接 \(变量/常量)
        let str = "\(name) \(age) \(title)\(point)"
        print(str) //老王 18 Optional("BOSS")(100.0, 100.0)
        
        //拼接字符串需要注意可选项
        //NSStringFromCGPoint(point)
        let str1 = "\(name) \(age) \(title ?? "") \(point)"
        print(str1) //老王 18 BOSS (100.0, 100.0)
    }

        func demo1 () -> () {
        let h = 8
        let m = 9
        let s = 6
        
        let dateStr = "\(h):\(m):\(s)"
        print(dateStr)
        
        let deteStr1 = String(format:"%02d:%02d:%02d",h,h,s)
        print(deteStr1)
        
    }






你可能感兴趣的:(Swift_3.0_初级篇14_拼接字符串_格式化字符串)