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

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
ghostmachine4
Posts: 319
Joined: 12 May 2006 01:13

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

#1 Post by ghostmachine4 » 05 Apr 2011 08:52

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?

jeb
Expert
Posts: 1055
Joined: 30 Aug 2007 08:05
Location: Germany, Bochum

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

#2 Post by jeb » 05 Apr 2011 13:19

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

DosItHelp
Expert
Posts: 239
Joined: 18 Feb 2006 19:54

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

#3 Post by DosItHelp » 05 Apr 2011 22:08

Fixed - thanks guys! :D

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

ghostmachine4
Posts: 319
Joined: 12 May 2006 01:13

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

#4 Post by ghostmachine4 » 05 Apr 2011 22:23

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 ?

DosItHelp
Expert
Posts: 239
Joined: 18 Feb 2006 19:54

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

#5 Post by DosItHelp » 05 Apr 2011 22:46

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"

Post Reply