var str = "hello,swift"
for c in str.characters{
print(c)
}
5肩狂、字符串拼接
兩個字符串的拼接:
let str1 = "shang"
let str2 = "hai"
let str3 = str1 + str2
字符串與其他數(shù)據(jù)類型的拼接:
let name = "jay"
let age = 12
let info = "his name is \(name),age is \(age)."
字符串的截取
方式一:將string轉(zhuǎn)成NSString
let str = "www.baidu.com"
var substr = (str as NSString).substringFromIndex(4)
substr = (str as NSString).substringToIndex(3)
substr = (str as NSString).substringWithRange(NSRange(location:4,length:5))
方式二:swift截取
// 1.定義字符串
let str = "www.baidu.com"
// 2.截取開始位置
let fromIndex = str.startIndex.advancedBy(4)
let header = str.substringFromIndex(fromIndex)
// 3.截取結(jié)束位置
let toIndex = str.endIndex.advancedBy(-3)
let footer = str.substringToIndex(toIndex)
// 4.截取中間的字符串
let range = Range(start: str.startIndex.advancedBy(4), end: str.endIndex.advancedBy(-3))
let middle = str.substringWithRange(range)