首页 > 编程语言 > 详细

Swift中计算String的长度

时间:2015-05-10 09:36:48      阅读:4877      评论:0      收藏:0      [点我收藏+]

 

 

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/

Swift中计算String的长度

原文:http://www.cnblogs.com/zeyang/p/4491756.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!