let unusualMenagerie = "Koala 🐨, Snail 🐌, Penguin 🐧, Dromedary 🐪"println("unusualMenagerie has \(count(unusualMenagerie)) characters")// prints "unusualMenagerie has 40 characters"
//: Playground - noun: a place where people can play
import UIKit
var str = " He\u{2606} "count(str) // 7
let length = count(str.stringByTrimmingCharactersInSet(NSCharacterSet.whitespaceAndNewlineCharacterSet())) as Int // 3println(length == 3) // true
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"
/*** 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)}}
extension String {// MARK: Use if it's Swift 2func stringLength(str: String) -> Int {return str.characters.count}
// MARK: Use if it's Swift 3func stringLength(_ str: String) -> Int {return str.characters.count}
// MARK: Use if it's Swift 4func stringLength(_ str: String) -> Int {return str.count}}
//In Swiftlet stringLenght = "This is my String"print(stringLenght.count)
//In Objective cNSString * stringLenght = @"This is my String";NSLog(@"%lu", stringLenght.length);
let strLength = "This is my string"print(strLength.count)//print(strLength.characters.count) //Error: 'characters' is deprecated: Please use String or Substring directly