size of a string in bytes

Ivan Kazmenko via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Sat Jan 28 08:01:38 PST 2017


On Saturday, 28 January 2017 at 15:32:33 UTC, Nestor wrote:
> I want to know variable size in memory. For example, say I have 
> an UTF-8 string of only 2 characters, but each of them takes 2 
> bytes. string length would be 2, but the content of the string 
> would take 4 bytes in memory (excluding overhead for type size).

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
}
-----

Ivan Kazmenko.



More information about the Digitalmars-d-learn mailing list