2批次字符串问题 [英] 2 batch string questions
问题描述
1)是否有任何内置的,可以告诉我,如果一个变量的内容只包含大写字母?
1) Is there any built-in which can tell me if a variable's contents contain only uppercase letters?
2)有没有什么办法,看看是否变量包含一个字符串?例如,我想看看如果变量%PATH%含有红宝石。
2) is there any way to see if a variable contains a string? For example, I'd like to see if the variable %PATH% contains Ruby.
推荐答案
有关部分1, FINDSTR
就是答案。你可以使用正则表达式的功能以及错误级别
:
For part 1, findstr
is the answer. You can just use the regex feature along with errorlevel
:
> set xxokay=ABC
> set xxbad=AB1C
> echo %xxokay%|findstr /r "^[A-Z]*$" >nul:
> echo %errorlevel%
0
> echo %xxbad%|findstr /r "^[A-Z]*$" >nul:
> echo %errorlevel%
1
这是在这种情况下,你做的重要的是不要的有回声%xxokay%
和管道字符空格 |
,因为这将导致空间被输出这不是你上可接受的角色之一
It's important in this case that you do not have a space between the echo %xxokay%
and the pipe character |
, since that will result in a space being output which is not one of your acceptable characters.
有关第2部分, FINDSTR
也是答案( / I
是忽略这可能是你想要的情况下, - 离开它,如果情况必须匹配):
For part 2, findstr
is also the answer (/i
is ignore case which may be what you want - leave it off if case must match):
> set xxruby=somewhere;c:\ruby;somewhere_else
> set xxnoruby=somewhere;somewhere_else
> echo %xxruby%|findstr /i ruby >nul:
> echo %errorlevel%
0
> echo %xxnoruby%|findstr /i ruby >nul:
> echo %errorlevel%
1
您可以再使用:
if %errorlevel%==1 goto :label
要改变这两种情况下你的脚本的行为。
to change the behaviour of your script in both cases.
例如,对于红宝石检查code段可能是这样的:
For example, the code segment for the ruby check could be something like:
:ruby_check
echo %yourvar%|findstr /i ruby >nul:
if %errorlevel%==1 goto :ruby_check_not_found
:ruby_check_found
:: ruby was found
goto :ruby_check_end
:ruby_check_not_found:
:: ruby was NOT found
:ruby_check_end
这篇关于2批次字符串问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!