5

I would like to get the number of bytes in a string using UTF-8 encoding without explicitly creating an array of bytes (because I do not need to use the array, just the number of bytes). Is this possible? My question is almost exactly this one but with C# instead of Java.

Thanks!

Walker in the City
  • 527
  • 1
  • 9
  • 22

1 Answers1

7

You can use the method GetByteCount to get the number of bytes that the string would produce with a given encoding.

var byteCount = System.Text.Encoding.UTF8.GetByteCount("myString");
Alexey Zimarev
  • 17,944
  • 2
  • 55
  • 83
Jehof
  • 34,674
  • 10
  • 123
  • 155