Calculate the size in bytes of a Swift String

后端 未结 1 1757
既然无缘
既然无缘 2021-01-02 07:09

I\'m trying to calculate the size in bytes of a String in Swift but I don\'t know what the size of a character is; in how many bytes is one coded?

Let\'

1条回答
  •  执笔经年
    2021-01-02 07:42

    It all depends on the character encoding, let's suppose UTF8:

    let string = "abde"
    let size = string.utf8.count
    

    Note that not all characters have the same byte size in UTF8. If your string is ASCII, you can assume 1 byte per character.

    0 讨论(0)
提交回复
热议问题