Swift中计算String的长度


 

 

extension String {
    var length: Int { return countElements(self) }  // Swift 1.1
}

extension String {
    var length: Int { return count(self)         }  // Swift 1.2
}

 

let globe = "" // U+1F30D EARTH GLOBE EUROPE-AFRICA
count(globe)     // -> 1

// The equivalent of NSString.length is counting the elements in the string's UTF-16 representation
count(globe.utf16) // -> 2

 

Ref:

http://stackoverflow.com/questions/24037711/get-the-length-of-a-string

http://oleb.net/blog/2014/07/swift-strings/


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM