Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Batch test if variable is equal to a space

In my code, I am looping through each character in a string. I need to test if the character is a space.

This is my code:

if %str% == " " (
    ::echo Empty
    echo | set /p=%space%
    goto loopEnd
)

I have also tried:

if [%str%] == [" "] (
    ::echo Empty
    echo | set /p=%space%
    goto loopEnd
)

Both give the error

( was unexpected at this time.

Or

] was unexpected at this time.

I don't get errors testing for letters or numbers. What am I doing wrong?

Thanks,

Zach

like image 815
BaleineBleue Avatar asked May 02 '17 19:05

BaleineBleue


1 Answers

Try putting quotes around your variable.

if "%str%" == " " (
    ...
)
like image 113
Steve Trout Avatar answered Oct 03 '22 16:10

Steve Trout