A Lesson in Using the Right Function for the Job


The dreaded mental block 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 is 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. So I have been using the wrong function!
Subscribe to my newsletter
Read articles from Doufu directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by

Doufu
Doufu
Current project developments on Microsoft platforms such as Business Central, Dynamics 365 Sales, Power Platform and occasionally Python. Used to get my paws dirty on Android and Web development.