如何拆分Windows批处理文件中的字符串?

时间:2013-03-12 02:28:41

标签: windows string batch-file split

我正在尝试拆分并在Windows批处理文件中创建一个新字符串,但我无法弄清楚如何执行此操作。

我想转换这个:

Crysis3_2560x1440

对此:

Crysis 3 - 2560x1440

然后:

HD7970CrossFire

对此:

HD 7970 CrossFire

有什么建议吗?

2 个答案:

答案 0 :(得分:2)

编辑:这是管理评论中提到的特殊情况的新版本:

@echo off
setlocal EnableDelayedExpansion

for /F %%a in (inputFile.txt) do (
   set string=%%a
   echo Original: !string!
   call :Splint string
   echo Modified: !string!
)
goto :EOF

:Splint string
set string=!%1!
rem Change underscore by space-dash-space
set string=%string:_= - %
rem Separate the string at first group of digits or space
for /F "tokens=1* delims=0123456789 " %%a in ("%string%") do (
   set first=%%a
   set last=%%b
)
rem Eliminate first part from middle
set middle=!string:%first%=!
rem Eliminate last part from middle, if exists
if defined last set middle=!middle:%last%=!
rem Eliminate spaces from middle
set middle=%middle: =%
rem Assemble the result
if defined middle (
   set %1=%first% %middle% %last%
) else (
   set %1=%first% %last%
)
exit /B

输出:

Original: Crysis3_2560x1440
Modified: Crysis 3 - 2560x1440
Original: HD7970CrossFire
Modified: HD 7970 CrossFire
Original: Skyrim_2560x1440
Modified: Skyrim - 2560x1440
Original: SleepingDogs_2560x1440
Modified: SleepingDogs - 2560x1440
Original: GTX680
Modified: GTX 680

安东尼奥

答案 1 :(得分:1)

您可以使用这些代码段作为基础......

set in=Crysis3_2560x1440
set out=!in:_= - !
set in
set out

和...

set in=HD7970CrossFire
set out=!in:HD7970CrossFire=HD 7970 CrossFire!
set in
set out

虽然我只是不明白你为什么不去

set str=HD 7970 CrossFire