Swift 获得字符串String长度

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/bitcser/article/details/54970359

在Objective-C中我们通常使用length来获取NSString类型的字符串的长度,而在Swift中,String结构体没有了这个方法,那怎样获取String类型的字符串的长度呢?我们可以通过他的扩展属性成员characterscount属性来获取

let titleString : String = "title"
let count = titleString.characters.count  //获取String字符串的长度
print(count)

注意:在xcode9.0中,swift4.0 直接去掉characters就可以了例如 titleString.count

猜你喜欢

转载自blog.csdn.net/bitcser/article/details/54970359