Converting a character to upper case in string
Laurent Tréguier
laurent.treguier.sink at gmail.com
Fri Sep 21 14:54:51 UTC 2018
On Friday, 21 September 2018 at 13:32:54 UTC, NX wrote:
> On Friday, 21 September 2018 at 12:34:12 UTC, Laurent Tréguier
> wrote:
>> I would probably go for std.utf.decode [1] to get the
>> character and its length in code units, capitalize it, and
>> concatenate the result with the rest of the string.
>>
>> [1] https://dlang.org/phobos/std_utf.html#.decode
>
> So by this I assume it is sufficient to work with dchars rather
> than graphemes?
----------
import std.stdio;
import std.conv;
import std.string;
import std.uni;
size_t index = 1;
auto theString = "he\u0308llo, world";
auto theStringPart = theString[index .. $];
auto firstLetter = theStringPart.decodeGrapheme;
auto result = theString[0 .. index]
~ capitalize(firstLetter[].text)
~ theString[index + graphemeStride(theString, index) .. $];
writeln(result);
----------
This will capitalize graphemes as a whole, and might be better
than what I previously wrote.
More information about the Digitalmars-d-learn
mailing list