Dynamics NAV Integration

Indexing a char in a string

by admin on dec.04, 2009, under Code

After working with Dynamics NAV (Navision) in five years, I can still be surprised by the small details.
I just did a new discovery that is even documented in the C/SIDE help :-)

I regularly use the COPYSTR function to extract substrings of a string. Even if it was only a single character that I wanted to substring.

When copying the charachter in position 5 … instead of using:

mySubString := COPYSTR(myFullString , 5, 1);

We can use:

mySubString := myFullString[5];

The result is the same, except from the latter where we actually extract a “char” datatype instead of a text string.
There can be circumstances where you have to FORMAT the char. For example if you want to MESSAGE it.

MESSAGE(myFullString[5]);

This won’t work because the MESSAGE function will only display the text datatype and not the char datatype.

This will work:

MESSAGE(FORMAT(myFullString[5]));

No matter if we assigned mySubstring to the char index [5] or return value of the COPYSTR this (below) will of course work.
There is an implicit datatype conversion from char to text when assigning mySubstring.

mySubString := myFullString[5];
MESSAGE(mySubstring);


2 Trackbacks / Pingbacks for this entry

Leave a Reply

Looking for something?

Use the form below to search the site:

Still not finding what you're looking for? Drop a comment on a post or contact us so we can take care of it!