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

#11
Swift 2.0:
Get a count: `yourString.text.characters.count`

Fun example of how this is useful would be to show a character countdown from some number (150 for example) in a UITextView:

func textViewDidChange(textView: UITextView) {
yourStringLabel.text = String(150 - yourStringTextView.text.characters.count)
}
Reply

#12
You can add this function to your extention

extension NSString {
func charLength() -> Int {
return count(self as String)
}
}
Reply

#13
in Swift 2.x the following is how to find the length of a string

let findLength = "This is a string of text"
findLength.characters.count

returns 24
Reply

#14
You could use SwiftString (

[To see links please register here]

) to do this.

```"string".length // 6```

DISCLAIMER: I wrote this extension
Reply

#15
test1.characters.count

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

ex:

test1 = "StackOverflow"

print(test1.characters.count)

(**prints** "***13***")
Reply

#16
You can use `str.utf8.count` and `str.utf16.count` which, I think, are the best solution
Reply

#17
Apple made it different from other major language. The current way is to call:


test1.characters.count

However, to be careful, when you say length you mean the count of characters not the count of bytes, because those two can be different when you use non-ascii characters.

For example;
`"你好啊hi".characters.count` will give you 5 but this is not the count of the bytes.
To get the real count of bytes, you need to do `"你好啊hi".lengthOfBytes(using: String.Encoding.utf8)`. This will give you 11.
Reply

#18

Right now (in Swift 2.3) if you use:

myString.characters.count

the method will return a "Distance" type, if you need the method to return an Integer you should type cast like so:

var count = myString.characters.count as Int

Reply

#19
You could try like this

var test1: String = "Scott"
var length = test1.bridgeToObjectiveC().length
Reply

#20
**tl;dr** If you want the length of a String type in terms of the number of human-readable characters, use **countElements()**. If you want to know the length in terms of the number of *extended grapheme clusters*, use **endIndex**. Read on for details.

The **String** type is implemented as an ordered collection (i.e., sequence) of Unicode characters, and it conforms to the **CollectionType** protocol, which conforms to the **_CollectionType** protocol, which is the input type expected by **countElements()**. Therefore, **countElements()** can be called, passing a **String** type, and it will return the count of characters.

However, in conforming to **CollectionType**, which in turn conforms to **_CollectionType**, **String** also implements the **startIndex** and **endIndex** computed properties, which actually represent the position of the index *before* the first character cluster, and position of the index *after* the last character cluster, respectively. So, in the string "ABC", the position of the index before A is 0 and after C is 3. Therefore, **endIndex** = 3, which is also the length of the string.

So, **endIndex** can be used to get the length of any String type, then, right?

Well, not always...Unicode characters are actually *extended grapheme clusters*, which are sequences of one *or more* Unicode scalars combined to create a single human-readable character.

let circledStar: Character = "\u{2606}\u{20DD}" // ☆⃝

**circledStar** is a single character made up of U+2606 (a white star), and U+20DD (a combining enclosing circle). Let's create a **String** from **circledStar** and compare the results of **countElements()** and **endIndex**.

let circledStarString = "\(circledStar)"
countElements(circledStarString) // 1
circledStarString.endIndex // 2
Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

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