gpt4 book ai didi

windows - 从 Windows 批处理文件中的字符串中删除第一个和最后一个字符

转载 作者:可可西里 更新时间:2023-11-01 12:41:24 25 4
gpt4 key购买 nike

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

最佳答案

希望对您有所帮助。

    setlocal enabledelayedexpansion

echo String WITHOUT stripping characters: %%I
set currentParameter=%%I
set currentParameter=!currentParameter:~1,-1!
echo String WITH stripping characters: !currentParameter!

关于windows - 从 Windows 批处理文件中的字符串中删除第一个和最后一个字符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12074510/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com