Dynamics 365 Business Central: How to capitalize the first letter of each word in a string (For example: NEW YORK -> New York)

Dynamics 365 Business Central

Hi, Readers.
A friend just asked me a question. Is there a way to format a string with “proper case? (Only initial caps), for example: NEW YORK -> New York, Los Angeles -> Los Angeles.

I immediately tested it and found it possible. Someone else may have run into this problem, so in this post, I will share how to do it, hope this will help.

First, you can use the following method to change the first letter of the sentence to uppercase.

So what we have to do is to split the string according to the Separator, then do the same processing, and finally combine them.

Update 2020/12/07
Using List Data type in AL. (Recommend)

TextList := InputString.Split(‘ ‘)
List Data Type

pageextension 50100 CustomerListExt extends "Customer List"
{
    trigger OnOpenPage();
    var
        InputString: Text[1024];
        StringList: List of [Text];
        OutputString: Text[1024];
        i: Integer;
    begin
        Clear(i);
        InputString := 'NEW YORK ABC CAD BDF RED BLUE';
        Message('InputString is %1.', InputString);
        StringList := InputString.Split(' ');
        for i := 1 to StringList.Count() do begin
            OutputString := OutputString + ' ' + UPPERCASE(COPYSTR(StringList.Get(i), 1, 1)) + LOWERCASE(COPYSTR(StringList.Get(i), 2));
        end;
        Message('OutputString is %1.', OutputString);
    end;
}

Test Video:

For reference only, not recommended:

The Split Strings Functions:

Test data is ‘NEW YORK ABC CAD BDF RED’. And the Separator is ‘ ‘(Space).

Output String is ‘New York Abc Cad Bdf Red’.

Source Code:

pageextension 50100 CustomerListExt extends "Customer List"
{
    trigger OnOpenPage();
    var
        InputString: Text[1024];
        MidString: array[100] of Text[1024];
        OutputString: Text[1024];
        i: Integer;
    begin
        InputString := 'NEW YORK ABC CAD BDF RED';
        Message('InputString is %1.', InputString);
        WHILE STRLEN(InputString) > 0 DO BEGIN
            i := i + 1;
            MidString[i] := SplitStrings(InputString, ' ');
            OutputString := OutputString + ' ' + UPPERCASE(COPYSTR(MidString[i], 1, 1)) + LOWERCASE(COPYSTR(MidString[i], 2));
        END;
        Message('OutputString is %1.', OutputString);
    end;

    procedure SplitStrings(VAR String: Text[1024]; Separator: Text[1]) SplitedString: Text[1024]
    var
        Pos: Integer;
    begin
        Clear(Pos);
        Pos := STRPOS(String, Separator);
        if Pos > 0 then begin //Whether there is a separator
            SplitedString := COPYSTR(String, 1, Pos - 1);//Copy the string before the separator
            if Pos + 1 <= STRLEN(String) then //Is it all
                String := COPYSTR(String, Pos + 1)//Copy the string after the separator
            else
                Clear(String);
        end else begin
            SplitedString := String;
            Clear(String);
        end;
    end;
}

END

If there is a better way, please contact me.

Hope this will help.

Thanks for your reading.

ZHU

コメント