<p>I am using StringReplace to replace &amp;gt and &amp;lt by the char itself in a generated XML like this:</p>

<pre><code>StringReplace(xml.Text,'&amp;gt;','&gt;',[rfReplaceAll]) ;
StringReplace(xml.Text,'&amp;lt;','&lt;',[rfReplaceAll]) ;
</code></pre>

<p>The thing is it takes way tooo long to replace every occurence of &amp;gt.</p>

<p>Do you purpose any better idea to make it faster?</p>

The problem is that you are iterating the entire string size twice (one for replacing &gt; by > and another one to replace &lt; by <).
You should iterate with a for and simply check ahead whenever you find a & for a gt; or lt; and do the immediate replace and then skipping 3 characters ((g|l)t;). This way it can do that in proportional time to the size of the string
xml.Text
.
A simple C# example as I do not know Delphi but should do for you to get the general idea.
```
```
String s = "&lt;xml&gt;test&lt;/xml&gt;";
char[] input = s.ToCharArray();
char[] res = new char[s.Length];
int j = 0;
for (int i = 0, count = input.Length; i < count; ++i)
{
    if (input[i] == '&')
    {
        if (i < count - 3)
        {
            if (input[i + 1] == 'l' || input[i + 1] == 'g')
            {
                if (input[i + 2] == 't' && input[i + 3] == ';')
                {
                    res[j++] = input[i + 1] == 'l' ? '<' : '>';
                    i += 3;
                    continue;
                }
            }
        }
    }

    res[j++] = input[i];
}
Console.WriteLine(new string(res, 0, j));
```
```
This outputs:
```
```
<xml>test</xml>
```
```