String length: Difference between revisions

Content deleted Content added
→‎Character Length: raw utf8 example
Line 220: Line 220:
}</lang>
}</lang>


===Dealing with raw multibyte string===
''TODO: non-standard library calls for system multi-byte encodings, such as _mbcslen()''
Following code is written in UTF-8, and environment locale is assumed to be UTF-8 too. Note that "møøse" is here directly written in the source code for clarity, which is not a good idea in general. <code>mbstowcs()</code>, when passed NULL as the first argument, effectively counts the number of chars in given string under current locale.
<lang c>#include <stdio.h>
#include <stdlib.h>
#include <locale.h>

int main()
{
setlocale(LC_CTYPE, "");
char moose[] = "møøse";
printf("bytes: %d\n", sizeof(moose) - 1);
printf("chars: %d\n", (int)mbstowcs(0, moose, 0));

return 0;
}</lang>output<pre>bytes: 7
chars: 5</pre>


=={{header|C++}}==
=={{header|C++}}==