7

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

BaleineBleue
  • 139
  • 2
  • 12
  • Don't use the double colon `::` (always use `rem`) to place comments inside blocks. The `::` syntax is treated as an empty label and can produce weird behaviours and many syntax errors. [see jeb's answer](http://stackoverflow.com/questions/12407800/which-comment-style-should-i-use-in-batch-files/12408045#12408045) and also [Comments](http://www.robvanderwoude.com/comments.php) – elzooilogico May 03 '17 at 08:46

1 Answers1

5

Try putting quotes around your variable.

if "%str%" == " " (
    ...
)
Steve Trout
  • 9,261
  • 2
  • 19
  • 30
  • This works, thanks! How would I go about setting a variable to a space character? when I do set space= I get nothing, and when I do set space=" " I get " " – BaleineBleue May 03 '17 at 21:41