Batch file: Find if substring is in string (not in a file)

前端 未结 10 2138
深忆病人
深忆病人 2020-11-22 14:14

In a batch file, I have a string abcdefg. I want to check if bcd is in the string.

Unfortunately it seems all of the solutions I\'m find

相关标签:
10条回答
  • 2020-11-22 14:46

    I usually do something like this:

    Echo.%1 | findstr /C:"%2">nul && (
        REM TRUE
    ) || (
        REM FALSE
    )
    

    Example:

    Echo.Hello world | findstr /C:"world">nul && (
        Echo.TRUE
    ) || (
        Echo.FALSE
    )
    
    Echo.Hello world | findstr /C:"World">nul && (Echo.TRUE) || (Echo.FALSE)
    

    Output:

    TRUE
    FALSE
    

    I don't know if this is the best way.

    0 讨论(0)
  • 2020-11-22 14:47

    Yes, you can use substitutions and check against the original string:

    if not x%str1:bcd=%==x%str1% echo It contains bcd
    

    The %str1:bcd=% bit will replace a bcd in str1 with an empty string, making it different from the original.

    If the original didn't contain a bcd string in it, the modified version will be identical.

    Testing with the following script will show it in action:

    @setlocal enableextensions enabledelayedexpansion
    @echo off
    set str1=%1
    if not x%str1:bcd=%==x%str1% echo It contains bcd
    endlocal
    

    And the results of various runs:

    c:\testarea> testprog hello
    
    c:\testarea> testprog abcdef
    It contains bcd
    
    c:\testarea> testprog bcd
    It contains bcd
    

    A couple of notes:

    • The if statement is the meat of this solution, everything else is support stuff.
    • The x before the two sides of the equality is to ensure that the string bcd works okay. It also protects against certain "improper" starting characters.
    0 讨论(0)
  • 2020-11-22 14:48

    For compatibility and ease of use it's often better to use FIND to do this.

    You must also consider if you would like to match case sensitively or case insensitively.

    The method with 78 points (I believe I was referring to paxdiablo's post) will only match Case Sensitively, so you must put a separate check for every case variation for every possible iteration you may want to match.

    ( What a pain! At only 3 letters that means 9 different tests in order to accomplish the check! )

    In addition, many times it is preferable to match command output, a variable in a loop, or the value of a pointer variable in your batch/CMD which is not as straight forward.

    For these reasons this is a preferable alternative methodology:

    Use: Find [/I] [/V] "Characters to Match"

    [/I] (case Insensitive) [/V] (Must NOT contain the characters)

    As Single Line:

    ECHO.%Variable% | FIND /I "ABC">Nul && ( Echo.Found "ABC" ) || ( Echo.Did not find "ABC" )
    

    Multi-line:

    ECHO.%Variable%| FIND /I "ABC">Nul && ( 
      Echo.Found "ABC"
    ) || (
      Echo.Did not find "ABC"
    )
    

    As mentioned this is great for things which are not in variables which allow string substitution as well:

    FOR %A IN (
      "Some long string with Spaces does not contain the expected string"
      oihu AljB
      lojkAbCk
      Something_Else
     "Going to evaluate this entire string for ABC as well!"
    ) DO (
      ECHO.%~A| FIND /I "ABC">Nul && (
        Echo.Found "ABC" in "%A"
      ) || ( Echo.Did not find "ABC" )
    )
    
    Output From a command:
    
        NLTest | FIND /I "ABC">Nul && ( Echo.Found "ABC" ) || ( Echo.Did not find "ABC" )
    
    As you can see this is the superior way to handle the check for multiple reasons.
    
    0 讨论(0)
  • 2020-11-22 14:48

    To find a text in the Var, Example:

    var_text="demo string test"
    Echo.%var_text% | findstr /C:"test">nul && (
        echo "found test" 
        ) || Echo.%var_text% | findstr /C:"String">nul && (
                 echo "found String with S uppercase letter" 
        ) || (
                 echo "Not Found " 
        )
    

    LEGEND:

    1. & Execute_that AND execute_this
    2. || Ex: Execute_that IF_FAIL execute this
    3. && Ex: Execute_that IF_SUCCESSFUL execute this
    4. >nul no echo result of command
    5. findstr
    6. /C: Use string as a literal search string
    0 讨论(0)
提交回复
热议问题