Best approach to handle accented letters
Marc Schütz via Digitalmars-d-learn
digitalmars-d-learn at puremagic.com
Fri Oct 28 05:52:04 PDT 2016
On Friday, 28 October 2016 at 11:24:28 UTC, Alfred Newman wrote:
> Hello,
>
> I'm getting some troubles to replace the accented letters in a
> given string with their unaccented counterparts.
>
> Let's say I have the following input string "très élégant" and
> I need to create a function to return just "tres elegant".
> Considering we need to take care about unicode chars, what is
> the best way to write a D code to handle that ?
>
> Cheers
import std.stdio;
import std.algorithm;
import std.uni;
import std.conv;
void main()
{
auto str = "très élégant";
immutable accents = unicode.Diacritic;
auto removed = str
.normalize!NFD
.filter!(c => !accents[c])
.to!string;
writeln(removed); // prints "tres elegant"
}
This first decomposes all characters into base and diacritic, and
then removes the latter.
More information about the Digitalmars-d-learn
mailing list