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+}} |
||
=== |
===Character Length=== |
||
<lang cpp>string s = "Hello, world!"; |
<lang cpp>string s = "Hello, world!"; |
||
int |
int characterLength = s.Length;</lang> |
||
⚫ | |||
Strings in .NET are stored in Unicode. |
|||
<lang cpp>using System.Text; |
|||
⚫ | |||
⚫ | |||
int byteLength = Encoding.Unicode.GetByteCount(s);</lang> |
|||
⚫ | |||
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}}== |