Best approach to handle accented letters
Alfred Newman via Digitalmars-d-learn
digitalmars-d-learn at puremagic.com
Fri Oct 28 06:50:24 PDT 2016
On Friday, 28 October 2016 at 11:40:37 UTC, Chris wrote:
> 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"
> }
@Chris
As a new guy in the D community, I am not sure, but I think the
line below is something like a Python's lambda, right ?
auto removed = to!string(str.map!(a => (a in _accent) ?
_accent[a] : a));
Can you please rewrite the line in a more didatic way ? Sorry,
but I'm still learning the basics.
Thanks in advance
More information about the Digitalmars-d-learn
mailing list