View source code
Display the source code in std/utf.d from which this page was generated on github.
Report a bug
If you spot a problem with this page, click here to create a Bugzilla issue.
Improve this page
Quickly fork, edit online, and submit a pull request for this page. Requires a signed-in GitHub account. This works well for small changes. If you'd like to make larger changes you may want to consider using local clone.

Function std.utf.toUTF16

Encodes the elements of s to UTF-16 and returns a newly GC allocated wstring of the elements.

wstring toUTF16(S) (
  S s
)
if (isInputRange!S && !isInfinite!S && isSomeChar!(ElementEncodingType!S));

Parameters

NameDescription
s the range to encode

Returns

A UTF-16 string

See Also

For a lazy, non-allocating version of these functions, see byUTF.

Example

import std.algorithm.comparison : equal;

// these graphemes are two code units in UTF-16 and one in UTF-32
writeln("𤭢"d.length); // 1
writeln("𐐷"d.length); // 1

assert("𤭢"d.toUTF16.equal([0xD852, 0xDF62]));
assert("𐐷"d.toUTF16.equal([0xD801, 0xDC37]));

Authors

Walter Bright and Jonathan M Davis

License

Boost License 1.0.