How to Count characters in a String in Swift 4?

By | December 20, 2017

To retrieve a count of the characters in a string, call the global countElements function and pass in a string as the function’s sole parameter:

  let sampleString = "Koala, Snail, Penguin, Dromedary"
  print("sampleString has \(countElements(sampleString)) characters")

prints “sampleString has 32 characters”

Note :

Different Unicode characters and different representations of the same Unicode 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 length of a string cannot be calculated without iterating through the string to consider each of its characters in turn. If you are working with particularly long string values, be aware that the countElements function must iterate over the characters within a string in order to calculate an accurate character count for that string.

Note also that the character count returned by countElements 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 characters within the string. To reflect this fact, the length property from NSString is called utf16count when it is accessed on a Swift String value.

Leave a Reply

Your email address will not be published. Required fields are marked *