我有一个问题:
所以我有一个脚本:
set /p "variableA"="Enter a Sentence: "
*Some script I don't know*
echo "%second word of sentence%"
我如何回应输入句子的第二个单词?
根据句子的内容,您还可以将每个单词设置为单独的变量;这样的事情:
@Echo Off
SetLocal EnableDelayedExpansion
Set "sentence="
For /F "Delims==" %%A In ('Set word[ 2^>Nul') Do Set "%%A="
:Loop
Set /P "sentence=Enter a sentence: "
If Not Defined sentence GoTo Loop
Set "i=1"
Set "word[!i!]=%sentence: ="&Set /A i+=1&Set "word[!i!]=%"
For /L %%A In (1,1,%i%) Do Echo %%word[%%A]%%=!word[%%A]!
Pause
正如您在倒数第二行的输出中所看到的(仅为显示目的而添加),第二个单词将是变量%word[2]%
的值。
尝试这个。与for /f
你可以通过分隔符分割字符串(默认的是=
,<space>
,<tab>
,;
,,
)。因此,在下面的代码段中,默认分隔符将充当空间。
set /p "variableA=Enter a Sentence: "
for /f "tokens=2" %%# in ("%variableA%") set "second_word=%%~#"
echo "%second_word%"