string - 2 批字符串问题

标签 string batch-file

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/

相关文章:

C - 加密算法

c# - 在 C# 中的字符串列表中查找字符串

sql-server - 我如何批量读取用户名列表并在 sql 语句中使用这些用户名?

windows - 您可以在兼容模式下运行 Windows 批处理文件吗

c# - 使用格式将字符串转换为十进制

string - 我应该如何处理 D 中的 C 字符串?

C - 返回字符指针数组中重复次数最多/出现次数最多的字符串

windows - 合并 csv 会破坏变音字符

batch-file - Windows批处理: Handle errors with rundll32

batch-file - 批处理文件嵌套 For/L 循环并退出它们