我的 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
.