AIF – How to get rid of white-space in AX?

When you work with the AIF and xml files, you often need to handle so called “white-space”. The normal AX functions strltrim() and strrtrim() only handles normal space (ascii 32), these are two examples which handle all kinds of spaces in files. Small and simple, but very handy. 🙂

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
// --- Description: Axapta does not remove Tab,CR,LF and
// other characters below space (32), this method handles 
// both.
static str XmlTrim(str _line)
{
    #define.SPACE(32)
    int p=1, sz = strlen(_line);
    ;
 
    while(p <= sz && char2num(_line,p) <= #SPACE)
        p++;
    while(sz > p && char2num(_line,sz) <= #SPACE)
        sz--;
 
    return substr(_line,p,sz-p+1);
}
 
 
// --- Description: skip white-space even inside strings,
// needed for xml and imports.
static str XmlSkipWS(str line)
{
    #define.SPACE(32)
    str a='', ch, prev=' ';
    int j, sz=strlen(line);
    ;
    for(j=1; j <= sz; j++)
    {
        ch = substr(line,j,1);
        if(char2num(ch,1) <= #SPACE)
            ch = " "; // all chars below normal space 
                      // is considered white-space. You
                      // can change to TAB,CR,LF etc,
                      // but this works.
        if(ch != " ")
            a += ch;
        else if(prev != " ") // only add space once.
            a += " ";
        prev = ch;
 
    }
    return XmlTrim(a); // remove even possibly ending space.
}

Last 5 posts in Development

Leave a Reply

Your email address will not be published. Required fields are marked *