Monday, September 8, 2014

Removing diacritics (accents on letters) from strings in X++

If you want to remove diacritics (accents on letters) from strings like this "ÁÂÃÄÅÇÈÉàáâãäåèéêëìíîïòóôõ£ALEX" to use the more friendly string "AAAAAACEEaaaaaaeeeeiiiioooo£ALEX", you can use this block of code:

static void AlexRemoveDiacritics(Args _args)
    str strInput = 'ÁÂÃÄÅÇÈÉàáâãäåèéêëìíîïòóôõ£ALEX';
    System.String input = strInput;
    str retVal;
    int i;

    System.Char c;
    System.Text.NormalizationForm FormD = System.Text.NormalizationForm::FormD;
    str normalizedString = input.Normalize(FormD);
    System.Text.StringBuilder stringBuilder = new System.Text.StringBuilder();

    for (i = 1; i <= strLen(normalizedString); i++)
        c = System.Char::Parse(subStr(normalizedString, i, 1));

        if (System.Globalization.CharUnicodeInfo::GetUnicodeCategory(c) != System.Globalization.UnicodeCategory::NonSpacingMark)

    input = stringBuilder.ToString();
    input = input.Normalize();
    retVal = input;

    info(strFmt("Before: '%1'", strInput));
    info(strFmt("After: '%1'", retVal));

I did not come up with this code, I merely adapted it from who adapted it from somewhere another person who deleted their blog.  It is still very useful.