Hi,
See the following piece of code:
set str=teh cat in teh hat
echo.%str%
set str=%str:teh=the%
echo.%str%
How can I use variables instead of teh and the in the line "set str=%str:teh=the%" ?
I have tried this:
set str=teh cat in teh hat
echo.%str%
var1=teh
var2=the
set str=%str:%var1%=%var2%%
echo.%str%
but it did not work.
How can this be done?
Thanks
Replace a substring with the contents of a variable
Moderator: DosItHelp
Re: Replace a substring with the contents of a variable
This should work.
Code: Select all
set str=teh cat in teh hat
echo.%str%
var1=teh
var2=the
call set "str=%%str:%var1%=%var2%%%"
echo.%str%
Re: Replace a substring with the contents of a variable
Or this one...
Code: Select all
setlocal EnableDelayedExpansion
set str=teh cat in teh hat
echo.%str%
var1=teh
var2=the
set str=!str:%var1%=%var2%!
echo.%str%
Re: Replace a substring with the contents of a variable
Thanks a lot. Both worked.