I need the fastest find and replace ever made.

Posted on 2004-11-22
Last Modified: 2011-04-14
I need the fastest find and replace ever made. Maybe not really the fastest, but i just want to find and replace the right way.
For example string is a text of length of 30. i have a list of words that I want to find in the string and replace them with 'xyz'.

Do you have any idea what the best way is to load the list of words and then do the replacing?

Question by:AngeloX
    LVL 6

    Expert Comment

    One way to do it is

      Str := StringReplace(Str,Key,'xyz',[rfReplaceAll]);

    LVL 5

    Accepted Solution

    You can do the same as above but much faster using FastReplace from
    LVL 14

    Expert Comment

    or get madshi's components, and do a


    // assuming that your keyword list is already in a TStringList called Keywords

    for i := 0 to Keywords.Count - 1 do
      ReplaceStr(Str, Keywords[i], 'xyz', True);

    from Madshi's help file:

    The following functions search through the whole "str" and replace the fragment "replaceThis" every time it occurs "withThis". The replacement can be done recursively, if you wish. That means, that the replaced string fragments are checked again. "ReplaceStr" searches case sensitively, while "ReplaceText" searches case insensitively.

    function ReplaceStr  (var str     : string;
                          replaceThis : string;
                          withThis    : string;
                          replaceSelf : boolean = false) : boolean;
    function ReplaceText (var str     : string;
                          replaceThis : string;
                          withThis    : string;
                          replaceSelf : boolean = false) : boolean;

    Get his package from

    Author Comment

    Thnx I guess FastStrings will do. But do you also have any idea what the best way is to load a list of words and then go by them one for one? Is the fastest way to load them in a TList and then do a for loop for every word, or is there a faster way to do that?

    LVL 17

    Expert Comment

    Maybe You can find it usefull for stringreplace

    Author Comment

    That one was fast enough hehe. Thnx!

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    IT, Stop Being Called Into Every Meeting

    Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

    A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
    Objective: - This article will help user in how to convert their numeric value become words. How to use 1. You can copy this code in your Unit as function 2. than you can perform your function by type this code The Code   (CODE) The Im…
    It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
    This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor ( If you're looking for how to monitor bandwidth using netflow or packet s…

    758 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    8 Experts available now in Live!

    Get 1:1 Help Now