size of a string in bytes

Nestor via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Sat Jan 28 10:04:58 PST 2017


On Saturday, 28 January 2017 at 16:01:38 UTC, Ivan Kazmenko wrote:
> As said, the byte count is indeed string.length.
> The number of code points can be found by std.range.walkLength, 
> but be aware it takes O(answer) time to compute.
>
> Example:
>
> -----
> import std.range, std.stdio;
> void main () {
> 	auto s = "Привет!";
> 	writeln (s.length); // 13 bytes
> 	writeln (s.walkLength); // 7 code points
> }

Thank you Ivan,

I believe I saw somewhere that in D a char was not neccesarrily 
the same as an ubyte because chars sometimes take more than one 
byte, so since a string is an array of chars, I thought length 
behaved like walkLength (which I had not seen), in other words, 
that it simply returned the amount of elements in the array.


More information about the Digitalmars-d-learn mailing list