批处理文件:如果找到的子字符串(而不是在一个文件中) [英] Batch file: Find if substring is in string (not in a file)
问题描述
在一个批处理文件,我有一个字符串 ABCDEFG
。我想,如果来检查BCD
是的字符串中。
In a batch file, I have a string abcdefg
. I want to check if bcd
is in the string.
不幸的是,似乎所有我发现搜索解决方案的文件的一个子字符串,而不是一个子字符串。
Unfortunately it seems all of the solutions I'm finding search a file for a substring, not a string for a substring.
有没有简单的解决方案,为这个?
Is there an easy solution for this?
推荐答案
是的,你可以使用替换和核对原始字符串:
Yes, you can use substitutions and check against the original string:
@setlocal enableextensions enabledelayedexpansion
@echo off
set str1=%1
if not x%str1:bcd=%==x%str1% echo It contains bcd
endlocal
的%STR1:BCD =%
位将在 STR1替换
为空字符串,使其从原来的不同。 BCD
The %str1:bcd=%
bit will replace a bcd
in str1
with an empty string, making it different from the original.
如果原来没有包含在其中的 BCD
字符串时,修改后的版本将是相同的。
If the original didn't contain a bcd
string in it, the modified version will be identical.
测试:
c:\testarea> testprog hello
c:\testarea> testprog abcdef
It contains bcd
c:\testarea> testprog bcd
It contains bcd
一对夫妇的注意事项:
A couple of notes:
- 的
如果
语句是该解决方案的肉,其他一切都是支持的东西。 - 的
X
面前人人平等的双方是保证字符串BCD
工作好。它还可以防止某些不当开始字符。
- The
if
statement is the meat of this solution, everything else is support stuff. - The
x
before the two sides of the equality is to ensure that the stringbcd
works okay. It also protects against certain "improper" starting characters.
这篇关于批处理文件:如果找到的子字符串(而不是在一个文件中)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!