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
Constant Unassigned Optional Will Not Be Nil by Default
Swift Variable Name With ' (Backtick)
How to Check If a Firebase Database Value Exists
Get a Swift Variable'S Actual Name as String
Firebase Getting Data in Order
Saving Cgimageref to a Png File
Lidar and Realitykit - Capture a Real World Texture For a Scanned Model
Passing Lists from One Function to Another in Swift
Add Local Notification in iOS 10 - Swift 3
Ios 11 Custom Navbar Goes Under Status Bar
Swift Beta 6 - Confusing Linker Error Message
Swift Generics Not Preserving Type
Swift Protocol Inheritance and Protocol Conformance Issue
Swipe-Able Table View Cell in iOS 9
Swift 2.0: 'Enumerate' Is Unavailable: Call the 'Enumerate()' Method on the Sequence