Page 1 of 1
Forum

Welcome to the Tweaking4All community forums!
When participating, please keep the Forum Rules in mind!

Topics for particular software or systems: Start your topic link with the name of the application or system.
For example “MacOS X – Your question“, or “MS Word – Your Tip or Trick“.

Please note that switching to another language when reading a post will not bring you to the same post, in Dutch, as there is no translation for that post!



Share:
Notifications
Clear all

[Solved] Lazarus - StringReplace multiple strings with multiple other strings

1 Posts
1 Users
0 Reactions
5,727 Views
 Hans
(@hans)
Famed Member Admin
Joined: 12 years ago
Posts: 2859
Topic starter  

I'm sure other have ran into this issue;
You have a string, and in this string you'd like to replace multiple strings with multiple other strings.
For example:

s := 'The quick brown fox jumps over the lazy dog';

And we'd like to replace "brown" with "yellow", "dog" with "Golden Retriever" and "lazy" with "old".

Option 1 is to run StringReplace 3 times, for example:

s := stringreplace(s,'brown','yellow',[rfReplaceAll]);
s := stringreplace(s,'dog','Golden Retriever',[rfReplaceAll]);
s := stringreplace(s,'lazy','old',[rfReplaceAll]);

Option 2 is by nesting StringReplace:

s := stringreplace(stringreplace(stringreplace,'brown','yellow',[rfReplaceAll]),'dog','Golden Retriever',[rfReplaceAll]),'lazy','old',[rfReplaceAll]);

(definitely hard to follow and read)

Option 3 is a using "StringsReplace()" which takes 2 arrays, one array with the words to look for, and one array with the words to replace them with:

s := StringsReplace(s,['brown','dog','lazy'],['yellow','Golden Retriever','old'],[rfReplaceAll]);

Obviously the latter is easier to write and read and does it all ...


   
ReplyQuote
Share: