Swift: Discover the surprisingly consistent Swift string length solution

Here is the test function:

func testString(string : String)
{
    var result : Int
       println("Testing \"\(string)\"")
    result = string.bridgeToObjectiveC().length
       println("string.bridgeToObjectiveC().length : \(result)")
    result = (string as NSString).length
       println("(string as NSString).length : \(result)")
    result = countElements(string)
       println("countElements(string) : \(result)")
    result = string.lengthOfBytesUsingEncoding(NSUTF8StringEncoding)
       println("string.lengthOfBytesUsingEncoding(NSUTF8StringEncoding) : \(result)")
    result = string.lengthOfBytesUsingEncoding(NSUTF16StringEncoding)
       println("string.lengthOfBytesUsingEncoding(NSUTF16StringEncoding) : \(result)")
    result = string.utf16count
       println("string.utf16count : \(result)")
    var manager = NSLayoutManager()
    let storage = NSTextStorage(string:string)
    manager.textStorage = storage
    result = manager.numberOfGlyphs
       println("layoutManager.numberOfGlyphs : \(result)")
       println("")
}

Here are the results. (It’s a picture not text because my CMS kept barfing at the special characters. Sorry.)
Screen Shot 2014-06-11 at 7.23.22 PM

This snippet by Fishrock123,  on the other hand, is just awesome. As he describes it, the code “[c]ounts the average byte length in a unicode string. (lol)”

Thanks especially to Simon Yang, August Joki, Phil Holland, Daniel Jalkut, Ken FerryFishrock123 (Jeremiah Senkpiel)

3 Comments

  • The countElements gives me a great help. Thanks!

  • […] while back, I posted about counting strings in Swift.  I discovered that countElements() offered the most consistently correct results regardless of […]

  • You are missing out an interesting example with combining diacritical mark “15uu{308}” instead of “15u{fc}”:

    Testing “15ü”
    (string as NSString).length : 4
    countElements(string) : 3
    string.lengthOfBytesUsingEncoding(NSUTF8StringEncoding) : 5
    string.lengthOfBytesUsingEncoding(NSUTF16StringEncoding) : 8
    string.utf16Count : 4
    layoutManager.numberOfGlyphs : 4