批處理文件:查找子字符串是否在字符串中(而不是在文件中)在批處理文件中,我有一個字符串。abcdefg..我想檢查一下bcd在繩子里。不幸的是,我找到的所有解決方案檔案對于子字符串,而不是用于子字符串的字符串。有什么簡單的解決辦法嗎?
3 回答

ibeautiful
TA貢獻1993條經驗 獲得超6個贊
if not x%str1:bcd=%==x%str1% echo It contains bcd
%str1:bcd=%
bcd
str1
bcd
@setlocal enableextensions enabledelayedexpansion @echo off set str1=%1 if not x%str1:bcd=%==x%str1% echo It contains bcd endlocal
c:\testarea> testprog hello c:\testarea> testprog abcdef It contains bcd c:\testarea> testprog bcd It contains bcd
這個 if
語句是該解決方案的核心,其他一切都是支持性的。 這個 x
前面的兩邊相等是為了確保字符串 bcd
工作正常。它還可以防止某些“不適當”的起始字符。

皈依舞
TA貢獻1851條經驗 獲得超3個贊
Echo.%1 | findstr /C:"%2">nul && ( REM TRUE ) || ( REM FALSE )
Echo.Hello world | findstr /C:"world">nul && ( Echo.TRUE ) || ( Echo.FALSE ) Echo.Hello world | findstr /C:"World">nul && (Echo.TRUE) || (Echo.FALSE)
TRUE FALSE
- 3 回答
- 0 關注
- 677 瀏覽
添加回答
舉報
0/150
提交
取消