1、定义可变字符串:
var str = "hello,swift"
2、定义不可变字符串:
let str = "hello world"
3、获取字符串的长度:
let count = str.characters.count
4、遍历字符串:
var str = "hello,swift"
for c in str.characters{
print(c)
}
5、字符串拼接
let str1 = "shang"
let str2 = "hai"
let str3 = str1 + str2
let name = "jay"
let age = 12
let info = "his name is \(name),age is \(age)."
字符串的截取
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))
// 1.定义字符串
let str = "www.baidu.com"
// 2.截取开始位置
let fromIndex = str.startIndex.advancedBy(4)
let header = str.substringFromIndex(fromIndex)
// 3.截取结束位置
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)