string 批处理文件:查找子字符串是否在字符串中(不在文件中)第 2 部分 - 使用变量
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17602659/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Batch file: Find if substring is in string (not in a file) Part 2 - Using Variables
提问by MAbraham1
In a Windows batch file, I have a string 'abcdefg'. I want to check if 'bcd' is in the string, but I also want each to be in a variable, or pass in a parameter for the string.
在 Windows 批处理文件中,我有一个字符串“abcdefg”。我想检查 'bcd' 是否在字符串中,但我也希望每个都在一个变量中,或者为字符串传入一个参数。
This solution comes close, but uses constants rather than variables. Batch file: Find if substring is in string (not in a file)
这个解决方案很接近,但使用常量而不是变量。 批处理文件:查找子字符串是否在字符串中(不在文件中)
回答by Endoro
try one:
尝试一个:
set "var=abcdefg"
set "search=bcd"
CALL set "test=%%var:%search%=%%"
if "%test%"=="%var%" (echo %search% is not in %var%) else echo %search% in %var% found
set "var=abcdefg"
set "search=bcd"
echo %var%|findstr /lic:"%search%" >nul && echo %search% found || echo %search% not found
回答by MAbraham1
The solution is to use FindStr
and the NULL redirect, >nul
.
解决方案是使用FindStr
NULL 重定向,>nul
.
SET var=%1
SET searchVal=Tomcat
SET var|FINDSTR /b "var="|FINDSTR /i %searchVal% >nul
IF ERRORLEVEL 1 (echo It does't contain Tomcat) ELSE (echo It contains Tomcat)
Save as test.bat
and execute with the parameter to be searched, as follows: test Tomcat7
另存为test.bat
,用要搜索的参数执行,如下: test Tomcat7
C:\>test Tomcat9
It contains Tomcat