如何使用批处理查看字符串是否包含子字符串

时间:2015-12-03 22:56:36

标签: string file batch-file substring

目前正在尝试查看字符串(在本例中为文本文件的当前行)是否包含子字符串#。我是批处理的新手,所以我不确定我将如何做这样的事情。这是代码 set substring = #

for /f "delims=," %%a in (Text.txt) do (
    set string = %%a

    //check substring method        

    echo %string%

)

4 个答案:

答案 0 :(得分:4)

echo %%a|find "substring" >nul
if errorlevel 1 (echo notfound) else (echo found)

批处理对SET语句中的空格敏感。 SET FLAG = N将名为“FLAG Space ”的变量设置为值“ Space N”

语法SET "var=value"(其中value可以为空)用于确保任何杂散尾随空格不包含在分配的值中。 set /a可以安全地使用“无报价”。

答案 1 :(得分:2)

作为find的替代方法,您可以使用字符串替换,如下所示:

@echo off
setlocal EnableDelayedExpansion
set "substring=#"
for /f "delims=," %%a in (Text.txt) do (
    set "string=%%a"
    if "!string:%substring%=!"=="!string!" (
        rem string with substring removed equals the original string,
        rem so it does not contain substring; therefore, output it:
        echo(!string!
    )
)
endlocal

此方法使用延迟环境变量扩展。在命令提示符下键入setlocal /?以了解如何启用它,并set /?查看其工作原理(读取!string!而不是%string%等变量)及其含义。 set /?还描述了字符串替换语法。

答案 2 :(得分:1)

我必须创建一个函数:

用作:

SearchText = "Why does the purple cow jump over the moon?"
SearchTerm = "Purple"
CALL:checkIfStringCotainsText "%SearchText%" "%SearchTerm%" RESULT
IF %RESULT%==true(
    ECHO Text Found!
) ELSE (
    ECHO Text NOT Found.
)

请记住,默认情况下它不区分大小写。如果您希望区分大小写,请在通话末尾添加true字样,例如:

SearchText = "Why does the purple cow jump over the moon?"
SearchTerm = "Purple"
CALL:checkIfStringCotainsText "%SearchText%" "%SearchTerm%" RESULT true
REM # Returns false because Purple is capitalized

这些功能位于批处理文件的底部。

:FUNCTIONS
@REM FUNCTIONS AREA
GOTO:EOF
EXIT /B

:checkIfStringCotainsText 
@REM # CHECKS IF A STRING CONTAINS A SUBSTRING
@REM # Returns the %3 as either set to true or false
@REM # Not case sensetive by defualt. But can be set to case sensetive buying adding true as the fourth paramater
@REM # For example: CALL:checkIfStringCotainsText "Whats up SLY Fox?"   "fox"          RESULT              true
@REM #                                                 SearchText     SearchTerm 
   true-or-false    CaseSensetive?
@Rem # Will check if "Whats up SLY Fox?"" contains the text "fox"
@REM # Then check the result with: if %RESULT%==true (Echo Text Found) Else (Text Not Found)
@REM # Remember do not add %RESULT% use only RESULT .Do not add % around RESULT when calling the function.
@REM # Only add % around RESULT when checking the result.
@REM # Make sure to add "SETLOCAL ENABLEDELAYEDEXPANSION" to the top of your Batch File! This is important!
@REM # Make sure you use quotes around SearchText and SearchTerm. For example "SearchText" not SearchText.
@REM # This is because if there is a space inside the SearchText, each space will make it look like a new parameter



SET SearchString=%~1
SET SearchTerm=%~2
@REM #Check if Case Senseitive
IF [%~4]==[true] (
    @REM if %~4 is not set to anything, treat it as the default as false
    @REM - Do nothing as FindStr is normally case sensetive
) ELSE (
    @REM Change both the text and search-term both to lowercase.
    CALL:LCase SearchString SearchString
    CALL:LCase SearchTerm SearchTerm

)
@set containsText=false
@echo DEBUG: Searching for ^|%~2^| inside ^|%~1^|
@echo "!SearchString!" | find "!SearchTerm!" > nul && if errorlevel 0 (set containsText=true)
SET %3=!containsText!
@GOTO:EOF


:LCase
:UCase
@REM Converts Text to Upper or Lower Case
@REM Brad Thone robvanderwoudeDOTcom
:: Converts to upper/lower case variable contents
:: Syntax: CALL :UCase _VAR1 _VAR2
:: Syntax: CALL :LCase _VAR1 _VAR2
:: _VAR1 = Variable NAME whose VALUE is to be converted to upper/lower case
:: _VAR2 = NAME of variable to hold the converted value
:: Note: Use variable NAMES in the CALL, not values (pass "by reference")
SET _UCase=A B C D E F G H I J K L M N O P Q R S T U V W X Y Z
SET _LCase=a b c d e f g h i j k l m n o p q r s t u v w x y z
SET _Lib_UCase_Tmp=!%1!
IF /I "%0"==":UCase" SET _Abet=%_UCase%
IF /I "%0"==":LCase" SET _Abet=%_LCase%
FOR %%Z IN (%_Abet%) DO SET _Lib_UCase_Tmp=!_Lib_UCase_Tmp:%%Z=%%Z!
SET %2=%_Lib_UCase_Tmp%
GOTO:EOF

请记住,您必须在批处理文件的顶部添加“ SETLOCAL ENABLEDELAYEDEXPANSION”,否则这些都将无法正常工作。

SETLOCAL ENABLEDELAYEDEXPANSION
@REM # Remember to add this to the top of your batch file.

答案 3 :(得分:0)

返回“ TRUE / FALSE”的函数。

https://docs.microsoft.com/en-us/windows-server/administration/windows-commands/find

@ECHO OFF
SET Str=Hello

::SET SubStr=el
SET SubStr=EL
::SET SubStr=eZ

CALL :SUBSTRINGCHECK %Str% %SubStr% RESULT
ECHO %RESULT%
PAUSE

:SUBSTRINGCHECK
SET RESULT=FALSE
SET Str=%~1
SET SubStr=%~2
CALL :reset_error
ECHO "%Str%" | FIND /i "%SubStr%" >nul
IF %ERRORLEVEL% EQU 0 (
 Set RESULT=TRUE
)
SET %3=%RESULT%
exit /b

:reset_error
exit /b 0