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

Dynamics 365 Business Central

Hi, Readers.
Today I would like to talk about how to replace a string in AL. I will share four different ways, hope this can help you.

Using Text.ConvertStr(Text, Text, Text) Method

Text.ConvertStr(Text, Text, Text) Method: Replaces all chars in source found in FromCharacters with the corresponding char in ToCharacters and returns the converted string.

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

For example,

Test code:

pageextension 50122 CustomerListExt extends "Customer List"
{
    trigger OnOpenPage()
    var
        OriginalString: Text[30];
        FromChars: Text[30];
        ToChars: Text[30];
        NewString: Text[30];
        Msg: Label 'The original sentence is:\%1\\The sentence is converted to:\%2';
        Text004: Label '';
    begin
        OriginalString := 'Do you like cars?';
        FromChars := 'Dylc';
        ToChars := 'dYLC';
        NewString := ConvertStr(OriginalString, FromChars, ToChars);
        Message(Msg, OriginalString, NewString);
    end;
}

But this method has some limitations. If the length of the FromCharacters parameter and the ToChars parameter are different, an exception is thrown, for example, from cars to vehicles.

ALConvertStr expects 2 strings in the conversion to be of equal length: Converting from cars to vehicles.

PS:
1. This method can be invoked without specifying the data type name.

2. If either the FromCharacters or the ToCharacters strings are empty, then the source is returned unchanged.

Using Text.Replace(Text, Text) Method

Text.Replace(Text, Text) Method: Returns a new Text in which all occurrences of a specified string in the current instance are replaced with another specified string.

Syntax: Result := Text.Replace(OldValue: Text, NewValue: Text)

This method cannot achieve the above character replacement, but it can have no length limit.

For example,

Test code:

pageextension 50122 CustomerListExt extends "Customer List"
{
    trigger OnOpenPage()
    var
        OriginalString: Text[30];
        FromChars: Text[30];
        ToChars: Text[30];
        NewString: Text[30];
        Msg: Label 'The original sentence is:\%1\\The sentence is converted to:\%2';
        Text004: Label '';
    begin
        OriginalString := 'Do you like cars?';
        FromChars := 'cars';
        ToChars := 'vehicles';
        NewString := OriginalString.Replace(FromChars, ToChars);
        Message(Msg, OriginalString, NewString);
    end;
}

Using TextBuilder.Replace(Text, Text) Method

TextBuilder.Replace(Text, Text) Method: Replaces all occurrences of a specified string in this TextBuilder instance with another specified string.

Syntax: [Ok := ] TextBuilder.Replace(OldText: Text, NewText: Text)

TextBuilder Data Type: Represents a lighweight wrapper for the .Net implementation of StringBuilder.
We have discussed Dynamics 365 Business Central: Using TextBuilder Data Type to export data to a text file before. This is very useful when you need to insert multiple lines of data and export them in txt format.

For example,

Test code:

pageextension 50122 CustomerListExt extends "Customer List"
{
    trigger OnOpenPage()
    var
        OriginalString: Text[30];
        FromChars: Text[30];
        ToChars: Text[30];
        NewString: Text[30];
        Msg: Label 'The original sentence is:\%1\\The sentence is converted to:\%2';
        TxtBuilder: TextBuilder;
    begin
        OriginalString := 'Do you like cars?';
        TxtBuilder.AppendLine(OriginalString);
        FromChars := 'cars';
        ToChars := 'vehicles';
        if TxtBuilder.Replace(FromChars, ToChars) then
            NewString := TxtBuilder.ToText();
        Message(Msg, OriginalString, NewString);
    end;
}

Using codeunit 46 SelectionFilterManagement

In codeunit 46 SelectionFilterManagement there is a standard method that can be used.

procedure ReplaceString(String: Text; FindWhat: Text; ReplaceWith: Text) NewString: Text

For example,

Test code:

pageextension 50122 CustomerListExt extends "Customer List"
{
    trigger OnOpenPage()
    var
        OriginalString: Text[30];
        FromChars: Text[30];
        ToChars: Text[30];
        NewString: Text[30];
        Msg: Label 'The original sentence is:\%1\\The sentence is converted to:\%2';
        SelectionFilterManagement: Codeunit SelectionFilterManagement;
    begin
        OriginalString := 'Do you like cars?';
        FromChars := 'cars';
        ToChars := 'vehicles';
        NewString := SelectionFilterManagement.ReplaceString(OriginalString, FromChars, ToChars);
        Message(Msg, OriginalString, NewString);
    end;
}

PS: If you need some special conversion processing, you can refer to this standard method to make some customization.

Very simple, give it a try!!!😁

END

Hope this will help.

Thanks for reading.

ZHU

コメント

Copied title and URL