我在Windows批处理文件中有以下字符串:
"-String"
字符串还包含了在开头和结尾处的两个引号,就像上面所写的一样。
我希望剥离掉第一个和最后一个字符,以便得到以下字符串:
-String
我尝试了这个:
set currentParameter="-String"
echo %currentParameter:~1,-1%
这会按照预期输出字符串:
-String
但是当我尝试像这样存储编辑后的字符串时,它失败了:
set currentParameter="-String"
set currentParameter=%currentParameter:~1,-1%
echo %currentParameter%
什么都没有被打印出来。我做错了什么?
这真的很奇怪。当我像这样删除字符时,它能正常工作:
set currentParameter="-String"
set currentParameter=%currentParameter:~1,-1%
echo %currentParameter%
它打印出:
-String
但是实际上我的批处理程序有点复杂,所以它不起作用。我将展示我编写的内容:
@echo off
set string="-String","-String2"
Set count=0
For %%j in (%string%) Do Set /A count+=1
FOR /L %%H IN (1,1,%COUNT%) DO (
echo .
call :myFunc %%H
)
exit /b
:myFunc
FOR /F "tokens=%1 delims=," %%I IN ("%string%") Do (
echo String WITHOUT stripping characters: %%I
set currentParameter=%%I
set currentParameter=%currentParameter:~1,-1%
echo String WITH stripping characters: %currentParameter%
echo .
)
exit /b
:end
输出结果为:
.
String WITHOUT stripping characters: "-String"
String WITH stripping characters:
.
.
String WITHOUT stripping characters: "-String2"
String WITH stripping characters: ~1,-1
.
但是我想要的是:
.
String WITHOUT stripping characters: "-String"
String WITH stripping characters: -String
.
.
String WITHOUT stripping characters: "-String2"
String WITH stripping characters: -String2
.
ECHO ist ausgeschaltet (OFF). ~1,-1
- Metalhead89~
就完成了工作...太棒了。非常感谢你。 - Metalhead89