How to convert multiple <br/> tags to a single <br/> tag in Pascal

1 Answer

0 votes
program ReplaceBrTags;

function ReplaceMultipleBrTags(input: string): string;
var
  searchTag, output: string;
  i: Integer;
begin
  searchTag := '<br/>';
  output := input;

  // Replace multiple consecutive <br/> with a single one
  repeat
    i := Pos(searchTag + searchTag, output);
    if i > 0 then
    begin
      Delete(output, i, Length(searchTag));
    end;
  until i = 0;

  ReplaceMultipleBrTags := output;
end;

var
  inputStr, resultStr: string;
begin
  inputStr := 'ab<br/><br/>cd<br/>efg<br/><br/><br/>hijk<br/><br/>';
  
  resultStr := ReplaceMultipleBrTags(inputStr);
  
  WriteLn(resultStr);
end.


   
(*
run:
  
ab<br/>cd<br/>efg<br/>hijk<br/>
  
*)

 



answered Jul 14, 2025 by avibootz
...