Search code examples
dynamics-business-centraldynamics-al

Delete multiple special characters in string


I'm new in Dynamics 365 Business Central and AL. I need to delete special characters from a string (email address, phone numbers). I tried DELCHR(String,'=','['), but there are more chars forbidden. I know I can add more than one, but I don't know if I caught them all. Can anyone please help me with a more efficient way? Help is highly appreciated.

Thanks


Solution

  • you can write a function which keeps the 'good' characters, like:

    local procedure DeleteSpecialChars(var yourText: Text)
    var
        AllowedChars: Text;
    begin
        AllowedChars := 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ-0123456789';
        exit(DelChr(yourText, '=', DELCHR(yourText, '=', AllowedChars)));
    end;
    

    I assume you develope for BC V14 or later. If so, you can use the codeunit 47 StringConversionManagement methods RemoveNonAlphaNumericCharacters or RemoveDecimalFromString.

    For email addresses you should use codeunit 9520 Mail Management. It holds a function which validates email addresses (CheckValidEmailAddress).