Get the length of a String

2019-01-01 10:03发布

How do you get the length of a 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.

标签: swift string
30条回答
永恒的永恒
2楼-- · 2019-01-01 10:05

In swift4 I have always used string.count till today I have found that

string.endIndex.encodedOffset

is the better substitution because it is faster - for 50 000 characters string is about 6 time faster than .count. The .count depends on the string length but .endIndex.encodedOffset doesn't.

But there is one NO. It is not good for strings with emojis, it will give wrong result, so only .count is correct.

查看更多
若你有天会懂
3楼-- · 2019-01-01 10:07
test1.characters.count

will get you the number of letters/numbers etc in your string.

ex:

test1 = "StackOverflow"

print(test1.characters.count)

(prints "13")

查看更多
若你有天会懂
4楼-- · 2019-01-01 10:10

TLDR:

For Swift 2.0 and 3.0, use test1.characters.count. But, there are a few things you should know. So, read on.

Counting characters in Swift

Before Swift 2.0, count was a global function. As of Swift 2.0, it can be called as a member function.

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.

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.

For example, you can also use test1.utf16.count

But, as noted below, the returned value is not guaranteed to be the same as that of calling count on characters.

From the language reference:

Extended grapheme clusters can be composed of one or more Unicode scalars. 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. 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.

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. 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.

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.

var emoji = "                                                                    
查看更多
与风俱净
5楼-- · 2019-01-01 10:10

Swift 4

"string".count

;)

Swift 3

extension String {
    var length: Int {
        return self.characters.count
    }
}

usage

"string".length

查看更多
临风纵饮
6楼-- · 2019-01-01 10:10

You could try like this

var test1: String = "Scott"
var length =  test1.bridgeToObjectiveC().length
查看更多
情到深处是孤独
7楼-- · 2019-01-01 10:11

Get string value from your textview or textfield:

let textlengthstring = (yourtextview?.text)! as String

Find the count of the characters in the string:

let numberOfChars = textlength.characters.count
查看更多
登录 后发表回答