// 遍历字符串
// 字符串遍历
var str = "Hello, Swift"
for c in str.characters {
print(c)
}
// 字符串拼接
//两个字符串的拼接
let str1 = "Hello"
let str2 = "World"
let str3 = str1 + str2
// 字符串和其他数据类型的拼接
let name = "why"
let age = 18
let info = "my name is \(name), age is \(age)"
// 字符串的格式化
比如时间:03:04
let min = 3
let second = 4
let time = String(format: "%02d:%02d", arguments: [min, second])
字符串的截取
Swift中提供了特殊的截取方式
该方式非常麻烦
Index创建较为麻烦
简单的方式是将String转成NSString来使用
在标识符后加:as NSString即可
let myStr = "www.baidu.com"
var subStr = (myStr as NSString).substringFromIndex(4)
subStr = (myStr as NSString).substringToIndex(3)
subStr = (myStr as NSString).substringWithRange(NSRange(location: 4, length: 5))