Friday, August 21, 2020

How to remove special characters from a string in AX 2012 using X++


Hi guys ,Today we see How to remove special characters from a string in Ax 2012 X++.

For this we have Multiple ways :
1.Using StrRem function.
2.Using Standard Class Replace method.
3 Developing some logic.

And 2 more we have.
4.Using strkeep function.
5.Using strAlpha fonction.

The above 2 functions will Remove all Special characters from  string.
In this blog I remove these special characters-'  ','/','_'(space,slash,Under score)
Just check Below code.

1.Using 'StrRem' function:

void clicked()
{
    str name = TextStringEdit.valueStr();
    name=strRem(name,' '+'/'+'_');

    info(name);
}


2.Using 'Standard Class Replace' method:

void clicked()
{
    str name = TextStringEdit.valueStr();
    name=System.Text.RegularExpressions.Regex::Replace(name, @"[/ _]", "");

    info(name);
}

3.Developing 'some logic':

void clicked()
{
    str name = TextStringEdit.valueStr();
    str replace(str _source, str _what, str _with)
    {
        int found = 0;
        str target = _source;
        ;
        do
        {
            found = strscan(target, _what, found, strlen(target));
              
            if (found != 0)
            {
                target = strdel(target, found, strlen(_what));
                target = strins(target, _with, found);
                found += strlen(_with);
            }
        } while (found != 0);
                return target;
    }
    ;

    name = replace(name, '/', "");
    name = replace(name, '_', "");
    name = replace(name, ' ', "");
    
    info(name);

}

4.Using 'strkeep'function:(It prints only A-Z)

void clicked()
{
    str name = TextStringEdit.valueStr();
    name=strkeep(TextStringEdit.text(), "ABCDEFGHIJKLMNOPQRSTUVWXYZ");

    info(name);
}


5.Using 'strAlpha' fonction:(It prints only Alpha Numeric values)

void clicked()
{
    str name = TextStringEdit.valueStr();
    name=strAlpha(TextStringEdit.valueStr());

    info(name);
}



Output:


The above screen shot is the output for 1,2,3.

Keep Daxing !!

No comments:

Post a Comment