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

#21
In Xcode 6.1.1

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


I think that brainiacs will change this on every minor version.
Reply

#22
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
Reply

#23
Here is what I ended up doing

let replacementTextAsDecimal = Double(string)

if string.characters.count > 0 &&
replacementTextAsDecimal == nil &&
replacementTextHasDecimalSeparator == nil {
return false
}
Reply

#24
**Swift 4**

`"string".count`

;)

**Swift 3**

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

usage

`"string".length`
Reply

#25
**Swift 4 update comparing with swift 3**

Swift 4 removes the need for a characters array on String. This means that you can directly call `count` on a string without getting characters array first.

"hello".count // 5
Whereas in swift 3, you will have to get characters array and then count element in that array. Note that this following method is still available in swift 4.0 as you can still call `characters` to access characters array of the given string

"hello".characters.count // 5

Swift 4.0 also adopts Unicode 9 and it can now interprets grapheme clusters. For example, counting on an emoji will give you 1 while in swift 3.0, you may get counts greater than 1.

"👍🏽".count // Swift 4.0 prints 1, Swift 3.0 prints 2
"👨‍❤️‍💋‍👨".count // Swift 4.0 prints 1, Swift 3.0 prints 4
Reply

#26
#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][1] in the comments.

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

[1]:

[To see links please register here]

Reply

#27
my two cents for swift 3/4

If You need to conditionally compile

#if swift(>=4.0)
let len = text.count
#else
let len = text.characters.count
#endif


Reply

#28
`test1.endIndex` gives the same result as `test1.characters.count` on Swift 3
Reply

#29
### Universal Swift 4 and 3 solution

/**
* Since swift 4 There is also native count, But it doesn't return Int
* NOTE: was: var count:Int { return self.characters.count }
* EXAMPLE: "abc👌".count//Output: 4
*/
extension String{
var count:Int {
return self.distance(from: self.startIndex, to: self.endIndex)
}
}
Reply

#30
Swift 4

let str = "Your name"

str.count

Remember: Space is also counted in the number
Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

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