批处理文件在字符串中查找子字符串
Batch file finding a substring in a string
我正在尝试创建一个函数,用于在另一个字符串中查找一个字符子字符串。我不能让它工作。 (SET "_gf=!_strif!:~!_cf!,1) 线路不工作。
注意:下面写的函数是工作修正版:
@ECHO off
setlocal enabledelayedexpansion
SET "_stri=abcdefghij"
SET "_strf=c"
SET "_s=0"
CALL :instr %_stri%, %_strf%, %_s%
echo Found at %_result%. position >> fun.txt
goto :eof
:instr
SETLOCAL
SET "_strif=%1"
SET "_strff=%2"
SET "_sf=%3"
SET "_cf="
SET "_gf="
FOR /L %%a IN (%_sf%,1,10) DO (
SET "_cf=%%a"
SET "_gf=!_strif:~%%a,1!"
IF "!_gf!"=="!_strff!" GOTO ENDF
)
:ENDF
ENDLOCAL & SET "_result=%_cf%"
SET "_gf=!_strif!:~!_cf!,1
^......^ ^...^ Variable start and end as seen by the parser
更好用
SET "_gf=!_strif:~%%a,1!"
我正在尝试创建一个函数,用于在另一个字符串中查找一个字符子字符串。我不能让它工作。 (SET "_gf=!_strif!:~!_cf!,1) 线路不工作。
注意:下面写的函数是工作修正版:
@ECHO off
setlocal enabledelayedexpansion
SET "_stri=abcdefghij"
SET "_strf=c"
SET "_s=0"
CALL :instr %_stri%, %_strf%, %_s%
echo Found at %_result%. position >> fun.txt
goto :eof
:instr
SETLOCAL
SET "_strif=%1"
SET "_strff=%2"
SET "_sf=%3"
SET "_cf="
SET "_gf="
FOR /L %%a IN (%_sf%,1,10) DO (
SET "_cf=%%a"
SET "_gf=!_strif:~%%a,1!"
IF "!_gf!"=="!_strff!" GOTO ENDF
)
:ENDF
ENDLOCAL & SET "_result=%_cf%"
SET "_gf=!_strif!:~!_cf!,1
^......^ ^...^ Variable start and end as seen by the parser
更好用
SET "_gf=!_strif:~%%a,1!"