字符串
var str:String = "hello"
let str2 = String("hello")
var emptyString = ""
var emptyString2 = String()
str.isEmpty
emptyString.isEmpty
let mark = "!!!"
str + mark//只加,str没有改变
str
str += mark//加了之后赋值给str,str改变了
str
let name = "lulu"
let age = 12
let height = 1.66
let s = "My name is \(name), My age is \(age), My height is \(height)"
print(s)
//反斜杠\--转义字符
//character
var str = "hello, playground"
for c in str.characters {
print(c)
}
let mark:Character = "!"
str + String(mark)
str
str.append(mark)
str
//unicode
let englishCharacter:Character = "a"
let chineseCharacter:Character = "你"
let dog:Character = "🐶"
let coolGuy:Character = "\u{1F60E}"
var coolLetters = "abc"
coolLetters.characters.count
var chineseLetters = "你好"
chineseLetters.characters.count
var coolGuys = "\u{1F60E}\u{1F60E}\u{1F60E}"
coolGuys.characters.count
var cafe = "café"
var cafe2 = "cafe\u{0301}"
cafe.characters.count
cafe2.characters.count
cafe == cafe2
var str = "hello,swift"
let startIndex = str.startIndex
startIndex
str[startIndex]
str[startIndex.advancedBy(5)]
let spaceIndex = startIndex.advancedBy(7)
spaceIndex
str[spaceIndex]
str[spaceIndex.predecessor()]
str[spaceIndex.successor()]
let endIndex = str.endIndex
endIndex
//str[endIndex]//前闭后开
str[endIndex.predecessor()]
str[startIndex..<endIndex]
str[startIndex..<endIndex.predecessor()]
let range = startIndex..<spaceIndex.predecessor()
str.replaceRange(range, with: "a")
str.appendContentsOf("!!!")
str.uppercaseString
str.lowercaseString
str.capitalizedString
str.containsString("sw")
str.hasSuffix("A")
str.hasPrefix("o")
let s = "one third is \(1.0/3.0)"
let s2 = NSString(format: "one third is %.2f", 1.0/3.0)
//s2--NSString--->String
let s3:String = NSString(format: "one third is %.2f", 1.0/3.0) as String
print(s3)
var s4:NSString = "one third is 0.33"
s4.substringFromIndex(4)
s4.substringToIndex(3)
s4.substringWithRange(NSMakeRange(4, 5))
let s6 = " ----hello--- " as NSString
s6.stringByTrimmingCharactersInSet(NSCharacterSet(charactersInString: " -"))//去除冗余