2

我怎样才能拆分字符串

"   This  is  a text  with  spaces    "

那是在变量“字符串”中放入文本部分而不丢失空格?

set string="#   This  is  a text  with  spaces    #"

应该分成

"#   This"
"  is"
"  a"
" text"
"  with"
"  spaces    #"

使用 For /F "delims= " ... 不起作用,因为它消除了所有空格。

是否有一个“简单”的解决方案,或者任何人都可以解释如何逐个字符地解析字符串,这样我就可以将空格计数到第一个字符,然后读取所有字符直到下一个空格,并将计数的空格和读取的字符一起写入一个新的/临时变量??

谢谢

4

5 回答 5

3

是的,我也不是很明白#“空格#”是什么让它保留尾随空格,而所有其他元素保留前面但不保留前面的空格?

哦,好吧,花在提问上的努力=花在回答上的努力。做你想做的。

@if (@a==@b) @end /*

:: batch portion

@echo off
setlocal

call :split "#   This  is  a text  with  spaces    #"
exit /b

:split <string>
cscript /nologo /e:jscript "%~f0" "%~1"
goto :EOF

:: JScript portion */
WSH.Echo(WSH.Arguments(0).match(/\s*\S+/g).join('\n'));

输出:

#
   This
  is
  a
 text
  with
  spaces
    #

更新

如果你想要第一个+第二个,倒数第二个+最终元素加入,修改上面脚本的JScript部分如下:

:: JScript portion */
var m = WSH.Arguments(0).match(/\s*\S+/g);
m[0] = m.shift() + m[0];
m[m.length - 2] += m.pop();
WSH.Echo(m.join('\n'));

输出:

#   This
  is
  a
 text
  with
  spaces    #

如果您希望将每个元素括在引号中,请将最后一行更改如下:

    WSH.Echo('"' + m.join('"\n"') + '"');

输出:

"#   This"
"  is"
"  a"
" text"
"  with"
"  spaces    #"
于 2013-04-17T20:27:09.253 回答
2

I don't see a simple solution in batch, though of course if you can consider powershell or javascript you'll be working with a more appropriate toolset for string manipulation.

Sticking with the batch requirement, you can loop through character by character and "collect" your words with something like this:

@echo off
setlocal enabledelayedexpansion

set "string=   This  is  a text  with  spaces    "

set idx=0
set "word="
set "char="
set "lastchar= "
:loop
if "!string:~%idx%!" equ "" goto :eof
set char=!string:~%idx%,1!
if "%char%" equ " " (
    if "%lastchar%" neq " " (
        echo [%word%]
        set word=%char%
    ) else (
        set word=%word%%char%
    )
) else (
    set word=%word%%char%
)
set lastchar=%char%
set /a idx=%idx%+1
goto loop

This script uses batch's substring feature !string:~%idx%,1 to grab a single character from the string, incrementing idx with each loop. Then it's just a matter of processing the word (echo in this example) when the previous character was not a space and the current one is.

This writes out:

[   This]
[  is]
[  a]
[ text]
[  with]
[  spaces]

Note that I'm ignoring the # you had in your example because I don't understand where they fit in.

于 2013-04-17T16:28:02.493 回答
2

如果我必须完成这个晦涩的任务,我会使用混合 JScript/批处理技术,如rojo 的回答。但是,我会使用我已经编写的 REPL.BAT 实用程序。假设我的 REPL.BAT 在当前文件夹中,或者在 PATH 中的某个位置,那么以下将起作用:

@echo off
setlocal enableDelayedExpansion
set "string=#   This  is  a text  with  spaces    #"

:: Build an "array" of text parts
set cnt=0
for /f delims^=^ eol^= %%A in ('repl "([^ ])(?= )" "$1\n" xs string') do (
  set /a cnt+=1
  set "string!cnt!=%%A"
)

:: Print the array values
for /l %%N in (1 1 %cnt%) do echo string%%N=[!string%%N!]

但如果我想要一个纯批处理解决方案,我会使用下面相当有效的方法:

@echo off
setlocal enableDelayedExpansion
set "string=#   This  is  a text  with  spaces    #"

:: Define LF to contain a single line feed character (0x0A)
set LF=^


:: Above 2 blank lines are critical - DO NOT REMOVE


:: Insert a line feed before every space
for %%n in ("!LF!") do set "string=!string: =%%~n !"

:loop  Remove line feeds sandwiched by spaces
for %%n in ("!LF!") do set "string2=!string: %%~n =  !"
if "!string2!" neq "!string!" (
  set "string=!string2!"
  goto :loop
)

:: Build an "array" of text parts: FOR /F splits the string at line feeds
set /a cnt=0
for /f delims^=^ eol^= %%A in ("!string!") do (
  set /a cnt+=1
  set "string!cnt!=%%A"
)

:: Print out the array values
for /l %%N in (1 1 %cnt%) do echo string%%N=[!string%%N!]

上述两种解决方案都提供以下输出:

string1=[#]
string2=[   This]
string3=[  is]
string4=[  a]
string5=[ text]
string6=[  with]
string7=[  spaces]
string8=[    #]

请注意,如果字符串包含由于延迟扩展,FOR 循环%%A扩展将破坏结果。!这个限制可以通过额外的编码来消除。所有其他发布的使用 FOR 循环的解决方案都受到同样的限制。(至少在我写这篇文章时他们做到了)

于 2013-04-18T00:14:26.900 回答
2

诀窍是用一个空格替换连续的空格,其余的替换为任意字符。假设您的字符串不包含#s 并且不超过 9 个连续空格,您可以试试这个

set st=%st:         = ########%
set st=%st:        = #######%
set st=%st:       = ######%
set st=%st:      = #####%
set st=%st:     = ####%
set st=%st:    = ###%
set st=%st:   = ##%
set st=%st:  = #%

然后你可以用空格解析for /f并替换你的 s#

setlocal enabledelayedexpansion
for /f %%a in ("%st%") do (
  set ss= %%a
  echo !ss:#= !
)  

请注意,set在括号块内需要您启用延迟扩展并使用!语法(请参阅HELP SET

但是这种技术只会提取第一个子字符串。概括地说,您需要另一个技巧,即将空格替换为换行符,以便for /f将逐行循环

请注意,为了获得换行符,您需要保留set命令后的两个空行

set nl=^


rem continue two lines down....
for /f %%a in ("%st: =!nl!%") do (
  set ss= %%a
  set ss=!ss:#= !
  echo [!ss!]
)  
于 2013-04-17T16:53:10.903 回答
2

尝试这个:

@echo off &setlocal enabledelayedexpansion
set "string=#   This  is  a text  with  spaces    #"

set string1=%string%
for %%i in (%string%) do (
    set string1=!string1: %%i = "%%i" !
    set /a strings+=1
)
set string1=#"%string1:~1,-1%"#
set string1=%string1:"= "%
for %%i in (%string1%) do (
    set /a count+=1
    set string2=%%i
    set string2=!string2: "=!
    set string2=!string2:"=!
    if !count! equ 2 (
     set $s1=!$s1!!string2!
    )else if !count! equ %strings% (
        set /a count-=1
        call set $s!count!=%%$s!count!%%!string2!
        ) else set $s!count!=!string2!
)
for /f "tokens=1*delims==" %%i in ('set "$s"') do echo "%%j"    

输出:

"#   This"
"  is"
"  a"
" text"
"  with"
"  spaces    #"
于 2013-04-17T17:32:13.563 回答