import UIKit
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}
func demo1() {
let str = "hello"
//1>返回指定编码的对应字节数量
//UTF8 的编码(0~4个) 每个汉字是3个字节
print(str.lengthOfBytes(using: .utf8))
// 字符串长度--返回字符的个数
print(str.characters.count)
// str as NSString 为强制转化类型 成字符串类型
}
func demo2() {
let str:String = "hello"
for c in str.characters {
print(c)
}
}
}
网友评论