On Wednesday, 14 August 2013 at 03:00:00 UTC, Jonathan M Davis
wrote:
On Wednesday, August 14, 2013 04:53:34 jicman wrote:
Greetings.
import std.stdio;
void main()
{
char[] str = "不良反應事件和產品客訴報告"; // 13 chinese characters...
writefln(str.length);
}
this program returns 39. I expected to return 13. How do I know
the exact length of the characters that I have in a char[]
variable? Thanks.
length gives you the length of the array, which is 39, because
it contains 39
chars. If you want to know the number of code points in the
string as opposed
to the number of code units (char is a UTF-8 code unit), then
use
std.range.walkLength. e.g.
writeln(walkLength(str));
It'll iterate through the string and count up the number of
code points.
- Jonathan M Davis
thanks, Jonathan. That looks like D2, since D1 does not have
std.range in its phobos library.