String length: Difference between revisions

Content added Content deleted
m (oct...)
Line 261: Line 261:


{{works with|C sharp|C #|1.0+}}
{{works with|C sharp|C #|1.0+}}
===Byte Length===
===Character Length===
<lang cpp>string s = "Hello, world!";
<lang cpp>string s = "Hello, world!";
int blength = System.Text.Encoding.GetBytes(s).length; // In Bytes.</lang>
int characterLength = s.Length;</lang>


===Byte Length===
Strings in .NET are stored in Unicode.
<lang cpp>using System.Text;


string s = "Hello, world!";
===Character Length===
int byteLength = Encoding.Unicode.GetByteCount(s);</lang>
<lang cpp>string s = "Hello, world!";
To get the number of bytes that the string would require in a different encoding, e.g., UTF8:
int clength = s.Length; // In characters</lang>
<lang cpp>int utf8ByteLength = Encoding.UTF8.GetByteCount(s);</lang>


=={{header|Clean}}==
=={{header|Clean}}==