gpt4 book ai didi

string - 2 批字符串问题

转载 作者:行者123 更新时间:2023-12-04 05:35:03 24 4
gpt4 key购买 nike

1)是否有任何内置可以告诉我变量的内容是否只包含大写字母?

2)有没有办法查看一个变量是否包含一个字符串?例如,我想查看变量 %PATH% 是否包含 Ruby。

最佳答案

对于第 1 部分,findstr是答案。您可以将正则表达式功能与 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

在这种情况下,重要的是您在 echo %xxokay% 之间没有空格。和管道字符 | ,因为这将导致输出一个空格,这不是您可以接受的字符之一。

对于第 2 部分, findstr也是答案( /i 是忽略大小写,这可能是您想要的 - 如果大小写必须匹配,则将其关闭):
> 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

在这两种情况下更改脚本的行为。

例如,ruby 检查的代码段可能类似于:
: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

关于string - 2 批字符串问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2634959/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com