批处理-查找给定字符串中某个字符的出现

时间:2019-03-17 23:10:18

标签: batch-file

我需要自动执行已经为OSX实施的无聊操作。但是,我陷入了“语言”翻译中,因为根据下面的示例,我找不到在下一个字符串中有3个\的方式:

  

.. \ .. \ .. \ ..

PS:我找不到在 CMD 中使作品“ cd-”。

我具有以下创建的功能:

:: Count the occurrences of a certain char in the given string
:: $~1 (input) String
:: $~2 (char) Char to be found & counted
:count_occurrences
set input=%1
set char=%2

set count=0
for %%a in ("%input:\= " "%") do set /A count+=1
echo. %count%
SET %~3=count

EXIT /B 0

但是,它始终返回1。但是,对我来说,根据本示例(https://stackoverflow.com/a/41961590/6093604),它应该返回3

感谢您的帮助!

编辑1(大多数情况下有效)

我的代码如下:

:: Count the occurrences of a certain char in the given string
:: $~1 (input) String
:: $~2 (char) Char to be found & counted
:: $~3 (count) The returned value
:count_occurrences
set "input=%~1"
set "char=%~2"

echo. %input%
for %%a in ("%input:\= " "%") do set /A %~3+=1

EXIT /B 0

如何将参数2 $~2用作要计数的字符?因为,正如我在评论中所说,此处我们仅搜索\

再次感谢!

0 个答案:

没有答案