swift-字符串的拼接和遍历长度

2018-11-12  本文已影响63人  叩首问路梦码为生
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)
        }
      
     
        
    }
}


上一篇 下一篇

猜你喜欢

热点阅读