I recently looked into a support incident where there were some problems when importing text data from an XML file into NAV. One of the problems was that in the xml file the string contained a lot of useless extra spaces.
It looked somehow similar to this:
'Lorem ipsum began as scrambled , nonsensical Latin derived from Cicero 1st-century BC text De Finibus Bonorum et Malorum . '
So, in order to fix this, I created a codeunit “String Mgt.” and a small function that takes as a parameter an InputString and returns a string without the useless spaces.
The logic of this small algorithm is simple:
–> We create a new string and copy to it characters from the Input string;
–> We look at each character in the Inputstring and:
* if the character is a ‘normal’ one (letter, number etc) we copy it to the result string.
* if the character is an empty space, then we look at the next character. If the next character is also an empty space or a special character (ex: point, comma, exclamation mark, question mark etc), we do not copy the current character to the result string. Else, we copy it.
Simple as that 🙂
The code looks like this:
ReduceSpaces(InputString : Text) OutputString : Text n := STRLEN(InputString); FOR i := 1 TO n DO IF (InputString[i] = ' ') AND (i < n) THEN BEGIN IF NOT (InputString[i+1] IN [32..47,58..63]) THEN //if the next char is special as 'empty space,.,!' etc we do not copy the current space OutputString += FORMAT(InputString[i]) END ELSE OutputString += FORMAT(InputString[i]); OutputString := DELCHR(OutputString,'<>',' ');
And if we test it with the string from my above example:
test := 'Lorem ipsum began as scrambled , nonsensical Latin derived from Cicero 1st-century BC text De Finibus Bonorum et Malorum . '; MESSAGE(ReduceSpaces(test));
The result is a string without useless spaces in it that we can use later in NAV: