Find index of substring or character in string.

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
lstewart
Posts: 2
Joined: 02 Jan 2009 16:12

Find index of substring or character in string.

#1 Post by lstewart » 02 Jan 2009 16:19

Does anyone know how to find the index of a substring or character in a string?

Thanks!

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

#2 Post by jeb » 04 Jan 2009 17:59

Hi lstewart,

try this

Code: Select all

setlocal

set str=abcdefghijklmnopqrstuvxyz

call :indexOf result "%str%" "e"
echo %result%
goto :eof

::::::::::::::::::::::::::::::::::::::::::::::::::::
:indexOf <resultVar> <str> <find>
SETLOCAL
set "str=%~2"
call set "part=%%str:%~3=&rem.%%"
set partB=%part%
call :strLen partB len
( ENDLOCAL
set %~1=%len%
EXIT /b
)


the :strLen function is from the DosTips-Library

hope it helps
Jan Erik

lstewart
Posts: 2
Joined: 02 Jan 2009 16:12

#3 Post by lstewart » 05 Jan 2009 17:17

Thanks!

Post Reply