Remove diacritics (accents) from a strings in AX/D365 X++
Views (654)
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;
info(strFmt("Before: '%1'", strInput));
info(strFmt("After: '%1'", retVal));
}
This was originally posted here.
*This post is locked for comments