Page 1 of 1

variable character replace

Posted: 2022-Jun-26, 9:16 am
by lazna
Need to test character presence in a string, but have no luck. What am I doing wrong?

Code: Select all

set string=abc
set char=a
call set "out=%%%string%:%char%=%%"
if %string%==%out% (echo Char %char% not present) else (echo Char %char% present)

Re: variable character replace

Posted: 2022-Jun-26, 1:56 pm
by Simon Sheppard
Try like this

Code: Select all

@echo off
set _string=abc
set _char=a
call set _out=%%_string:%_char%=%%
echo [%_out%]
if %_string%==%_out% (echo Char %_char% not present) else (echo Char %_char% present)
So in the CALL SET line, the outer %'s have to be doubled because we are using CALL, the _string variable doesn't need any %'s and the _char variable does because we want to pass the value not the variable name.

Re: variable character replace

Posted: 2022-Jun-27, 7:09 am
by lazna
thanks, it work as expected.