swift 字符串拼接、截取

import UIKit

//swift 字符串
//1>swift中的字符串String是一个结构体,性能更高
//2>NSString是一个OC对象,性能略差
//3>String支持直接遍历
//4>Swift提供了String和NSString的无缝转换
let str = "hello world"

//遍历字符串
for c in str.characters{
    print(c)
}

//字符串拼接
//两个字符串之间的拼接
let str1 = "你好"

let str2 = "世界"

let str3 = str1 + str2

//字符串和其它标识符的拼接
let name = "ZYF"

let age = 18

let height = 1.88

let info = "my name is \(name) , my age is \(age) , my height is \(height)"

//拼接字符串时,字符串的格式化
let minent = 2

let seconds = 8

let timeString = String(format: "%02d:%02d", arguments:[minent,seconds])

print(timeString)

//字符串的截取
let urlStr = "www.zhengyanfeng.com"

//将String类型转换为NSString类型


let header = (urlStr as NSString).substring(to: 3)

let body = (urlStr as NSString).substring(with: NSRange(location: 4, length: 12))

let afterbody = (urlStr as NSString).substring(from: 17)


你可能感兴趣的:(swift)