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:
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
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.Iterate through Words:
for %%i in (%inputString%) do ... loop iterates through each word in inputString.Substring Search:
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.Condition Check:
if !errorlevel! equ 0 checks if findstr found a match (i.e., errorlevel is 0).%%i is the word containing the substring.End of Script:
endlocal ends the local environment, ensuring variables like inputString and substring do not persist outside the script.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.
How to iterate through a string with whitespace delimiter in a Windows batch script?
@echo off setlocal enabledelayedexpansion set "string=This is a sample string" for %%a in (%string%) do ( echo Found word: %%a )
How to find words containing a specific substring in a Windows batch script?
@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 ) )
How to count words in a string separated by whitespace in a Windows batch script?
@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%
How to replace words containing a substring in a Windows batch script?
@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!
How to extract words containing a substring from a string in a Windows batch script?
@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 ) )
How to check if a specific word exists in a string separated by whitespace in a Windows batch script?
@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. )
How to trim whitespace from words in a string in a Windows batch script?
@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! )
How to ignore case sensitivity when finding words containing a substring in a Windows batch script?
@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 ) )
How to concatenate words containing a substring from a string in a Windows batch script?
@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!
How to split a string into words and store them in an array in a Windows batch script?
@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]! )
tcp javasound joi pyc react-native-flexbox datetime-parsing javacv for-loop visualization ipywidgets