How to covert dchar and wchar to string?

Ferhat Kurtulmuş aferust at gmail.com
Mon Jan 25 19:15:34 UTC 2021


On Monday, 25 January 2021 at 18:45:11 UTC, Rempas wrote:
> Actually what the title says. For example I have dchar c = 
> '\u03B3'; and I want to make it into string. I don't want to 
> use "to!string(c);". Any help?

if you are trying to avoid GC allocations this is not what you 
want.

     dchar c = '\u03B3';

     string s = "";
     s ~= c;

     writeln(s);
     writeln(s.length); // please aware of this

Some useful things:

string is immutable(char)[]
wstring is immutable(wchar)[]
dstring is immutable(dchar)[]

if you have a char[]:
you can convert it to a string using assumeUnique:

import std.exception: assumeUnique;

char[] ca = ...

string str = assumeUnique(ca); // similar for dchar->dstring and 
wchar->wstring



More information about the Digitalmars-d-learn mailing list