How to replace " ' " the "'" HTML character with Stringreplace

1.2k views Asked by At

i use a code to get the HTML from a page and save it's strings but these strings sometimes come with ' as the ' character

im trying to replace it with the actual ' character and it doesn't seem to work i've tried this lines to find the string but no results

  StringReplace(myString, ''', char(39), [rfReplaceAll]);
  StringReplace(myString, char(39), '*', [rfReplaceAll]);

  aPos := Pos(myString, char(39));
  aPos := Pos(myString, ''');
  aPos := Pos(myString, '&');
  aPos := Pos(myString, '#');
  aPos := Pos(myString, '039');

Apos is never > 0

  if aPos > 0 then
  begin
    // replace the string
  end;
2

There are 2 answers

5
Rudy Velthuis On

StringReplace does not work in-place. You must assign the result of the function to a(nother) string:

myOtherString := StringReplace(myString, '&#039', '''', [rfReplaceAll]);

Try that, and you'll see that myOtherString contains the proper format. Of course you can also do:

myString := StringReplace(myString, '&#039', '''', [rfReplaceAll]);

EDIT

IMO, the function Pos is easier to remember as:

function Pos(const Needle, Haystack: string): Integer;

So if you swap your parameters, it should work.

0
David Heffernan On

StringReplace is a function that returns the new value. You are ignoring that new value. You need:

myString := StringReplace(myString, ...);
....

The other part of your question concerns the use of Pos. The first parameter to Pos is the sub-string that you are searching for within the second parameter. So you have your parameters to Pos in reverse order.