LastIndexOf in Windows Package

I need to implement a function in a windows script package to get the LastIndexOf character in the given string.

For example: Given the following line, I need to get the last index of the '/' character:

 /name1/name2/name3 ^ 

Therefore, I need to get the value:

 12 
+4
source share
3 answers

(Note. I assume the files are on Windows because, to be honest, I saw only one question requiring the actual DOS batch file here. Most people simply misappropriate β€œDOS” to everything that has a gray box - black and black monospaced text is not knowing what they are talking about.)

Just skip it by updating the index when you go:

 @echo off setlocal enabledelayedexpansion set S=/name1/name2/name3 set I=0 set L=-1 :l if "!S:~%I%,1!"=="" goto ld if "!S:~%I%,1!"=="/" set L=%I% set /a I+=1 goto l :ld echo %L% 
+6
source

Joey's solution works, but the character to search for is hard-coded and is relatively slow.

Below is a parameterized function that is quick and can find any character (except nul) inside the string. I pass the name of the variables containing the string and the character, instead of string literals, so that the function easily supports all characters.

 @echo off setlocal set "test=/name1/name2/name3" set "char=/" ::1st test simply prints the result call :lastIndexOf test char ::2nd test stores the result in a variable call :lastIndexOf test char rtn echo rtn=%rtn% exit /b :lastIndexOf strVar charVar [rtnVar] setlocal enableDelayedExpansion :: Get the string values set "lastIndexOf.char=!%~2!" set "str=!%~1!" set "chr=!lastIndexOf.char:~0,1!" :: Determine the length of str - adapted from function found at: :: http://www.dostips.com/DtCodeCmdLib.php#Function.strLen set "str2=.!str!" set "len=0" for /L %%A in (12,-1,0) do ( set /a "len|=1<<%%A" for %%B in (!len!) do if "!str2:~%%B,1!"=="" set /a "len&=~1<<%%A" ) :: Find the last occurrance of chr in str for /l %%N in (%len% -1 0) do if "!str:~%%N,1!" equ "!chr!" ( set rtn=%%N goto :break ) set rtn=-1 :break - Return the result if 3rd arg specified, else print the result ( endlocal if "%~3" neq "" (set %~3=%rtn%) else echo %rtn% ) exit /b 

No major changes are required to create a more general function :indexOf , which takes an additional argument that determines which detection to find. A negative number may indicate a search in reverse order. So 1 can be 1st, 2nd, -1 last, -2 penultimate, etc.

+8
source

I know this question is a bit outdated, but I needed a function that could find the location of the substring (of any length) inside the string and adapt the dbenham solution for my purposes. This function also works with individual characters within a string, as asked in the original question, and can search for specific instances (as suggested by dbenham).

To use this function, you must pass the actual strings. Dbenham notes that this supports fewer characters than passing actual variables, but I believe this option is more reused (especially with channels).

The third argument accepts the instance to be found, with negative numbers indicating a search from the end. The returned index is the offset from the beginning of the line to the first character in the substring.

 @ECHO off SET search_string=sub CALL :strIndex "The testing subjects subjects to testing." "%search_string%" -2 ECHO %ERRORLEVEL% PAUSE EXIT :strIndex string substring [instance] REM Using adaptation of strLen function found at http://www.dostips.com/DtCodeCmdLib.php#Function.strLen SETLOCAL ENABLEDELAYEDEXPANSION SETLOCAL ENABLEEXTENSIONS IF "%~2" EQU "" SET Index=-1 & GOTO strIndex_end IF "%~3" EQU "" (SET Instance=1) ELSE (SET Instance=%~3) SET Index=-1 SET String=%~1 SET "str=A%~1" SET "String_Length=0" FOR /L %%A IN (12,-1,0) DO ( SET /a "String_Length|=1<<%%A" FOR %%B IN (!String_Length!) DO IF "!str:~%%B,1!"=="" SET /a "String_Length&=~1<<%%A" ) SET "sub=A%~2" SET "Substring_Length=0" FOR /L %%A IN (12,-1,0) DO ( SET /a "Substring_Length|=1<<%%A" FOR %%B IN (!Substring_Length!) DO IF "!sub:~%%B,1!"=="" SET /a "Substring_Length&=~1<<%%A" ) IF %Substring_Length% GTR %String_Length% GOTO strIndex_end SET /A Searches=%String_Length%-%Substring_Length% IF %Instance% GTR 0 ( FOR /L %%n IN (0,1,%Searches%) DO ( CALL SET StringSegment=%%String:~%%n,!Substring_Length!%% IF "%~2" EQU "!StringSegment!" SET /A Instance-=1 IF !Instance! EQU 0 SET Index=%%n & GOTO strIndex_end )) ELSE ( FOR /L %%n IN (%Searches%,-1,0) DO ( CALL SET StringSegment=%%String:~%%n,!Substring_Length!%% IF "%~2" EQU "!StringSegment!" SET /A Instance+=1 IF !Instance! EQU 0 SET Index=%%n & GOTO strIndex_end )) :strIndex_end EXIT /B %Index% 
0
source

Source: https://habr.com/ru/post/1441256/


All Articles