以下是我的代码段:
"cms913_to_9132.json cms9132_to_9133.json"
CMS_FROM_VERSION 和 CMS_TO_VERSION 都是变量
我想从%CMS_SQL%
%CMS_FROM_VERSION% and %CMS_TO_VERSION%
只想获取包含这两个变量值的@+id/
之间的子字符串
不想使用文件来执行此操作,也不希望使用硬编码来获取子字符串,因为这两个字符串是变量。
如何在这两个字符串之间提取子字符串? 感谢
答案 0 :(得分:0)
在 windows cmd
中,这很简单:
set "result=%A:~2,6%"
echo %result%
但也许你的意思是真正的DOS?
(假设你想要6
个字符, starting at character
2 when character
0 is the first in a string-variable named
a` - 变量案例无关紧要)
请参阅文档提示中的set /?
(或SO上的数千个条目中的任意一个)
答案 1 :(得分:0)
做替换:
@echo off
set "string=AB134SSPQS"
set "substr=%string:AB=%"
set "substr=%substr:QS=%"
echo %substr%
答案 2 :(得分:0)
要重新迭代我的注释,以下内容不知道或计算子字符串长度,它只是从字符串中删除前两个和后两个字符:
Set "String=AB134SSPQS"
Set "subString=%String:~2,-2%"
Echo %subString%
如果AB
和QS
在所需的子字符串之前并且成功,但不一定开始并启动字符串,那么这可能适用于您:
Rem Looking for a file.txt substring preceded by AB and succeeded by QS
@Echo Off
For /F "Delims=" %%A In ('FindStr /I "AB.*QS" "file.txt"') Do Call :Sub "%%A"
Pause
GoTo :EOF
:Sub
Set "Line=%~1"
Set "Up2Sub=%Line:*AB=%"
Set "SubStr=%Up2Sub:QS="&:"%"
Echo %SubStr%
Exit /B
我必须编制一个方案(请参阅Rem
),因为您没有提供足够的信息来确定答案。
修改强>
Set "String=AB134SSPQS"
Set "Up2Sub=%String:*AB=%"
Set "SubStr=%Up2Sub:QS="&:"%"
Echo %SubStr%
<强> EDIT2 强>
如您在其他问题中所述,并根据您现在更改的问题添加到此处,这是一种方法:
@Echo Off
Set "CMS_SQL=cms91_to_913.json cms913_to_9132.json cms9132_to_9133.json cms9133_to_10100.json"
Set "CMS_FROM_VERSION=cms913_"
Set "CMS_TO_VERSION=to_9133.json"
For /F "Delims=" %%A In ('Echo %%CMS_SQL:*%CMS_FROM_VERSION%^=%%'
) Do Set "SUBSTRING=%CMS_FROM_VERSION%%%A"
For /F "Delims=" %%A In ('Echo %%SUBSTRING:%CMS_TO_VERSION%^=^&:%%'
) Do Set "SUBSTRING=%%A%CMS_TO_VERSION%"
Set SUBSTRING
Pause
答案 3 :(得分:0)
稍微改变一下:
:: SO50021845.cmd
Echo off
set "CMS_SQL=cms91_to_913.json cms913_to_9132.json cms9132_to_9133.json cms9133_to_10100.json"
set "FROM=cms913_"
set "TO=to_9133.json"
:: Check if CMS_SQL contains FROM and TO
Echo:%CMS_SQL%|Findstr /i "%FROM%.*%TO:.=\.%" >NUL 2>&1 || (
Echo string doesn't contain FROM and TO & Pause & Exit /b
)
:: Remove part before FROM
Call set "Result=%%CMS_SQL:*%FROM%=%FROM%%%"
:: extract part to remove behind TO
Call set "Remove=%%Result:*%TO%=%%"
:: remove part behind TO
Call set "Result=%%Result:%Remove%=%%"
Echo %Result%
示例输出:
cms913_to_9132.json cms9132_to_9133.json