How to know the size of the string in bytes?

user705414 picture user705414 · Jan 3, 2012 · Viewed 137.7k times · Source

I'm wondering if I can know how long in bytes for a string in C#, anyone know?

Answer

diya picture diya · Jan 3, 2012

You can use encoding like ASCII to get a character per byte by using the System.Text.Encoding class.

or try this

  System.Text.ASCIIEncoding.Unicode.GetByteCount(string);
  System.Text.ASCIIEncoding.ASCII.GetByteCount(string);