Test whether a string contains another string

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
Hendrik_-
Posts: 3
Joined: 27 Apr 2017 07:14

Test whether a string contains another string

#1 Post by Hendrik_- » 27 Apr 2017 07:42

Hi,

I want to test in a batch file whether a string contains a different string.
For some reason, it is not possible to get the search string properly in the formula.

I have two variable that contain the strings:

set StringA=aba d25 c9.8 v23
set SearchString=9.7

Then i do the test:

If NOT "%StringA%"=="%StringA:%SearchString%=%" (
echo Yes
) else (
echo No
)

The test returns 'Yes' but it must be 'nN', as the following test does:

If NOT "%StringA%"=="%StringA:9.7=%" (
echo Yes
) else (
echo No
)

What am I doing wrong? Can anyone help?

Compo
Posts: 600
Joined: 21 Mar 2014 08:50

Re: Test whether a string contains another string

#2 Post by Compo » 27 Apr 2017 11:06

The recommended way is to enable delayed expansion.

Code: Select all

@Echo Off
SetLocal DisableDelayedExpansion

Set "StringA=Aba D25 C9.8 V23"
Set "SearchString=9.7"

SetLocal EnableDelayedExpansion
If /I Not "%StringA%"=="!StringA:%SearchString%=!" (Echo Yes) Else Echo No
EndLocal

penpen
Expert
Posts: 2009
Joined: 23 Jun 2013 06:15
Location: Germany

Re: Test whether a string contains another string

#3 Post by penpen » 27 Apr 2017 11:31

Hendrik_- wrote:What am I doing wrong?
String replacement sometimes could be tricky.
In your case the expansion of (the second) "StringA" variable ends at the percentage sign character right after the double colon;
in this case there is no string operation because the part after the colon is missing, so it expands the environment variable "StringA:", which probably is not defined.
The next environment variable in this file is "=" which also shouldn't be defined.

So the if line after the expansion is (if both variables are undefined):

Code: Select all

If NOT "aba d25 c9.8 v23"=="SearchString" (
The strings "aba d25 c9.8 v23" and "SearchString" are not equal, so it echoes "Yes".

Sidenote: Please use code tags ([code][/code]) around your code.


penpen

Hendrik_-
Posts: 3
Joined: 27 Apr 2017 07:14

Re: Test whether a string contains another string

#4 Post by Hendrik_- » 27 Apr 2017 15:10

penpen wrote:
Hendrik_- wrote:What am I doing wrong?
String replacement sometimes could be tricky.
In your case the expansion of (the second) "StringA" variable ends at the percentage sign character right after the double colon;
in this case there is no string operation because the part after the colon is missing, so it expands the environment variable "StringA:", which probably is not defined.
The next environment variable in this file is "=" which also shouldn't be defined.

So the if line after the expansion is (if both variables are undefined):

Code: Select all

If NOT "aba d25 c9.8 v23"=="SearchString" (
The strings "aba d25 c9.8 v23" and "SearchString" are not equal, so it echoes "Yes".

Sidenote: Please use code tags ([code][/code]) around your code.


penpen


Thanks for your response.

Sorry, I'm not familiar with the code tags.
Can you give an example of how you apply it?

penpen
Expert
Posts: 2009
Joined: 23 Jun 2013 06:15
Location: Germany

Re: Test whether a string contains another string

#5 Post by penpen » 27 Apr 2017 16:19

Hendrik_- wrote:Sorry, I'm not familiar with the code tags.
Can you give an example of how you apply it?
Of course; there are two ways to use code tags:
1) When you create or edit a post, you should see some buttons above the editing textarea.
Just click the button with the label "Code" (without doublequotes) and a pair of code tags should be inserted to the editing textarea at the current cursor position.
You could also mark some text in the textarea before clicking the button, and this text is inserted between code tags.

2) You just could type "[code]Insert your code.[/code]" (without doublequotes); result:

Code: Select all

Insert your code.


penpen

ShadowThief
Expert
Posts: 1166
Joined: 06 Sep 2013 21:28
Location: Virginia, United States

Re: Test whether a string contains another string

#6 Post by ShadowThief » 27 Apr 2017 20:30

Image

Hendrik_-
Posts: 3
Joined: 27 Apr 2017 07:14

Re: Test whether a string contains another string

#7 Post by Hendrik_- » 28 Apr 2017 07:38

@penpen and @ShadowThief, thanks for the information.

@Compo, thank you for your suggestion.
I have now discovered that the procedure in my case does not work because it takes place in a "for" loop in a batch file.
I solved the problem by executing the string comparison in a subroutine.
And then the test works.

Anyway, thanks for the help
I've found that the old 'Dos' commando language has many options ...

Aacini
Expert
Posts: 1914
Joined: 06 Dec 2011 22:15
Location: México City, México
Contact:

Re: Test whether a string contains another string

#8 Post by Aacini » 28 Apr 2017 08:53

If the search string changes in a FOR loop, then you may use an additional FOR command to change the delayed expansion of the search string by an equivalent replaceable parameter, and then use the delayed expansion for the base string:

Code: Select all

for /F %%x in ("!SearchString!") do if NOT "%StringA%"=="!StringA:%%~x=!" ( echo Yes...

This method is simpler and run faster than a CALL to a subroutine.

This type of management is fully explained at this post, although the topic is not exactly the same...

Antonio

Post Reply