Business Central – Problem with too much spaces?

The dreaded mental block have struck again and I was left pondering on how to remove spaces from a text variable for quite a while. The most straightforward way, a bit primitive, was to loop through each character and append those without spaces to a new string. But there has to be a better way. AL language has a function to remove leading and trailing spaces, what is left is the spaces in between words.

An example of what was to be achieved as listed below:

Before: SIMON & SPENCER

After: SIMON&SPENCER

I tried using Text.ConvertStr function but I got an error.

NewString := Text.ConvertStr(String: Text, FromCharacters: Text, ToCharacters: Text)

The ToCharacters cannot be an empty text. I was stuck.

After browsing to and fro, I finally found the answer. It has been staring at me all these while and it has been something I have been using so frequently. Introducing Text.DelChr. I don’t even have to do any trimming at all.

NewString := Text.DelChr(String: Text [, Where: Text] [, Which: Text])
MyOutputString := Text.DelChr(MyInputText, '=', ' ')

Using the above, all spaces in the text variable will be removed. Nice!

Advertisement