Escape/replace characters in a string

Hi guys,

I use a script for title updates to our RDS encoder, but the : and / character are reserved for special purposes apparently… So I need to escape them when they exist in an artist/title. So : needs to become : and / needs to become /. Any ideas how to do this? I tried the Delphi function StringReplace but mAirList doesn’t know that unfortunately…

Thanks!

Hmm I look at this now and see “: needs to become : and / needs to become /” doesn’t make sense of course, the backslashes dissapeared.

So : -> \: and / -> \/.

You might try something like this:

var
  Oldstring, Newstring, Part: string;
  i: integer;
  
begin
  Newstring := '';
  for i := 1 to Length(Oldstring) do
    begin
      Part := Copy(Oldstring, i, 1);
      if Part = ':' then
        Part := '\:'
      else if Part = '/' then
        Part := '\/';      
      Newstring := Newstring + Part;
    end;
end.

Oldstring derives from the function extracting the title string, Newstring is the escaped title to be sent to the encoder.

Replaced regards

TSD

Addendum:

The whole thing can be packed into a function, like this:

function Escape(Oldstring: string): string;
var
  Newstring, Part: string;
  i: integer;
begin
  Newstring := '';
  for i := 1 to Length(Oldstring) do
    begin
      Part := Copy(Oldstring, i, 1);
      if Part = ':' then
        Part := '\:'
      else if Part = '/' then
        Part := '\/';      
      Newstring := Newstring + Part;
    end;
  Result := Newstring;
end;

This function can be called within the script from which the title string is generated:

var
  Title: string;

function Escape(Oldstring: string): string;
  // (everything from above)

procedure OnPlayerStart(PlaylistIndex: integer; PlayerIndex: integer; Item: IPlaylistItem);
begin
  Title := Escape(Item.GetTitle);
end;

begin
end.


Tidied-up regards

TSD

Thank you very much Tondose! :slight_smile:

1 Like