美文网首页
swift字符串

swift字符串

作者: 神码 | 来源:发表于2019-03-19 14:51 被阅读0次

    定义字符串

    let a : String = "hello world"
    let b = "hello"
    

    遍历字符串

    for c in str {
        print(c)
    }
    

    字符串之间的拼接

    let str1 = "da"
    let str2 = "jia"
    let str3 = "hao"
    let totalStr = str1 + str2 + str3 
    

    字符串和其他标识符之间的拼接

    let name = "lxd"
    let age = 24
    let height = 188
    let myInfo = "my name is \(name),my name is \(age),my name is \(height)" 
    print(myInfo)
    //输出my name is lxd, my age is 24, my height is 188
    

    字符串的格式化

    let minutes = 6
    let seconds = 33
    let time = String(format: "%02d:%02d", arguments: [minutes, seconds])
    print(time)
    //输出06:33
    

    截取字符串

    let urlStr = "www.baidu.com"
    
    let prefixStr = urlStr.prefix(3) 
    print(prefixStr) //输出www
    
    let suffixStr = urlStr.suffix(3)
    print(suffixStr) //输出com
    
    let range = NSRange(location: 4, length: 5)
    let middleStr = (urlStr as NSString).substring(with: range)
    print(middleStr) //输出baidu
    

    相关文章

      网友评论

          本文标题:swift字符串

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