3 回答

TA貢獻(xiàn)1829條經(jīng)驗(yàn) 獲得超7個(gè)贊
看HELP FOR例子
或快速嘗試一下
for /F %%a in ("AAA BBB CCC DDD EEE FFF") do echo %%c

TA貢獻(xiàn)1811條經(jīng)驗(yàn) 獲得超5個(gè)贊
遍歷字符串的三個(gè)可能的解決方案:
版本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
當(dāng)字符串包含通配符(例如“ *”或“?”)時(shí),版本1不起作用。
版本1和版本3處理字符,例如'=',';' 或','作為單詞分隔符。這些字符與空格字符具有相同的效果。

TA貢獻(xiàn)1865條經(jīng)驗(yàn) 獲得超7個(gè)贊
以下代碼將使用任意數(shù)量的子字符串分割字符串:
@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 關(guān)注
- 1955 瀏覽
添加回答
舉報(bào)