3 回答

TA貢獻1811條經驗 獲得超5個贊
遍歷字符串的三個可能的解決方案:
版本1:
@echo off & setlocal
set s=AAA BBB CCC DDD EEE FFF
for %%a in (%s%) do echo %%a
版本2:
@echo off & setlocal
set s=AAA BBB CCC DDD EEE FFF
set t=%s%
:loop
for /f "tokens=1*" %%a in ("%t%") do (
echo %%a
set t=%%b
)
if defined t goto :loop
版本3:
@echo off & setlocal
set s=AAA BBB CCC DDD EEE FFF
call :sub1 %s%
exit /b
:sub1
if "%1"=="" exit /b
echo %1
shift
goto :sub1
當字符串包含通配符(例如“ *”或“?”)時,版本1不起作用。
版本1和版本3處理字符,例如'=',';' 或','作為單詞分隔符。這些字符與空格字符具有相同的效果。

TA貢獻1865條經驗 獲得超7個贊
以下代碼將使用任意數量的子字符串分割字符串:
@echo off
setlocal ENABLEDELAYEDEXPANSION
REM Set a string with an arbitrary number of substrings separated by semi colons
set teststring=The;rain;in;spain
REM Do something with each substring
:stringLOOP
REM Stop when the string is empty
if "!teststring!" EQU "" goto END
for /f "delims=;" %%a in ("!teststring!") do set substring=%%a
REM Do something with the substring -
REM we just echo it for the purposes of demo
echo !substring!
REM Now strip off the leading substring
:striploop
set stripchar=!teststring:~0,1!
set teststring=!teststring:~1!
if "!teststring!" EQU "" goto stringloop
if "!stripchar!" NEQ ";" goto striploop
goto stringloop
)
:END
endlocal
- 3 回答
- 0 關注
- 1961 瀏覽
添加回答
舉報