-1

I would like to edit my code to replace all special characters. For example I am replacing the & character with ""&""

Here is my code:

trigger OnOpenPage();
    var
        ItInput: Text[50];
        ItWhat: Text[50];
        ItWhit: Text[50];
        ItOutput: Text[50];

        LiInputLen: Integer;
        LiWhatLen: Integer;
        LiFindFirst: Integer;

        Aux: Label 'Juan & Jose & "Niño"';
        Text001: Label '&';
        Text002: Label '&';


    begin
        // La función TextReplace encuentra todas las ocurrencias de 'What' en la cadena 'Input' y las reemplaza con 'ltWith'
        ItInput := Aux;
        ItWhat := Text001;
        ItWhit := Text002;

        LiInputLen := STRLEN(ItInput);
        LiWhatLen := STRLEN(ItWhat);

        // Comprueba si ltInput y ltWhat no son cadenas vacías
        If (LiInputLen > 0) and (LiWhatLen > 0) then begin
            LiFindFirst := STRPOS(ItInput, ItWhat);
            WHILE (LiFindFirst > 0) do begin
                ItOutput := ItOutput + COPYSTR(ItInput, 1, liFindFirst - 1);
                ItOutput := ItOutput + ItWhit;
                ItInput := DELSTR(ItInput, 1, liFindFirst + liWhatLen - 1);
                liFindFirst := STRPOS(ItInput, ItWhat);
            end;
            ItOutput := ItOutput + ItInput;
            Message(ItOutput);

        end else begin
            ItOutput := ItInput;
        end;
    end;

This is the message I show:

Juan & Jose & "Niño


...But I would like to implement the following validations:

■ In the case of &, the sequence "&amp";

■ In the case of “, the sequence "&quot";

■ In the case of <, the sequence "&lt";

■ In the case of >, the sequence "&gt";

■ In the case of ', the sequence "&apos";

Can someone help me here?

I'd appreciate it guys.

1 Answers1

1

There are two ways to handle this:

  1. Use codeunit Uri
  2. Use the built in Replace method

Using codeunit Uri is pretty simple:

trigger OnOpenPage()
var
    Uri: Codeunit Uri;
begin
    ItOutput := Uri.EscapeDataString(ItInput);
end;

Using Replace requires a bit more code and that you make sure to replace in the correct order:

trigger OnOpenPage()
begin
    ItOutput := ItInput.Replace('&', '&amp;').Replace('"', '&quot;').Replace(...).Replace(...);
end;
kaspermoerch
  • 16,127
  • 4
  • 44
  • 67