英文:
Removing Strings From a Variable
问题
我正在编写一个批处理文件,用于循环遍历一个由逗号分隔的字符串列表,并从变量的内容中删除它们。然而,我似乎无法使它正常工作。
我期望变量中的字符串被删除,如下所示:
假设我有一个列表 a,b,c
并且我想检测并从一个变量中删除这些字符,例如 set "var=abcd"
。
我希望它循环遍历列表中的字符并将它们从变量中删除:
set "var=%var:~a=%"
set "var=%var:~b=%"
set "var=%var:~c=%"
所以输出应该是:
> echo %var%
d
>
然而,使用底部的代码,我得到的结果是:
> echo %var%
ECHO is off.
>
这里发生了什么?
我的代码:
@echo off
setlocal enabledelayedexpansion
@echo on
:: 设置带有“_”结尾的变量列表
set "varlist=a,b,c"
set varlist=%varlist%_
set "var="
set "string=acd"
:: 循环遍历每个字符,直到找到“,”,然后转到 :var 以将其从字符串中删除
:loopvar
set ch=%varlist:~0,1%
set varlist=%varlist:~1%
if "%ch%"=="," goto var
set "var=%var%%ch%"
if "%varlist%"=="_" goto var
goto loopvar
:: 从变量中删除
:var
set "string=%string:~%var%=%"
set "var="
if "%varlist%"=="_" goto end
goto loopvar
:: 显示最终结果
:end
echo %var%
pause
希望这可以帮助你解决问题。
英文:
I am working on a batch file to loop through a list of strings (separated by commas) and remove them from the contents of a variable. However, I can't seem to get it working.
I was expecting for the varaible to have the strings removed from it, like so:
Lets say I have a list of a,b,c
and I wanted to detect and remove those characters from a variable, for example, set "var=abcd"
.
I want it to loop through the characters in the list and remove them from the variable:
set "var=%var:~a=%"
set "var=%var:~b=%"
set "var=%var:~c=%"
so the output would be:
> echo %var%
d
>
However, what I am getting is this using the code at the bottom:
> echo %var%
ECHO is off.
>
What is going on here?
(I am fairly new to batch and I could not find anything that would help me)
My code:
@echo off
setlocal enabledelayedexpansion
@echo on
::Set the variable list, with '_' at the end to make the end of the items
set "varlist=a,b,c"
set varlist=%varlist%_
set "var="
set "string=acd"
::Loop through each character until ',' is found, then goto :var to remove it from the string
:loopvar
set ch=%varlist:~0,1%
set varlist=%varlist:~1%
if "%ch%"=="," goto var
set "var=%var%%ch%"
if "%varlist%"=="_" goto var
goto loopvar
::Remove from variable
:var
set "string=%string:~%var%=%"
set "var="
if "%varlist%"=="_" goto end
goto loopvar
::Display final result
:end
echo %var%
pause
答案1
得分: 0
以下是翻译好的内容:
Mmm... 这是我会这样做的方式:
@echo off
setlocal EnableDelayedExpansion
rem 设置变量列表,以逗号结尾以标记项目的结束
set "varlist=a,b,c,"
set "string=acd"
set "p=%%"
set "var=%varlist:,=& call set "string=!p!string:!var!=!p!"" & set "var=%"
echo %string%
详细的方法描述出现在此线程中。您也可以删除@echo off
行并仔细查看执行的代码,以便理解其中的“魔法”... ;)
<details>
<summary>英文:</summary>
Mmm... This is the way I would do it:
@echo off
setlocal EnableDelayedExpansion
rem Set the variable list, with ',' at the end to make the end of the items
set "varlist=a,b,c,"
set "string=acd"
set "p=%%"
set "var=%varlist:,=" & call set "string=!p!string:!var!=!p!" & set "var=%"
echo %string%
A detailed description of the method used appears at [this thread][1]. You can also remove the `@echo off` line and carefully review the executed code, so you can grasp _the magic_... **`;)`**
[1]: https://www.dostips.com/forum/viewtopic.php?f=3&t=6429
</details>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论