【发布时间】:2020-09-30 16:08:58
【问题描述】:
在批处理中,我尝试将字符串拆分为子字符串,最好基于分隔符。
这个例子有效,但有没有更好的方法来做到这一点:
for /f "tokens=1-20 delims=:" %%a in (string.txt) do (
echo %%a& echo.%%b& echo.%%c& echo.%%d& echo.%%e& echo.%%f& echo.%%g& echo.%%h& echo.%%i& echo.%%j
echo.%%k& echo.%%l& echo.%%m& echo.%%n& echo.%%o& echo.%%p& echo.%%q& echo.%%r& echo.%%s& echo.%%t
) >substrings.txt
我尝试过 Magoo 的 this code,它适用于默认分隔符,但使用冒号失败:
@echo off
setLocal
for /f "delims=:" %%a in (string.txt) do (
for %%i in (%%a) do echo %%i >>substrings.txt
)
我也尝试过来自 Aacini 的 this code,但使用特殊字符似乎失败了。
@echo off
setlocal EnableDelayedExpansion
set i=1
set "x=%string%"
set "x!i!=%x::=" & set /A i+=1 & set "x!i!=%"
set x >substrings.txt
%string% var 或 string.txt 的示例是:
Select project:/option:report name="Sustainability":option value="201":Huonville:/option:/report:report name="Energy and Water Management":option value="102":Cygnet:/option:/report:report name="Tree Management":option value="101":Cygnet:/option:/report:option disabled="disabled":/option:/select
我正在尝试将 %string% var 或 string.txt 输出到 substring.txt,如下所示:
Select project
/option
report name="Sustainability"
option value="201"
Huonville
/option
/report
report name="Energy and Water Management"
option value="102"
Cygnet
/option
/report
report name="Tree Management"
option value="101"
Cygnet
/option
/report
option disabled="disabled"
/option
/select
任何帮助将不胜感激。谢谢
【问题讨论】:
标签: batch-file split substring