Page 1 of 1
Test whether a string contains another string
Posted: 27 Apr 2017 07:42
by Hendrik_-
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?
Re: Test whether a string contains another string
Posted: 27 Apr 2017 11:06
by Compo
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
Re: Test whether a string contains another string
Posted: 27 Apr 2017 11:31
by penpen
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
Re: Test whether a string contains another string
Posted: 27 Apr 2017 15:10
by Hendrik_-
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?
Re: Test whether a string contains another string
Posted: 27 Apr 2017 16:19
by penpen
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:
penpen
Re: Test whether a string contains another string
Posted: 27 Apr 2017 20:30
by ShadowThief
Re: Test whether a string contains another string
Posted: 28 Apr 2017 07:38
by Hendrik_-
@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 ...
Re: Test whether a string contains another string
Posted: 28 Apr 2017 08:53
by Aacini
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