REPLACESTRINGS bulk replaces matched substrings from one list with corresponding entries in a 2nd list.
If lists are not same length, only makes replacements up to the last entry in the shortest list. Function works recursively and returns the final string with all replacements.
If lists are not same length, only makes replacements up to the last entry in the shortest list. Function works recursively and returns the final string with all replacements.
Excel Formula:
=LAMBDA(
searchTxt,
findList,
replaceList,
LET(
n,
MIN(
ROWS(findList),
ROWS(replaceList)
),
Loop,
LAMBDA(
ME,
myTxt,
i,
LET(
find,INDEX(findList,i),
rep,INDEX(replaceList,i),
new,SUBSTITUTE(myTxt,find,rep),
IF(
i<n,ME(ME,new,i+1),
new
)
)
),
Loop(Loop,searchTxt,1)
)
)
LAMBDA_Testing_ReplaceStrings.xlsm | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|
B | C | D | E | F | G | H | I | |||
2 | Input: | This is a test | ||||||||
3 | ||||||||||
4 | Input Lists | Modeled Behavior | ||||||||
5 | Find | Replace | Text | i | Find | Replace | Return | |||
6 | This | That | This is a test | 1 | This | That | That is a test | |||
7 | is | IS | That is a test | 2 | is | IS | That IS a test | |||
8 | a | an | That IS a test | 3 | a | an | Thant IS an test | |||
9 | test | example | Thant IS an test | 4 | test | example | Thant IS an example | |||
10 | ||||||||||
11 | Anonymous: | Thant IS an example | ||||||||
12 | Named: | Thant IS an example | ||||||||
Scratch |
Cell Formulas | ||
---|---|---|
Range | Formula | |
E6 | E6 | =C2 |
G6:G9 | G6 | =INDEX($B$6:$B$9,F6) |
H6:H9 | H6 | =INDEX($C$6:$C$9,F6) |
I6:I9 | I6 | =SUBSTITUTE(E6,G6,H6) |
E7:E9 | E7 | =I6 |
F7:F9 | F7 | =F6+1 |
C11 | C11 | =LAMBDA(searchTxt,findList,replaceList,LET(n,MIN(ROWS(findList),ROWS(replaceList)),Loop,LAMBDA(ME,myTxt,i,LET(find,INDEX(findList,i),rep,INDEX(replaceList,i),new,SUBSTITUTE(myTxt,find,rep),IF(i<n,ME(ME,new,i+1),new))),Loop(Loop,searchTxt,1)))(C2,B6:B9,C6:C9) |
C12 | C12 | =ReplaceStrings(C2,B6:B9,C6:C9) |
Last edited by a moderator:
Upvote
1