variable character replace

Microsoft Windows
Post Reply
lazna
Posts: 8
Joined: 2022-Jun-19, 1:48 pm

variable character replace

Post 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)
User avatar
Simon Sheppard
Posts: 102
Joined: 2021-Jul-10, 7:46 pm
Contact:

Re: variable character replace

Post 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.
lazna
Posts: 8
Joined: 2022-Jun-19, 1:48 pm

Re: variable character replace

Post by lazna »

thanks, it work as expected.
Post Reply