Best approach to handle accented letters
Chris via Digitalmars-d-learn
digitalmars-d-learn at puremagic.com
Fri Oct 28 04:40:37 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
You could try something like this. It works for accents. I
haven't tested it on other characters yet.
import std.stdio;
import std.algorithm;
import std.array;
import std.conv;
enum
{
dchar[dchar] _accent = ['á':'a', 'é':'e', 'è':'e', 'í':'i',
'ó':'o', 'ú':'u', 'Á':'A', 'É':'E', 'Í':'I', 'Ó':'O', 'Ú':'U']
}
void main()
{
auto str = "très élégant";
auto removed = to!string(str.map!(a => (a in _accent) ?
_accent[a] : a));
writeln(removed); // prints "tres elegant"
}
More information about the Digitalmars-d-learn
mailing list