获取字符串的长度

本文翻译自:Get the length of a String

How do you get the length of a String ? 你如何得到一个String的长度? For example, I have a variable defined like: 例如,我有一个定义如下的变量:

var test1: String = "Scott"

However, I can't seem to find a length method on the string. 但是,我似乎无法在字符串上找到长度方法。


#1楼

参考:https://stackoom.com/question/1crJ1/获取字符串的长度


#2楼

Swift 5+: 迅捷5+:

test1.count

(Swift 5.0 strings can, in some ways, simply be treated as an array. Hence you can use .count or for example .first ) (在某些方面,Swift 5.0字符串可以简单地视为一个数组。因此,您可以使用.count或例如.first


As of Swift 4 从Swift 4开始

It's just: 只是:

test1.count

for reasons. 原因。

(Thanks to Martin R) (感谢Martin R)

As of Swift 2: 从Swift 2开始:

With Swift 2, Apple has changed global functions to protocol extensions, extensions that match any type conforming to a protocol. 在Swift 2中,Apple已将全局功能更改为协议扩展,这些扩展可与符合协议的任何类型匹配。 Thus the new syntax is: 因此,新语法为:

test1.characters.count

(Thanks to JohnDifool for the heads up) (感谢JohnDifool的注意)

As of Swift 1 从Swift 1开始

Use the count characters method: 使用计数字符方法:

let unusualMenagerie = "Koala 🐨, Snail 🐌, Penguin 🐧, Dromedary 🐪"
println("unusualMenagerie has \(count(unusualMenagerie)) characters")
// prints "unusualMenagerie has 40 characters"

right from the Apple Swift Guide 直接来自Apple Swift指南

(note, for versions of Swift earlier than 1.2, this would be countElements(unusualMenagerie) instead) (请注意,对于早于1.2的Swift版本,它将countElements(unusualMenagerie)

for your variable, it would be 对于您的变量,它将是

length = count(test1) // was countElements in earlier versions of Swift

Or you can use test1.utf16count 或者您可以使用test1.utf16count


#3楼

You could try like this 你可以这样尝试

var test1: String = "Scott"
var length =  test1.bridgeToObjectiveC().length

#4楼

TLDR: TLDR:

For Swift 2.0 and 3.0, use test1.characters.count . 对于Swift 2.0和3.0,请使用test1.characters.count But, there are a few things you should know. 但是,您应该了解一些事情。 So, read on. 因此,请继续阅读。

Counting characters in Swift 在Swift中计数字符

Before Swift 2.0, count was a global function. 在Swift 2.0之前, count是一个全局函数。 As of Swift 2.0, it can be called as a member function. 从Swift 2.0开始,它可以被称为成员函数。

test1.characters.count

It will return the actual number of Unicode characters in a String , so it's the most correct alternative in the sense that, if you'd print the string and count characters by hand, you'd get the same result. 它会返回String中Unicode字符的实际数量,因此从某种意义上说,这是最正确的选择,如果您打印字符串并手工计算字符,您将获得相同的结果。

However, because of the way Strings are implemented in Swift, characters don't always take up the same amount of memory, so be aware that this behaves quite differently than the usual character count methods in other languages. 但是,由于在Swift中实现Strings的方式,字符并不总是占用相同的内存量,因此请注意,其行为与其他语言中的常规字符计数方法完全不同。

For example, you can also use test1.utf16.count 例如,您也可以使用test1.utf16.count

But, as noted below, the returned value is not guaranteed to be the same as that of calling count on characters . 但是,如下所述,不能保证返回的值与在characters上调用count的返回值相同。

From the language reference: 从语言参考:

Extended grapheme clusters can be composed of one or more Unicode scalars. 扩展字素簇可以由一个或多个Unicode标量组成。 This means that different characters—and different representations of the same character—can require different amounts of memory to store. 这意味着不同的字符(以及相同字符的不同表示形式)可能需要存储不同数量的内存。 Because of this, characters in Swift do not each take up the same amount of memory within a string's representation. 因此,Swift中的字符在字符串表示中不会各自占用相同数量的内存。 As a result, the number of characters in a string cannot be calculated without iterating through the string to determine its extended grapheme cluster boundaries. 结果,如果不对字符串进行迭代来确定其扩展的字素簇边界,就无法计算字符串中的字符数。 If you are working with particularly long string values, be aware that the characters property must iterate over the Unicode scalars in the entire string in order to determine the characters for that string. 如果使用特别长的字符串值,请注意,characters属性必须遍历整个字符串的Unicode标量,才能确定该字符串的字符。

The count of the characters returned by the characters property is not always the same as the length property of an NSString that contains the same characters. 由characters属性返回的字符数并不总是与包含相同字符的NSString的length属性相同。 The length of an NSString is based on the number of 16-bit code units within the string's UTF-16 representation and not the number of Unicode extended grapheme clusters within the string. NSString的长度基于字符串的UTF-16表示形式中16位代码单元的数量,而不是字符串中的Unicode扩展字素簇的数量。

An example that perfectly illustrates the situation described above is that of checking the length of a string containing a single emoji character, as pointed out by n00neimp0rtant in the comments. 可以很好地说明上述情况的示例是检查包含单个表情符号字符的字符串的长度,如n00neimp0rtant在注释中指出的那样。

var emoji = "👍"
emoji.characters.count             //returns 1
emoji.utf16.count                  //returns 2

#5楼

Here's something shorter, and more natural than using a global function: 这比使用全局函数更短,更自然:

aString.utf16count

I don't know if it's available in beta 1, though. 我不知道它是否在beta 1中可用。 But it's definitely there in beta 2. 但是肯定在beta 2中。


#6楼

Updated for Xcode 6 beta 4, change method utf16count --> utf16Count 已针对Xcode 6 beta 4更新,更改方法utf16count-> utf16Count

 var test1: String = "Scott"
 var length =  test1.utf16Count

Or 要么

 var test1: String = "Scott"
 var length = test1.lengthOfBytesUsingEncoding(NSUTF16StringEncoding)
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值