windows - batch iterate string with whitespace delimiter, find word containing substring

Windows - batch iterate string with whitespace delimiter, find word containing substring

To iterate through a string with whitespace delimiters in a Windows batch script and find words containing a specific substring, you can use a for loop along with string manipulation and conditional checks. Here's how you can approach it:

Example Batch Script

Assume you have a string with whitespace-separated words and you want to find words containing a specific substring, such as "sub":

@echo off setlocal enabledelayedexpansion REM Example string with whitespace-separated words set "inputString=apple subliminal banana submerge" REM Substring to search for set "substring=sub" REM Iterate through each word in the inputString for %%i in (%inputString%) do ( REM Check if the word contains the substring echo %%i | findstr /C:"%substring%" > nul if !errorlevel! equ 0 ( echo Found: %%i ) ) endlocal 

Explanation:

  1. Set Variables:

    • inputString: This variable holds the example string with whitespace-separated words.
    • substring: This variable holds the substring you want to search for within each word.
  2. Iterate through Words:

    • The for %%i in (%inputString%) do ... loop iterates through each word in inputString.
  3. Substring Search:

    • Within the loop, echo %%i | findstr /C:"%substring%" > nul checks if the current word (%%i) contains the substring (%substring%).
    • findstr /C:"%substring%" searches for %substring% in the echoed word (%%i).
    • > nul suppresses the output, so only the errorlevel is checked.
  4. Condition Check:

    • if !errorlevel! equ 0 checks if findstr found a match (i.e., errorlevel is 0).
    • If a match is found, it echoes "Found: %%i", where %%i is the word containing the substring.
  5. End of Script:

    • endlocal ends the local environment, ensuring variables like inputString and substring do not persist outside the script.

Notes:

  • Case Sensitivity: By default, findstr is case-insensitive. Use /I option with findstr for case-insensitive search.

  • Whitespace Handling: The script handles whitespace-separated words within inputString.

  • Substring Matching: Adjust %substring% to match the substring you're interested in.

This approach efficiently iterates through the words in the input string and checks each word for the presence of the specified substring, providing a flexible solution for batch processing in Windows environments.

Examples

  1. How to iterate through a string with whitespace delimiter in a Windows batch script?

    • Description: Split a string by whitespace and iterate through each word.
    • Code Implementation:
      @echo off setlocal enabledelayedexpansion set "string=This is a sample string" for %%a in (%string%) do ( echo Found word: %%a ) 
  2. How to find words containing a specific substring in a Windows batch script?

    • Description: Search for words within a string that contain a specified substring.
    • Code Implementation:
      @echo off setlocal enabledelayedexpansion set "string=This is a sample string" set "substring=sample" for %%a in (%string%) do ( echo %%a | findstr /i /c:%substring% > nul if not errorlevel 1 ( echo Found word containing "%substring%": %%a ) ) 
  3. How to count words in a string separated by whitespace in a Windows batch script?

    • Description: Determine the number of words in a string separated by spaces.
    • Code Implementation:
      @echo off setlocal enabledelayedexpansion set "string=This is a sample string" set "count=0" for %%a in (%string%) do ( set /a count+=1 ) echo Number of words: %count% 
  4. How to replace words containing a substring in a Windows batch script?

    • Description: Replace words in a string that contain a specified substring.
    • Code Implementation:
      @echo off setlocal enabledelayedexpansion set "string=This is a sample string" set "substring=sample" set "replacement=newword" set "result=" for %%a in (%string%) do ( echo %%a | findstr /i /c:%substring% > nul if not errorlevel 1 ( set "result=!result! %replacement%" ) else ( set "result=!result! %%a" ) ) echo Result: !result:~1! 
  5. How to extract words containing a substring from a string in a Windows batch script?

    • Description: Extract and display words from a string that contain a specified substring.
    • Code Implementation:
      @echo off setlocal enabledelayedexpansion set "string=This is a sample string" set "substring=sample" for %%a in (%string%) do ( echo %%a | findstr /i /c:%substring% > nul if not errorlevel 1 ( echo Found word containing "%substring%": %%a ) ) 
  6. How to check if a specific word exists in a string separated by whitespace in a Windows batch script?

    • Description: Verify if a particular word exists within a string split by spaces.
    • Code Implementation:
      @echo off setlocal enabledelayedexpansion set "string=This is a sample string" set "searchword=sample" set "found=false" for %%a in (%string%) do ( if /i "%%a"=="%searchword%" ( set "found=true" goto :found ) ) :found if "%found%"=="true" ( echo Word "%searchword%" found in string. ) else ( echo Word "%searchword%" not found in string. ) 
  7. How to trim whitespace from words in a string in a Windows batch script?

    • Description: Remove leading and trailing spaces from each word in a string.
    • Code Implementation:
      @echo off setlocal enabledelayedexpansion set "string= This is a sample string " for %%a in (%string%) do ( set "word=%%a" set "word=!word: =!" echo Trimmed word: !word! ) 
  8. How to ignore case sensitivity when finding words containing a substring in a Windows batch script?

    • Description: Perform a case-insensitive search for words containing a specified substring.
    • Code Implementation:
      @echo off setlocal enabledelayedexpansion set "string=This is a Sample string" set "substring=sample" for %%a in (%string%) do ( echo %%a | findstr /i /c:%substring% > nul if not errorlevel 1 ( echo Found word containing "%substring%": %%a ) ) 
  9. How to concatenate words containing a substring from a string in a Windows batch script?

    • Description: Combine words that contain a specified substring into a single string.
    • Code Implementation:
      @echo off setlocal enabledelayedexpansion set "string=This is a sample string" set "substring=sample" set "result=" for %%a in (%string%) do ( echo %%a | findstr /i /c:%substring% > nul if not errorlevel 1 ( set "result=!result! %%a" ) ) echo Concatenated words containing "%substring%": !result:~1! 
  10. How to split a string into words and store them in an array in a Windows batch script?

    • Description: Split a string by whitespace and store each word in an array for further processing.
    • Code Implementation:
      @echo off setlocal enabledelayedexpansion set "string=This is a sample string" set "index=0" for %%a in (%string%) do ( set /a index+=1 set "word[!index!]=%%a" ) echo Words in array: for /l %%i in (1, 1, %index%) do ( echo !word[%%i]! ) 

More Tags

tcp javasound joi pyc react-native-flexbox datetime-parsing javacv for-loop visualization ipywidgets

More Programming Questions

More Geometry Calculators

More Trees & Forestry Calculators

More Entertainment Anecdotes Calculators

More Gardening and crops Calculators