Create an account

Very important

  • To access the important data of the forums, you must be active in each forum and especially in the leaks and database leaks section, send data and after sending the data and activity, data and important content will be opened and visible for you.
  • You will only see chat messages from people who are at or below your level.
  • More than 500,000 database leaks and millions of account leaks are waiting for you, so access and view with more activity.
  • Many important data are inactive and inaccessible for you, so open them with activity. (This will be done automatically)


Thread Rating:
  • 682 Vote(s) - 3.5 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Get the length of a String

#31
In <b>Swift 4</b> :
If the string does not contain unicode characters then use the following

let str : String = "abcd"
let count = str.count // output 4

If the string contains unicode chars then use the following :

let spain = "España"
let count1 = spain.count // output 6
let count2 = spain.utf8.count // output 7
Reply

#32
You can get the length simply by writing an extension:

extension String {
// MARK: Use if it's Swift 2
func stringLength(str: String) -> Int {
return str.characters.count
}

// MARK: Use if it's Swift 3
func stringLength(_ str: String) -> Int {
return str.characters.count
}

// MARK: Use if it's Swift 4
func stringLength(_ str: String) -> Int {
return str.count
}
}
Reply

#33
In Swift 2.0 `count` doesn't work anymore. You can use this instead:

var testString = "Scott"
var length = testString.characters.count
Reply

#34
In Swift 4.1 and Xcode 9.4.1

To get length in Objective c and Swift is different. In Obj-c we use **length** property, but in Swift we use **count** property

Example :

//In Swift
let stringLenght = "This is my String"
print(stringLenght.count)

//In Objective c
NSString * stringLenght = @"This is my String";
NSLog(@"%lu", stringLenght.length);
Reply

#35
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.
Reply

#36
<strike>Swift 1.2 Update: There's no longer a countElements for counting the size of collections. Just use the count function as a replacement: count("Swift")</strike>

<strike> Swift 2.0, 3.0 and 3.1:
> let strLength = string.characters.count
</strike>

Swift 4.2 (4.0 onwards): [[Apple Documentation - Strings]][1]

> let strLength = string.count

[1]:

[To see links please register here]

Reply

#37



**Swift 1.1**

extension String {
var length: Int { return countElements(self) } //
}



***
**Swift 1.2**

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

***
**Swift 2.0**

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

***
**Swift 4.2**

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


***


let str = "Hello"
let count = str.length // returns 5 (Int)
Reply

#38
In Swift 4.2 and Xcode 10.1

In Swift strings can be treated like an array of individual characters. So each character in string is like an element in array. To get the length of a string use **yourStringName.count** property.

In **Swift**

`yourStringName.characters.count` property in deprecated. So directly use `strLength.count` property.



let strLength = "This is my string"
print(strLength.count)
//print(strLength.characters.count) //Error: 'characters' is deprecated: Please use String or Substring directly
If **Objective C**

NSString *myString = @"Hello World";
NSLog(@"%lu", [myString length]); // 11
Reply

#39
Swift 5.0 strings can be treated as an array of individual characters. So, to return the length of a string you can use **yourString.count** to count the number of items in the characters array.
Reply

#40
Swift 5.1, 5
---------

let flag = "🇵🇷"

print(flag.count)
// Prints "1" -- Counts the characters and emoji as length 1

print(flag.unicodeScalars.count)
// Prints "2" -- Counts the unicode lenght ex. "A" is 65

print(flag.utf16.count)
// Prints "4"

print(flag.utf8.count)
// Prints "8"

Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

©0Day  2016 - 2023 | All Rights Reserved.  Made with    for the community. Connected through