bat批處理獲取字符串中字符的索引

首先創建一個文件爲getIndex.bat並輸入一下內容

@echo off 
echo "getindex"
set res=10121211111
rem 獲取字符串索引
set str=%1
set dst=%2
set $res$=%3
set count$=1
set $len$=%str%
call :get
set %$res$%=%errorlevel%
goto :eof
:get
setlocal enabledelayedexpansion
:looper
	if "%$len$%" == "" (
		set /a count$=0
		goto :break
	)
	if "!str:~%count$%,1!" == "%dst%" (
		goto :break
	)
	set $len$=%$len$:~1%
	set /a count$+=1
	goto :looper
:break
exit /b %count$%

然後開始調用該腳本獲取索引:

call getIndex.bat C:\Windows\System32\cmd.exe \ nibi
echo res %nibi%
pause

getIndex.bat 傳入的參數爲  [字符串] [目標字符] [結果變量名]

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章