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

 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;

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);

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='';

No comments:

Post a Comment