Batch inString()-like function?

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
JWinslow23
Posts: 58
Joined: 17 Jun 2014 10:38

Batch inString()-like function?

#1 Post by JWinslow23 » 28 Aug 2014 09:05

I have searched for a long time for a function meeting my needs, but I haven't found it yet.

Is there a function that allows you to search for a substring or character in another string, and that returns the position of the first occurrence of the string? Example:

Code: Select all

set original=abcdefghij
set substring=bcd
:inString original substring result
echo %result%
::Should output 2
set original=bc
:inString original substring result
echo %result%
::Should output 0

I could make my own function like this, but I'm wondering if such a function exists.

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Batch inString()-like function?

#2 Post by foxidrive » 28 Aug 2014 09:15

What character sets does it have to support?

JWinslow23
Posts: 58
Joined: 17 Jun 2014 10:38

Re: Batch inString()-like function?

#3 Post by JWinslow23 » 28 Aug 2014 10:15

foxidrive wrote:What character sets does it have to support?


I only need it to support ASCII characters 32-127.

einstein1969
Expert
Posts: 941
Joined: 15 Jun 2012 13:16
Location: Italy, Rome

Re: Batch inString()-like function?

#4 Post by einstein1969 » 29 Aug 2014 02:10

I thinks that 2 step resolve this.

It's possible to check if there'is a substring.

then if there is substring it's possible found the index else return 0

einstein1969

einstein1969
Expert
Posts: 941
Joined: 15 Jun 2012 13:16
Location: Italy, Rome

Re: Batch inString()-like function?

#5 Post by einstein1969 » 29 Aug 2014 03:24

i have developed two versions. The last should be faster.

Code: Select all

:inString_ string substring index -- returns the position of substring in string
::
  setlocal EnableDelayedExpansion
    set O=!%1!
    set S=!%2!
    set ris=!O:*%S%=!
    set i=0
    if NOT %ris%==%O% (
      For /f %%f in ('cmd /u /c echo %O%^|find /v ""') do if not defined break (
        if "%S:~0,1%"=="%%f" set break=true
        set /a i+=1
      )
    )
 
 ( endlocal & set %3=%i% )
exit /b

:inString string substring index -- returns the position of substring in string
::
  setlocal EnableDelayedExpansion
    set O=!%1!
    set S=!%2!
    set ris=!O:*%S%=!
    set i=0
    if NOT %ris%==%O% (
      set ris=!O:%ris%=!
      set ris=!ris:%S%=!
      call :strlen Ris i
      set /a i+=1
    )
 
 ( endlocal & set %3=%i% )
exit /b


I have not optimized for fast execution .

Einstein1969

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Batch inString()-like function?

#6 Post by foxidrive » 29 Aug 2014 04:39

JWinslow23 wrote:
foxidrive wrote:What character sets does it have to support?


I only need it to support ASCII characters 32-127.


ASCII 34, 37 are often a problem, and 33 too if delayed expansion is used.

ASCII 127 may also be an issue and 94 will be an issue if CALL is used.

JWinslow23
Posts: 58
Joined: 17 Jun 2014 10:38

Re: Batch inString()-like function?

#7 Post by JWinslow23 » 29 Aug 2014 20:20

Thank you. I shall use this whenever I can.

Post Reply