我正在尝试批量编写文本冒险,所以我想知道如何拆分变量,如 set userinput=take book 并将其转换为数组。我希望能够编写一个程序,将字符串拆分为每个空间的数组项。我已经用很多其他语言做到了这一点。还有其他几个类似的问题,但我不觉得他们回答了我的问题。
这个任务可能比你想象的要困难得多。有很多“简单”的方法,但没有一个简单的方法是健壮的。
例如,一个简单的 FOR 循环可以解析出单个标记,但它会被
*
、?
和可能的 "
破坏。
健壮的解决方案需要大量代码。
这是我的解决方案,有大量评论:
@echo off
:: Start out with delayed expansion disabled, and define "user input"
:: I intentionally include "problem" characters
setlocal disableDelayedExpansion
set "userinput=take book! & ;MustPreserve EmptyLinesAreIgnored"
:: Initialize count
set "cnt=0"
:: Enable and use delayed expansion to protect against poison characters
setlocal enableDelayedExpansion
:: Substitute <LineFeed> for each <space>
(set parsed=!userinput: =^
%= Do not remove or alter this line =%
!)
:: Iterate each line, setting delims and eol to <space> to preserve all tokens
for /f "eol= delims= " %%A in ("!parsed!") do (
%= Return to delayed expansion disabled on first iteration, else ! is lost =%
if "!" equ "" endlocal
%= Increment the count =%
set /a cnt+=1
%= Temporarily enable delayed expansion to capture current count in FOR variable =%
setlocal enableDelayedExpansion
for %%N in (!cnt!) do (
endlocal
%= Save the array value =%
set "token.%%N=%%A"
)
)
::Print the results. Safe array access requires delayed expansion
setlocal enableDelayedExpansion
for /l %%N in (1 1 !cnt!) do echo token.%%N=!token.%%N!
没有评论看起来好多了
@echo off
setlocal disableDelayedExpansion
set "userinput=take book! & :MustPreserveColon ;MustPreserve EmptyLinesAreIgnored"
set "cnt=0"
setlocal enableDelayedExpansion
(set parsed=!userinput: =^
%= Do not remove or alter this line =%
!)
for /f "eol= delims= " %%A in ("!parsed!") do (
if "!" equ "" endlocal
set /a cnt+=1
setlocal enableDelayedExpansion
for %%N in (!cnt!) do (
endlocal
set "token.%%N=%%A"
)
)
setlocal enableDelayedExpansion
for /l %%N in (1 1 !cnt!) do echo token.%%N=!token.%%N!
这是输出:
token.1=take
token.2=book!
token.3=&
token.4=;MustPreserve
token.5=EmptyLinesAreIgnored
@echo off
color 08 & Title %~dpnx0
setlocal enabledelayedexpansion
echo.______S__T__A__R__T_______
echo. ArrayNumber: HierarchieChange inside of the for
echo. or start lenght with 1
echo. ArrayPosi Zero is not defined in this example
set array[4]=you
set array=one two
set array=!array! three
set /a lenght=0
for %%a in (!array!) do (
set array[!lenght!]=%%a
set /a lenght+=1
)
echo.___!array[2]!___!array[1]!___!array[0]!
echo.&echo.
echo.******************************************
for /l %%a in (!lenght!,-1, -1) do echo !array[%%a]!
echo._______________________________________________
for /l %%a in (0,1,!lenght!) do echo !array[%%a]!
echo.&echo.____________without any errors____________________
set /a lenght-=1
for /l %%a in (0,1,!lenght!) do echo !array[%%a]!
echo.-----------------------------------------------------
echo.-----------------------------------------------------
echo. but the charPos 1 is zero when u splitt Strings
set splitt=ABCD
echo !splitt!>TSCHHHHHHHHHHUUUUUUUUUUUUUNGBamBam.txt
for %%a in (TSCHHHHHHHHHHUUUUUUUUUUUUUNGBamBam.txt) do set /a chars=%%~za -2
echo. chars: !chars!
echo. char3: !splitt:~2,1!
for /l %%a in (0,1,!chars!) do echo. %%a !splitt:~%%a,1!
echo._________E__N__D__________
timeout 3 >nul
pause