1

How can I get the UTF8 bytesize of a string in Swift ?

For example, in Ruby I can do:

str = "hello"
str.bytesize
=> 5

What's the most efficient way to do this in Swift 3 ?

Graham Slick
  • 6,692
  • 9
  • 51
  • 87
  • 1
    Do you want the string length (character count)? See https://stackoverflow.com/a/24037759/1187415. If you want a "byte size" then you have to specify an encoding. – I don't know about Ruby, but in Swift a string is a collection of "extended grapheme clusters" (see e.g. https://stackoverflow.com/a/24037759/1187415), so "byte count" and "character count" can be quite different. – Martin R Jul 16 '17 at 16:57
  • @MartinR thanks, indeed I've adjusted my question, the encoding is UTF 8 – Graham Slick Jul 16 '17 at 17:01

0 Answers0