String length: Difference between revisions

m (oct...)
Line 261:
 
{{works with|C sharp|C #|1.0+}}
===ByteCharacter Length===
<lang cpp>string s = "Hello, world!";
int blengthcharacterLength = System.Text.Encoding.GetBytes(s).lengthLength; // In Bytes.</lang>
 
===CharacterByte Length===
Strings in .NET are stored in Unicode.
<lang cpp>using System.Text;
 
<lang cpp>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}}==
Anonymous user