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

前端 未结 10 2131
深忆病人
深忆病人 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:33

    I'm probably coming a bit too late with this answer, but the accepted answer only works for checking whether a "hard-coded string" is a part of the search string.

    For dynamic search, you would have to do this:

    SET searchString=abcd1234
    SET key=cd123
    
    CALL SET keyRemoved=%%searchString:%key%=%%
    
    IF NOT "x%keyRemoved%"=="x%searchString%" (
        ECHO Contains.
    )
    

    Note: You can take the two variables as arguments.

提交回复
热议问题