Thu Jul 19, 2018 5:46 am
Login Register Lost Password? Contact Us


How to do multiple "FindReplace" for a single string value?

Questions around writing code and queries

Thu Nov 12, 2015 2:08 am Change Time Zone

Hello:

I have a string "ABCDE"

with another string array:
['A','B','D']

I wanna replace all the letters in Group B From Group A to the letter "Z", so the result is:

ZZCZE

FindReplace can only accept one string, in the other languages we have foreach /for……loop. But how to implement that in ECL? Please don't use FindReplace three times, because we don't know how many replacement chars there are in the string array. So this solution should be general enough:)
WeiDong
 
Posts: 8
Joined: Fri Nov 06, 2015 1:05 am

Thu Nov 12, 2015 3:08 pm Change Time Zone

WeiDong,

You can use the STD.Str.SubstituteIncluded() function to do exactly that, just like this:
Code: Select all
IMPORT STD;
STD.Str.SubstituteIncluded('ABCDE', 'ABD', 'Z');

HTH,

Richard
rtaylor
Community Advisory Board Member
Community Advisory Board Member
 
Posts: 1366
Joined: Wed Oct 26, 2011 7:40 pm

Tue Jul 03, 2018 4:47 pm Change Time Zone

Just noticed this post.

You dont have to use a library function. The language itself has this functionality:
Code: Select all
s := 'ABCDE';
REGEXREPLACE('[ABD]',s,'Z');


I always use a language feature over a library function if there is a choice as the compiler can be clever with a language construct and might fold the whole expression away to nothing. With a library function it's forced to perform a call (which is not cheap) and all register tracking/ optimizations are lost.

Yours
Allan
Allan
 
Posts: 305
Joined: Sat Oct 01, 2011 7:26 pm


Return to Programming

Who is online

Users browsing this forum: No registered users and 0 guests

cron