Dynamics 365 Business Central: How to quickly remove white-space characters from the current Text/String (leading, trailing and all) in AL

Dynamics 365 Business Central

Hi, Readers.
Today I would like to share another mini tip about Business Central, how to quickly remove white-space characters from the current Text (leading, trailing and all) in AL.

This is generally divided into the following four situations.

Let’s see more details.

Remove leading white-space

Using Text.TrimStart([Text]) Method: Removes all leading occurrences of a set of characters specified in an array from the current Text object.

Remove trailing white-space

Using Text.TrimEnd([Text]) Method: Removes all trailing occurrences of a set of characters specified in an array from the current Text object.

Remove leading and trailing white-space

Using Text.Trim() Method: Returns a new Text in which all leading and trailing white-space characters from the current Text object are removed.

Remove all white-space

Using Text.DelChr(Text [, Text] [, Text]) Method: Deletes chars contained in the which parameter in a string based on the contents on the where parameter. More details: How to use String Functions in AL (StrPos, CopyStr, PadStr, StrLen, LowerCase, SelectStr…)

Test video:

Very simple, give it a try!!!😁

Test code: Github (Please note that the source code is for reference only, you can improve it according to your own needs)

page 50110 "Remove Spaces"
{
    PageType = Card;
    ApplicationArea = All;
    UsageCategory = Administration;

    layout
    {
        area(Content)
        {
            group(Input)
            {
                field(String; String)
                {
                    ShowCaption = false;
                    ApplicationArea = All;
                }
            }
            group(Result)
            {
                field(Result01; Result)
                {
                    ShowCaption = false;
                    ApplicationArea = All;
                    Editable = false;
                }
            }
        }
    }
    actions
    {
        area(Processing)
        {
            action(RemoveLeading)
            {
                ApplicationArea = All;
                Caption = 'Remove Leading White-spaces';
                Promoted = true;
                PromotedCategory = Process;
                Image = Delete;
                ToolTip = 'Removes leading spaces from the input string.';
                trigger OnAction()
                begin
                    if String <> '' then
                        Result := String.TrimStart();
                end;
            }
            action(RemoveTrailing)
            {
                ApplicationArea = All;
                Caption = 'Remove trailing White-spaces';
                Promoted = true;
                PromotedCategory = Process;
                Image = Delete;
                ToolTip = 'Removes trailing spaces from the input string.';
                trigger OnAction()
                begin
                    if String <> '' then
                        Result := String.TrimEnd();
                end;
            }
            action(RemoveLeadingTrailing)
            {
                ApplicationArea = All;
                Caption = 'Remove Leading and Trailing White-spaces';
                Promoted = true;
                PromotedCategory = Process;
                Image = Delete;
                ToolTip = 'Removes leading and trailing spaces from the input string.';
                trigger OnAction()
                begin
                    if String <> '' then
                        Result := String.Trim();
                end;
            }
            action(RemoveAll)
            {
                ApplicationArea = All;
                Caption = 'Remove All White-spaces';
                Promoted = true;
                PromotedCategory = Process;
                Image = Delete;
                ToolTip = 'Removes all spaces from the input string.';
                trigger OnAction()
                begin
                    if String <> '' then
                        Result := DelChr(String, '=', ' ');
                end;
            }
        }
    }
    var
        String: Text;
        Result: Text;
}

PS:
1. How to validate E-Mail field and Phone field format via AL (Validation rules)

2. Business Central 2023 wave 1 (BC22): Iterating with foreach on Text variables

3. How to get the next or previous letter in the alphabet

4. How to prohibit users from entering illegal characters such as emoji, Hiragana, kanji, etc.

5. Dynamics 365 Business Central: How to reverse a string in AL

6. Dynamics 365 Business Central: How to replace a string in AL (Four ways)

END

Hope this will help.

Thanks for reading.

ZHU

コメント

Copied title and URL