Alex
Alex

Reputation: 29

regex.replace c# diacritics

I'm trying to replace strings with diacritics, but no luck. I need to replace, for example "Ю" and "ю" to "yu", "Б" and "б" to "b" and so on with my own table. I have a such code, which is not working:

case "LastRUEN":
                if (csentry["LAST"].IsPresent)
                {
                    string FIELD_RU = csentry["LAST"].Value;
                    string FIELD_EN;
                    FIELD_EN = Regex.Replace(FIELD_RU, "[Аа]", "a");
                    FIELD_EN = Regex.Replace(FIELD_RU, "[Бб]", "b");
                    FIELD_EN = Regex.Replace(FIELD_RU, "[Вв]", "v");
                    FIELD_EN = Regex.Replace(FIELD_RU, "[Гг]", "h");
                    FIELD_EN = Regex.Replace(FIELD_RU, "[Ґґ]", "g");
                    FIELD_EN = Regex.Replace(FIELD_RU, "[Дд]", "d");
                    FIELD_EN = Regex.Replace(FIELD_RU, "[Ее]", "e");
                    mventry["lastNameEN"].Value = FIELD_EN;
}
break;

Can anybody hepl with troubleshooting? Maybe it would be better to use a method for my case? Thanks!

Upvotes: 0

Views: 451

Answers (1)

Ralf Bönning
Ralf Bönning

Reputation: 15415

This should work better, because the result of the Replace calls will be the input of the next operation.

case "LastRUEN":
            if (csentry["LAST"].IsPresent)
            {
                string FIELD_RU = csentry["LAST"].Value;
                string FIELD_EN;
                FIELD_EN = Regex.Replace(FIELD_RU, "[Аа]", "a");
                FIELD_EN = Regex.Replace(FIELD_EN , "[Бб]", "b");
                FIELD_EN = Regex.Replace(FIELD_EN , "[Вв]", "v");
                FIELD_EN = Regex.Replace(FIELD_EN , "[Гг]", "h");
                FIELD_EN = Regex.Replace(FIELD_EN , "[Ґґ]", "g");
                FIELD_EN = Regex.Replace(FIELD_EN , "[Дд]", "d");
                FIELD_EN = Regex.Replace(FIELD_EN , "[Ее]", "e");
                mventry["lastNameEN"].Value = FIELD_EN;
}
break;

And of course you should move the operations into a separate method.

EDIT: Replace Multiple Characters in a String is interessing to combine the multiple calls.

Upvotes: 3

Related Questions