从变量中删除字符串

huangapple go评论98阅读模式
英文:

Removing Strings From a Variable

问题

我正在编写一个批处理文件,用于循环遍历一个由逗号分隔的字符串列表,并从变量的内容中删除它们。然而,我似乎无法使它正常工作。

我期望变量中的字符串被删除,如下所示:

假设我有一个列表 a,b,c 并且我想检测并从一个变量中删除这些字符,例如 set "var=abcd"

我希望它循环遍历列表中的字符并将它们从变量中删除:

  1. set "var=%var:~a=%"
  2. set "var=%var:~b=%"
  3. set "var=%var:~c=%"

所以输出应该是:

  1. > echo %var%
  2. d
  3. >

然而,使用底部的代码,我得到的结果是:

  1. > echo %var%
  2. ECHO is off.
  3. >

这里发生了什么?

我的代码:

  1. @echo off
  2. setlocal enabledelayedexpansion
  3. @echo on
  4. :: 设置带有“_”结尾的变量列表
  5. set "varlist=a,b,c"
  6. set varlist=%varlist%_
  7. set "var="
  8. set "string=acd"
  9. :: 循环遍历每个字符,直到找到“,”,然后转到 :var 以将其从字符串中删除
  10. :loopvar
  11. set ch=%varlist:~0,1%
  12. set varlist=%varlist:~1%
  13. if "%ch%"=="," goto var
  14. set "var=%var%%ch%"
  15. if "%varlist%"=="_" goto var
  16. goto loopvar
  17. :: 从变量中删除
  18. :var
  19. set "string=%string:~%var%=%"
  20. set "var="
  21. if "%varlist%"=="_" goto end
  22. goto loopvar
  23. :: 显示最终结果
  24. :end
  25. echo %var%
  26. 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:

  1. set "var=%var:~a=%"
  2. set "var=%var:~b=%"
  3. set "var=%var:~c=%"

so the output would be:

  1. > echo %var%
  2. d
  3. >

However, what I am getting is this using the code at the bottom:

  1. > echo %var%
  2. ECHO is off.
  3. >

What is going on here?
(I am fairly new to batch and I could not find anything that would help me)

My code:

  1. @echo off
  2. setlocal enabledelayedexpansion
  3. @echo on
  4. ::Set the variable list, with '_' at the end to make the end of the items
  5. set "varlist=a,b,c"
  6. set varlist=%varlist%_
  7. set "var="
  8. set "string=acd"
  9. ::Loop through each character until ',' is found, then goto :var to remove it from the string
  10. :loopvar
  11. set ch=%varlist:~0,1%
  12. set varlist=%varlist:~1%
  13. if "%ch%"=="," goto var
  14. set "var=%var%%ch%"
  15. if "%varlist%"=="_" goto var
  16. goto loopvar
  17. ::Remove from variable
  18. :var
  19. set "string=%string:~%var%=%"
  20. set "var="
  21. if "%varlist%"=="_" goto end
  22. goto loopvar
  23. ::Display final result
  24. :end
  25. echo %var%
  26. pause

答案1

得分: 0

以下是翻译好的内容:

  1. Mmm... 这是我会这样做的方式:
  2. @echo off
  3. setlocal EnableDelayedExpansion
  4. rem 设置变量列表,以逗号结尾以标记项目的结束
  5. set "varlist=a,b,c,"
  6. set "string=acd"
  7. set "p=%%"
  8. set "var=%varlist:,=& call set "string=!p!string:!var!=!p!"" & set "var=%"
  9. echo %string%

详细的方法描述出现在此线程中。您也可以删除@echo off行并仔细查看执行的代码,以便理解其中的“魔法”... ;)

  1. <details>
  2. <summary>英文:</summary>
  3. Mmm... This is the way I would do it:
  4. @echo off
  5. setlocal EnableDelayedExpansion
  6. rem Set the variable list, with &#39;,&#39; at the end to make the end of the items
  7. set &quot;varlist=a,b,c,&quot;
  8. set &quot;string=acd&quot;
  9. set &quot;p=%%&quot;
  10. set &quot;var=%varlist:,=&quot; &amp; call set &quot;string=!p!string:!var!=!p!&quot; &amp; set &quot;var=%&quot;
  11. echo %string%
  12. 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_... **`;)`**
  13. [1]: https://www.dostips.com/forum/viewtopic.php?f=3&amp;t=6429
  14. </details>

huangapple
  • 本文由 发表于 2023年6月29日 23:29:29
  • 转载请务必保留本文链接:https://go.coder-hub.com/76582525.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定