swift 字符串

利用 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?



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值