bat 判断变量字符串中是否包含字符串

bat 判断变量字符串中是否包含字符串

@echo off

rem way 1
set str=machine-order-service
set matchStr=orderd
echo %str% | findstr %matchStr% >nul && echo yes || echo no
rem end way 1
pause

rem way 2
setLocal EnableDelayedExpansion
if not "x!str:%matchStr%=!"=="x%str%" (
    echo Y
) else (
    echo N
)
endlocal
rem end way 2

pause
@echo off rem way 1 set str=machine-order-service set matchStr=orderd echo %str% | findstr %matchStr% >nul && echo yes || echo no rem end way 1 pause rem way 2 setLocal EnableDelayedExpansion if not "x!str:%matchStr%=!"=="x%str%" ( echo Y ) else ( echo N ) endlocal rem end way 2 pause

代码注释:

  在 way 1 中,加了 >nul 就可以不打印 echo 的内容, way1 可以不设置 setlocal

bat 判断变量字符串中是否包含字符串 @echo off rem way 1 set str=machine-order-service set matchStr=orderd echo %str% | findstr %matchStr% >nul && echo yes || echo no rem end way 1 pause rem way 2 setLocal EnableDelayedExpansion if not "x!str:%matchStr%=!"=="x%str%" ( echo Y ) else ( echo N ) endlocal rem end way 2 pause 代码注释:   在 way 1 中,加了 >nul 就可以不打印 echo 的内容, way1 可以不设置 setlocal
经验分享 程序员 微信小程序 职场和发展