Tuesday, 23 June 2020

Remove diacritics (accents) from a strings in AX/D365 X++

There will be some cases where you need to remove accents from the strings before you post the data to endpoints/API's. Otherwise they may reject during the process.

I found the below post what exactly I was looking for. I was posting the 3PL information to the warehouse endpoint and they were rejecting the files where there were some accents in the addresses/delivery names.


The below code helped me to solve the problem. Please change the code according to your requirement.



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)
        {
            stringBuilder.Append(c);
        }
    }

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

    retVal = System.Text.RegularExpressions.Regex::Replace(retVal, @"[^\w\.@-]", " ");// This
// will remove any non-printable ASCII characters from a string

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



Adding a newline into a string in C# and X++

Below is the sample code we can use for  adding a newline after every occurrence of "@" symbol in the string in C#   using System...