[英]BATCH: String substitution sub
I'm trying to make a sub in batch that find a string in a string and replace it with a third one. 我正在尝试制作一个批处理子,在字符串中找到一个字符串并用第三个字符串替换它。 Searching around, I found here that I can erase a string with the
SET
command. 搜索四周,我发现在这里 ,我可以擦除与一个字符串
SET
命令。
So, here's what I tried: 所以,这是我尝试过的:
:modifyString what with [in]
SET _what=%~1
ECHO "%_what%"
SET "_with=%~2
ECHO "%toWith%"
SET _In=%~3
ECHO "%_In%"
SET _In=%_In:%toWhat%=%toWith%%
ECHO %_In%
SET "%~3=%_In%"
EXIT /B
The ECHO
s are there only for "debugging" purposes. ECHO
仅用于“调试”目的。
What I know is that the error is in... 我所知道的是错误在......
SET _In=%_In:%toWhat%=%toWith%%
...because of the % character that close the %_in%
variable. ...因为关闭
%_in%
变量的%字符。
I tried also things such as... 我也尝试过诸如......
SET _In=%_In:!toWhat!=!toWith!%
SET _In=!_In:%toWhat%=%toWith%!
...and others without sense. ......和其他没有感觉的人。
This is the main problem, the other is to return %_In%
in [in]
. 这是主要问题,另一个是在
[in]
返回%_In%
。
Any tips? 有小费吗?
Here is an example using the !
这是一个使用的例子
!
DelayedExpansion method. 延迟扩展方法。
@echo off
setlocal EnableExtensions EnableDelayedExpansion
set "xxString=All your base are belong to us"
set "xxSubString=your base are belong"
set "xxNewSubString=of your bases belong"
echo Before
set xx
echo.
set "xxString=!xxString:%xxSubString%=%xxNewSubString%!"
echo After
set xx
endlocal
pause >nul
Output 产量
Before
xxNewSubString=of your bases belong
xxString=All your base are belong to us
xxSubString=your base are belong
After
xxNewSubString=of your bases belong
xxString=All of your bases belong to us
xxSubString=your base are belong
Fixed Yours 修正你的
@echo off
:: Make sure that you have delayed expansion enabled.
setlocal EnableDelayedExpansion
:modifyString what with [in]
SET "_what=%~1"
SET "_with=%~2"
SET "_In=%~3"
ECHO "%_what%"
ECHO "%_with%"
ECHO "%_In%"
:: The variable names were not the same as the ones
:: defined above.
SET _In=!_In:%_what%=%_with%!
ECHO %_In%
:: This will not change the value of the 3rd parameter
:: but instead will create a new parameter with the
:: value of %3 as the variable name.
SET "%~3=%_In%"
endlocal
EXIT /B
How to do the substring replacement without delayed expansion. 如何在没有延迟扩展的情况下进行子串替换。 Use the
call
command to create two levels of variable expansion. 使用
call
命令创建两个级别的变量扩展。 Use single %
around variables to expand first and double %%
around variables to expand second. 使用单个
%
围绕变量进行扩展,将%%
围绕变量展开以扩展第二个。
@echo off
setlocal EnableExtensions
set "xxString=All your base are belong to us"
set "xxSubString=your base are belong"
set "xxNewSubString=of your bases belong"
echo Before
set xx
echo.
call set "xxString=%%xxString:%xxSubString%=%xxNewSubString%%%"
echo After
set xx
endlocal
pause >nul
Thank you all guys! 谢谢大家!
I paste you what I finally made: 我贴你最后做的:
:modifyString what with in tRtn
set "_in=%~3"
set "_in=!_in:%~1=%~2!"
IF NOT "%~4" == "" SET %~4=%_in%
EXIT /B
EG If I call this sub in this way: EG如果我这样称呼这个子:
SET "str=All your base are belong to us"
SET "toFind=your base are belong"
SET "space=of your bases belong"
ECHO %str%
CALL :modifyString "%toFind%" "%space%" "%str%" string
%string% would became the new corrected string, so if you do... %string%将成为新的更正字符串,所以如果你这样做...
ECHO "%string%"
it would print: 它会打印:
"All of your bases belong to us"
PS I'm sorry if I'm late, but because of my low reputation i had to wait! PS我很抱歉,如果我迟到了,但由于我的低声誉,我不得不等待!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.