swift学习Day03

字符串和字符

今天继续学习swift中的字符串和字符

string类型是字符类型的一个集合,在swift中string类型是值类型。
声明空的字符串:

var emptyStr = ""
var anotheremptyStr = String()

字符串是可变的,可以通过 连接。

遍历字符串中的字符:

for c in "Char!?.".characters {
    print(c)
}

字符和字符串互转:

let chas:[Character] = ["a", "b", "c"]
let str = String(chas)
print(str)

字符串判断相等: ==, 不等: !=


let aa = [
    "act 1, 1",
    "act 1, 2",
    "act 1, 3",
    "act 2, 1",
    "act 3, 1"
]

var preCount = 0
var sufCount = 0
for value in aa {
    if value.hasPrefix("act 1") {
        preCount++
    }

    if value.hasSuffix("1") {
        sufCount++
    }
}

print(preCount)
print(sufCount)

hasPrefix和hasSuffix判断字符串的前缀和后缀是否相等。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值