如何在 Windows 批处理文件中按空格拆分字符串?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1707058/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 06:53:42  来源:igfitidea点击:

How to split a string by spaces in a Windows batch file?

windowsbatch-filesplit

提问by Cheeso

Suppose I have a string "AAA BBB CCC DDD EEE FFF".

假设我有一个字符串“AAA BBB CCC DDD EEE FFF”。

How can I split the string and retrieve the nth substring, in a batch file?

如何在批处理文件中拆分字符串并检索第 n 个子字符串?

The equivalent in C# would be

C# 中的等价物是

"AAA BBB CCC DDD EEE FFF".Split()[n]

采纳答案by PA.

see HELP FORand see the examples

查看HELP FOR和查看示例

or quick try this

或快速尝试这个

 for /F %%a in ("AAA BBB CCC DDD EEE FFF") do echo %%c

回答by Christian d'Heureuse

Three possible solutions to iterate through the words of the string:

遍历字符串单词的三种可能解决方案:

Version 1:

版本 1:

@echo off & setlocal
set s=AAA BBB CCC DDD EEE FFF
for %%a in (%s%) do echo %%a

Version 2:

版本 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

Version 3:

版本 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

Version 1 does not work when the string contains wildcard characters like '*' or '?'.

当字符串包含“*”或“?”等通配符时,版本 1 不起作用。

Versions 1 and 3 treat characters like '=', ';' or ',' as word separators. These characters have the same effect as the space character.

版本 1 和 3 处理像“=”、“;”这样的字符 或 ',' 作为单词分隔符。这些字符与空格字符具有相同的效果。

回答by Scott E

This is the only code that worked for me:

这是唯一对我有用的代码:

for /f "tokens=4" %%G IN ("aaa bbb ccc ddd eee fff") DO echo %%G 

output:

输出:

ddd

回答by Dave

The following code will split a string with an arbitrary number of substrings:

以下代码将拆分具有任意数量子字符串的字符串:

@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

回答by capdragon

easy

简单

batch file:

批处理文件:

FOR %%A IN (1 2 3) DO ECHO %%A

command line:

命令行:

FOR %A IN (1 2 3) DO ECHO %A

output:

输出:

1
2
3

回答by user2637808

The following code will split a string with N number of substrings with # separated values. You can use any delimiter

以下代码将拆分一个字符串,其中包含 N 个子字符串,其中包含 # 个分隔值。您可以使用任何分隔符

@echo off
if "%1" == "" goto error1

set _myvar="%1"

:FORLOOP
For /F "tokens=1* delims=#" %%A IN (%_myvar%) DO (
    echo %%A
    set _myvar="%%B"
    if NOT "%_myvar%"=="" goto FORLOOP
)

goto endofprogram
:error1
echo You must provide Argument with # separated

goto endofprogram
:endofprogram

回答by p_champ

If someone need to split a string with any delimiter and store values in separate variables, here is the script I built,

如果有人需要用任何分隔符拆分字符串并将值存储在单独的变量中,这是我构建的脚本,

FOR /F "tokens=1,2 delims=x" %i in ("1920x1080") do (
  set w=%i
  set h=%j
)
echo %w%
echo %h%

Explanation: 'tokens' defines what elements you need to pass to the body of FOR, with token delimited by character 'x'. So after delimiting, the first and second token are passed to the body. In the body %i refers to first token and %j refers to second token. We can take %k to refer to 3rd token and so on..

说明:'tokens' 定义了需要传递给 FOR 主体的元素,标记由字符 'x' 分隔。所以在定界之后,第一个和第二个标记被传递给正文。在正文中,%i 指的是第一个标记,而 %j 指的是第二个标记。我们可以用 %k 来指代第三个令牌等等..

Please also type HELP FORin cmd to get a detailed explanation.

还请在 cmd 中键入HELP FOR以获取详细说明。

回答by ecciethetechie

or Powershell for a 0 indexed array.

或 0 索引数组的 Powershell。

PS C:\> "AAA BBB CCC DDD EEE FFF".Split()
AAA
BBB
CCC
DDD
EEE
FFF

PS C:\> ("AAA BBB CCC DDD EEE FFF".Split())[0]
AAA

回答by Jon

set a=AAA BBB CCC DDD EEE FFF
set a=%a:~6,1%

This code finds the 5th character in the string. If I wanted to find the 9th string, I would replace the 6 with 10 (add one).

此代码查找字符串中的第 5 个字符。如果我想找到第 9 个字符串,我会将 6 替换为 10(加一个)。

回答by Bruno

I ended up with the following:

我最终得到了以下结果:

set input=AAA BBB CCC DDD EEE FFF
set nth=4
for /F "tokens=%nth% delims= " %%a in ("%input%") do set nthstring=%%a
echo %nthstring%

With this you can parameterize the input and index. Make sure to put this code in a bat file.

有了这个,您可以参数化输入和索引。确保将此代码放在 bat 文件中。