Page 1 of 1

Does string1 begin with string2 bug or is it just me?

Posted: 05 Apr 2011 08:52
by ghostmachine4
Source: Does String1 begin with String2

Code being tested with slight modification ( on WinXP)

Code: Select all

@echo off
SETLOCAL ENABLEDELAYEDEXPANSION
set string1="ant"
set string2="testing"
set s=@@@%string1%
if "%s%"=="!s:@@@%string2%!=" (
    echo.%string1% doesn`t begin with %string2%
) ELSE (
    echo.%string1% begins with %string2%
)


on the command line

Code: Select all

C:\work>type test.bat
@echo off
SETLOCAL ENABLEDELAYEDEXPANSION
set string1="ant"
set string2="testing"
set s=@@@%string1%
if "%s%"=="!s:@@@%string2%!=" (
    echo.%string1% doesnt begin with %string2%
) ELSE (
    echo.%string1% begins with %string2%
)

C:\work>test.bat
"ant" begins with "testing"


clearly how can "ant" begins with "testing". Is it me, or is it a bug?

Re: Does string1 begin with string2 bug or is it just me?

Posted: 05 Apr 2011 13:19
by jeb
Yes you are right, it's a bug in http://www.dostips.com/DtCodeSnippets.php#Snippets.DoesString1StartWithString2 ( And I always fogot how to use the url-tag ).

But I didn't believe that you didn't found the bug :wink:

And of course batch is good for having fun, not for writing readable, good or big programs.
I used here my personal definition of fun, there could be persons who didn't share my definition.

jeb

Re: Does string1 begin with string2 bug or is it just me?

Posted: 05 Apr 2011 22:08
by DosItHelp
Fixed - thanks guys! :D

old:
if "%s%"=="!s:@@@%string2%!=" (
new:
if "%s%"=="!s:@@@%string2%=!" (

Re: Does string1 begin with string2 bug or is it just me?

Posted: 05 Apr 2011 22:23
by ghostmachine4
DosItHelp wrote:Fixed - thanks guys! :D


ok, did i do something incorrect this time ?

Code: Select all

C:\work>type test.bat
@echo off
SETLOCAL ENABLEDELAYEDEXPANSION
set string1="testing"
set string2="test"
set s=@@@%string1%
if "%s%"=="!s:@@@%string2%=!" (
    echo.%string1% doesnt begin with %string2%
) ELSE (
    echo.%string1% begins with %string2%
)

C:\work>test.bat
"testing" doesnt begin with "test"


but "testing" does begin with "test". I think its just the echo part ?

Re: Does string1 begin with string2 bug or is it just me?

Posted: 05 Apr 2011 22:46
by DosItHelp
The double quotes are trouble.
5th character of string1 is not a double quote.

this should work:
set string1="test"ing
set string2="test"

or this:
set "string1=testing"
set "string2=test"

or this:
set string1=testing
set string2=test

or even this
set "string1="test"ing"
set "string2="test""

but unbalanced quotes however will cause trouble in the IF statement:
set "string1="testing"
set "string2="test"