如何检查变量是否批量包含字母/短语?

问题描述

因此,我正在批量制作一个假冒的Messenger东西,以便与人们打成一片,我想让它看起来更逼真。基本上,我需要能够搜索变量以查看其是否包含单词?如果有道理。我问过的每个人都说过要使用findstr函数,但是我不能真正做到这一点,因为它不是一个单独的文本文件

我想知道是否有办法做

set /p input=thing (a variable for user input)
if %thing% contains "o" echo yes

我不知道这是否有意义。抱歉。我是初学者。

非常感谢您阅读本文。我已经花了3个小时寻找这些东西

解决方法

findstr可行,但是您必须考虑所有因素。例如:

findstr /irc:"hi"

将在任何字符串中找到单词Hi,但也会找到highwhich等。因此,要使响应接近100,将花费大量的时间和精力。 %..反正都是为了好玩。试试看。

@echo off
cls
set /p "you=What is your name? "
@echo off & setlocal enabledelayedexpansion
set "cn=0"
for %%i in (Mary Catlin John Steve Emma Candice) do (
    set /a cn+=1
    set "name[!cn!]=%%i"
    echo !cn!. %%i
)
for /l %%a in (1,1,!cn!) do set "ucount=!ucount!%%a"
choice /c !ucount! /m "Hi %you%,Who'd you like to chat to?"
set name=!name[%errorlevel%]!
echo Please wait for !name! to join.. & timeout /t 6 /nobreak>nul 2>&1 & echo !name! is online.

for /F %%a in ('echo prompt $E ^| cmd') do set "ESC=%%a"
:chat
set /p "string=%you%: %ESC%[91m"
(timeout /t 1)>nul 2>&1
call :strings
goto :chat

:hi
echo %ESC%[92m!name!: What's up?%ESC%[0m
goto :eof

:doing
echo %ESC%[92m!name!: I am well,thank you%ESC%[0m
goto :eof

:time
echo %ESC%[92m!name!: It is now %time%%ESC%[0m
goto :eof

:date
echo %ESC%[92m!name!: The current date is %date%%ESC%[0m
goto :eof

:ben_jerry
echo %ESC%[92m!name!: I love Ben ^& Jerry's!!%ESC%[0m
goto :eof

:dude
echo %ESC%[92m!name!: DUDE!!%ESC%[0m
goto :eof

:not_love
echo %ESC%[92m!name!: WOAH!!! You're going to fast,I'm out%ESC%[0m
goto :eof

:strings
echo "%string%" | findstr /irc:"\<hi\>">nul 2>&1 && call :hi
echo "%string%" | findstr /irc:"\<how\>">nul 2>&1 && call :doing
echo "%string%" | findstr /irc:"\<time\>">nul 2>&1 && call :time
echo "%string%" | findstr /irc:"\<date\>">nul 2>&1 && call :date
echo "%string%" | findstr /irc:"\<love\>">nul 2>&1 && call :not_love
echo "%string%" | findstr /irc:"\<ben & jerry's\>">nul 2>&1 && call :ben_jerry
echo "%string%" | findstr /irc:"ice cream">nul 2>&1 && call :dude && call :ben_jerry
if "%string%" == "/exit" color & cls & exit
goto :eof

结果,其中me:是我键入的内容,而`Bot:是响应。 enter image description here

,

您可以在外部命令中使用CMD的set替换来实现。

set /p "thing=thing (a variable for user input) "

if not "%thing%"=="%thing:o=_%"  echo Contains 'o'

以此,您将临时 %thing% 的内容替换为_,然后检查两个字符串是否相等。

如果replace匹配0个结果,则字符串将相等并且不显示任何内容。如果替换了某些字符串,则字符串将不匹配,因此将包含字符。