Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Boolean Test for Existence of Substring in String?

9,751 views
Skip to first unread message

HumanJHawkins

unread,
May 11, 2010, 6:17:10 PM5/11/10
to
I am trying to construct a simple IF statement that would look like
this in pseudocode:

IF %SubString% IS IN %String% THEN DO Something

I hope to understand this before I go further, but the longer term
goal would be to also do something like this:

IF %SubString1% OR %SubString2% OR %SubString3% IS IN %String% THEN DO
Something

I've seen other places where it appears that people are piping the
string into a FIND command, but I can't seem to get this to work even
with very simple experiments. I would really like to avoid temp files
if possible, though of course if there is no other way I would live
with it.

Many thanks in advance.

foxidrive

unread,
May 11, 2010, 6:24:56 PM5/11/10
to
On Tue, 11 May 2010 15:17:10 -0700 (PDT), HumanJHawkins
<JHaw...@Locutius.Com> wrote:

>I am trying to construct a simple IF statement that would look like
>this in pseudocode:
>
>IF %SubString% IS IN %String% THEN DO Something
>
>I hope to understand this before I go further, but the longer term
>goal would be to also do something like this:
>
>IF %SubString1% OR %SubString2% OR %SubString3% IS IN %String% THEN DO
>Something


This is the simplest method I think.

@echo off
>file.tmp echo/%string%
set substring1=abc
set substring2=def
set substring3=hij
findstr "%substring1% %substring2% %substring3%" file.tmp>nul
if not errorlevel 1 echo found one of the strings
del file.tmp
pause

--
Regards,
Mic

Todd Vargo

unread,
May 11, 2010, 9:06:29 PM5/11/10
to

The IF command does not have to ability to do this. Here is one way you can
use FIND which will return an exit code (ERRORLEVEL) which you can then use
IF to direct the program flow.

Using your subject as the string, we can search for substrings within it.
For this example, I will use ECHO to pipe the string into FIND.

@echo off
setlocal
set string=Boolean Test for Existence of Substring in String
set SubString1=test
echo %string% |find "%SubString1%" > nul
if errorlevel 1 goto notfound
echo "%SubString1%" is found in "%string%".
goto :eof

:notfound
echo "%SubString1%" not found in "%string%".

Note, this will return not found because the comparison is case sensitive.
If you change SubString1 to match the case found in string, then it will be
found. Likewise, To make it case insensitive, simply add the /i switch to
FIND and the string will also be found.

**** OR condition testing ****

Here is a way to test multiple conditions and continue based on the first
one found. This one includes the /i switch for all three FIND commands.

@echo off
setlocal
set string=Boolean Test for Existence of Substring in String
set SubString1=test
set SubString2=sub
set SubString3=exist
echo %string% |find /i "%SubString1%" > nul
if not errorlevel 1 goto found
echo %string% |find /i "%SubString2%" > nul
if not errorlevel 1 goto found
echo %string% |find /i "%SubString3%" > nul
if not errorlevel 1 goto found
echo Neither "%SubString1%", "%SubString2%" or "%SubString3%" were found
echo within "%string%".
goto :eof

:found
echo At least one SubString was found within "%string%".


--
Todd Vargo

(Post questions to group only. Remove "z" to email personal messages)

Matthias Tacke

unread,
May 12, 2010, 2:48:31 AM5/12/10
to
Am 2010-05-12 00:17, schrieb HumanJHawkins:
> I am trying to construct a simple IF statement that would look like
> this in pseudocode:
>
> IF %SubString% IS IN %String% THEN DO Something
>
> I hope to understand this before I go further, but the longer term
> goal would be to also do something like this:
>
Hi,
you can achive this with substring replacing with nothing in string
and comparing with the original string, if they differ substring has
beeen replaced, ergo it is contained in string.
You need delayed expansion for this to work.

> IF %SubString1% OR %SubString2% OR %SubString3% IS IN %String% THEN DO
> Something
>
> I've seen other places where it appears that people are piping the
> string into a FIND command, but I can't seem to get this to work even
> with very simple experiments. I would really like to avoid temp files
> if possible, though of course if there is no other way I would live
> with it.
>

You don't need temp files.

@Echo off&Setlocal EnableDelayedExpansion
set substr1=abc
set substr2=def
set substr3=hij
set "string=This is just a string conataining abc and hij"

Echo ___Test one substr_________________________________________
IF "!string:%substr1%=!" NEQ "%string%" Echo string conatains substr1

Echo ___Test three substrings and tell which____________________
set flag=0
IF "!string:%substr1%=!" NEQ "%string%" Set /A flag+=100
IF "!string:%substr2%=!" NEQ "%string%" Set /A flag+=20
IF "!string:%substr4%=!" NEQ "%string%" Set /A flag+=3
If flag GTR 0 Echo string contains substrings %flag:0=%

Echo ___Use pipe to findstr to check______________________________
echo %string%|findstr "%substr1% %substr2% %substr3%" >NUL ^
&&Echo string contains substr||Echo string doens't contain substr


HTH

--
Regards
Matthias

Cristi .eXPV

unread,
May 12, 2010, 4:25:01 AM5/12/10
to
Another method avoiding files:

@echo off
set string=<your string>
set substring=<your substring>
call :search string %substring%
pause>nul

:search
call set tmp=%%%1:%2=%%
if %tmp%==%string% (echo "%2" was not found in %1) else (echo "%2" was
found in %1)
goto:eof

Twayne

unread,
May 16, 2010, 9:47:03 AM5/16/10
to
In news:bb65faf8-b20b-4a98...@y18g2000prn.googlegroups.com,
HumanJHawkins <JHaw...@Locutius.Com> typed:

HOOOOO, a veratible cornucopia of alternative methods! Saved those to a new
folder under my INbox! Thanks much from a still-learninger<g>.

HTH,

Twayne`


adeel...@gmail.com

unread,
Mar 15, 2016, 8:40:18 AM3/15/16
to
Thanks @Matthias , It helped.
0 new messages