String Length in Swift 1.2 and Swift 2.0

String length in Swift 1.2 and Swift 2.0

You can use extension for it like:

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

and you can use it:

if mystr.length >= 3 {

}

Or you can directly count this way:

if count(mystr) >= 3{

}

And this is also working for me :

if count(mystr.utf16) >= 3 {

}

For Swift 2.0:

extension String {
var length: Int {
return characters.count
}
}
let str = "Hello, World"
str.length //12

Another extension:

extension String {
var length: Int {
return (self as NSString).length
}
}
let str = "Hello, World"
str.length //12

If you want direct use:

let str: String = "Hello, World"
print(str.characters.count) // 12

let str1: String = "Hello, World"
print(str1.endIndex) // 12

let str2 = "Hello, World"
NSString(string: str2).length //12

String length Swift

Just use count()

count(textView.text)

in swift 2.0 some of the stuff you mentioned works =]

Get the length of a String

As of Swift 4+

It's just:

test1.count

for reasons.

(Thanks to Martin R)

As of Swift 2:

With Swift 2, Apple has changed global functions to protocol extensions, extensions that match any type conforming to a protocol. Thus the new syntax is:

test1.characters.count

(Thanks to JohnDifool for the heads up)

As of 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

(note, for versions of Swift earlier than 1.2, this would be countElements(unusualMenagerie) instead)

for your variable, it would be

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

Or you can use test1.utf16count

Get the length of a String

As of Swift 4+

It's just:

test1.count

for reasons.

(Thanks to Martin R)

As of Swift 2:

With Swift 2, Apple has changed global functions to protocol extensions, extensions that match any type conforming to a protocol. Thus the new syntax is:

test1.characters.count

(Thanks to JohnDifool for the heads up)

As of 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

(note, for versions of Swift earlier than 1.2, this would be countElements(unusualMenagerie) instead)

for your variable, it would be

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

Or you can use test1.utf16count

length (byte-wise) of a string in swift 2.2

I think type casting is another easier way to use the methods, properties of NSString class.

print((str as NSString).length)

Swift string length strange behavior

Your "asdf" literal is a __NSCFString instance. NSString does indeed have a length method.

"asdf".className
--> "__NSCFString"

Swift 2.0 substringwithrange

Try this:

let myString = "full text container"
myString[myString.startIndex..<myString.startIndex.advancedBy(3)]


Related Topics



Leave a reply



Submit