How convert String to Fixed wchar Array?

Steven Schveighoffer schveiguy at
Sat Oct 17 01:40:07 UTC 2020

On 10/16/20 8:23 PM, Marcone wrote:
> How convert String to Fixed wchar Array?
> import std;
> void main(){
>      string name = "Marvin";
>      wchar[255] wtext =!(wchar[]); // Can not convert.
>      writeln(wtext);
> }

void main()
     string name = "Marvin";
     wchar[255] wtext;
     import std.range;

     wchar[] buf = wtext[];
     put(buf, name);
     auto setLen = wtext.length - buf.length; // number of wchars set in 
     writeln(wtext[0 .. setLen]);

A little bit of explanation: wchar[] is an output range that can accept 
any form of text. When you use `put` on it, it writes to it, AND shrinks 
from the front to allow remembering where it was. So at the end, buf is 
pointing at what is *left* in the buffer.

There may be better ways to do this, but this way will avoid any extra 
GC allocation.


More information about the Digitalmars-d-learn mailing list