Function for Split Text With Maximum Characters Without Damaging Words – AL / C/AL

I created a function to split a text with maximum characters trying without damaging words. For example, when developing reports, there may be requirements to capture fields containing long text without exceeding page size. In that case, you have to split the text with a maximum length and also they need to be shown line by line. If you use standard string functions directly to split text with a maximum length it can be caused to damage words. To avoid this issue you can use the following function and which tries to split text without exceeding the given maximum length and even without damaging words. It can use in AL and C/AL programming.

local procedure TrySplitTextWithoutDamagingWords(FullText: Text; MaximumCaractorLength:Integer; var MaximumText: Text; var RemainingText: Text) IsRemainingToSplit: Boolean
    var
        SpcPos: Integer;
        MaxSpcPos: Integer;
        CheckText: Text;
    begin
        FullText := DelChr(FullText, '<>', ' ');
        if (FullText = '') or (MaximumCaractorLength = 0) or (StrLen(FullText) <= MaximumCaractorLength) then begin
            MaximumText := FullText;
            RemainingText := '';
            exit(false);
        end;
        MaximumText := CopyStr(FullText, 1, MaximumCaractorLength);
        RemainingText := CopyStr(FullText, MaximumCaractorLength + 1, StrLen(FullText));
        if CopyStr(FullText, MaximumCaractorLength+1, 1)<>' 'then begin
            CheckText := MaximumText;
            repeat begin
                SpcPos := StrPos(CheckText, ' ');
                MaxSpcPos += SpcPos;
                CheckText := CopyStr(CheckText, SpcPos + 1, MaximumCaractorLength);
            end until SpcPos = 0;
            if MaxSpcPos > 0 then begin
                MaximumText := CopyStr(FullText, 1, MaxSpcPos - 1);
                RemainingText := CopyStr(FullText, MaxSpcPos + 1, StrLen(FullText));
            end;
        end;
        exit(StrLen(RemainingText) > MaximumCaractorLength);
    end;Code language: PHP (php)

How to use the function?

There are four parameters and a boolean type return value.

  1. Parameter – FullText:
    The text that you want to split. The data type is Text.
  2. Parameter – MaximumCaractorLength:
    Maximum length to split. The data type is Integer.
  3. Parameter – MaximumText:
    The var type parameter that will be assigned split text. The data type is Text.
  4. Parameter – RemainingText:
    The var type parameter that will be assigned remaining text. The data type is Text.
  5. Return Value – IsRemainingToSplit:
    If there is a remaining text exceeding the maximum length it returns true. The data type is Boolean.

Example 1: Single split:

I create three local variables (FullText: Text, SplitText: Text, and RemainingText: Text) and pass them as follows (The maximum character length is 26).

FullText:='NAVUSER is the best site for learning Microsoft Dynamics Navision and Dynamics 365 Business Central.';
TrySplitTextWithoutDamagingWords(FullText,26,SplitText,RemainingText);
Message(SplitText);Code language: JavaScript (javascript)

The output is: “NAVUSER is the best site“.

Example 2: Multiple split:

Same as the above example, I create the local variables and build the logic as follows. (The maximum character length is 26).

FullText:='NAVUSER is the best site for learning Microsoft Dynamics Navision and Dynamics 365 Business Central.';
RemainingText:=FullText;
repeat
     TrySplitTextWithoutDamagingWords(RemainingText,26,SplitText,RemainingText);
     Message(SplitText);
until RemainingText='';Code language: JavaScript (javascript)

The outputs are:

  1. NAVUSER is the best site
  2. for learning Microsoft
  3. Dynamics Navision and
  4. Dynamics 365 Business
  5. Central.

Senior Solutions Architect - Microsoft Dynamics Navision / Microsoft Dynamics 365 Business Central and freelance developer. (The admin of NAVUSER)

Leave a Comment