利用 for-in 循环来遍历字符串中的每一个字符:
for character in "hello word!" {
println(character)
}
使用countElements() 函数计算字符串长度
println(countElements("hello word!"))
使用hasPrefix 和 hasSuffix 方法来判断字符串的前缀和后缀是否相等
let romeoAndJuliet = [
"Act 1 Scene 1: Verona, A public place",
"Act 1 Scene 2: Capulet's mansion",
"Act 1 Scene 3: A room in Capulet's mansion",
"Act 1 Scene 4: A street outside Capulet's mansion",
"Act 1 Scene 5: The Great Hall in Capulet's mansion",
"Act 2 Scene 1: Outside Capulet's mansion",
"Act 2 Scene 2: Capulet's orchard",
"Act 2 Scene 3: Outside Friar Lawrence's cell",
"Act 2 Scene 4: A street in Verona",
"Act 2 Scene 5: Capulet's mansion",
"Act 2 Scene 6: Friar Lawrence's cell"
]
var act = 0
var mansion = 0
for scene in romeoAndJuliet {
if scene.hasPrefix("Act 1 ") {
++act
}
if scene.hasSuffix("Capulet's mansion"){
++mansion;
}
}
println("act: \(act) sapuletMansion: \(mansion)");
使用 uppercaseString 和 lowercaseString 属性访问一个字符串的大、小写版本
let normal = "Could you help me, please?"
println(normal.uppercaseString)
println(normal.lowercaseString)
//
//COULD YOU HELP ME, PLEASE?
//could you help me, please?