任何人都可以幫助我用窗口批處理/ DOS腳本刪除一個字符串。窗口批處理/ DOS腳本刪除字符串中的重複單詞
如果字符串 -
test1的test2的test1的TEST3 TEST2 TEST3
我需要一個腳本來顯示爲
TEST1 TEST2 TEST3
任何人都可以幫助我用窗口批處理/ DOS腳本刪除一個字符串。窗口批處理/ DOS腳本刪除字符串中的重複單詞
如果字符串 -
test1的test2的test1的TEST3 TEST2 TEST3
我需要一個腳本來顯示爲
TEST1 TEST2 TEST3
以同樣的方式,你會做手工:取每一個元素,檢查它是否已經在輸出,如果沒有,追加它:
@echo off
setlocal enabledelayedexpansion
set "string=test1 test2 test1 test3 test2 test3"
set "newstring="
for %%i in (%string%) do (
echo !newstring!|findstr /i "\<%%i\>" >nul || set "newstring=!newstring! %%i"
)
echo %newstring:~1%
(注意:如果你想區分大小寫,請刪除/i
)
編輯爲處理完整的單詞而不是(可能的)子字符串。
有幾種方法可以做到這一點;例如:
@echo off
setlocal EnableDelayedExpansion
set "in=test1 test2 tes test1 test3 test test2 test3"
rem 1- Insert the word if it is not in the output already
set "out= "
for %%a in (%in%) do (
if "!out: %%a =!" equ "!out!" set "out=!out!%%a "
)
echo "%out:~1,-1%"
rem 2- Remove each word from output, then insert it again
echo/
set "out= "
for %%a in (%in%) do (
set "out=!out: %%a = !"
set "out=!out!%%a "
)
echo "%out:~1,-1%"
你試過了什麼?請張貼你的例子 – Jeff
嗯...你還在使用80年代/ 90年代的DOS嗎?你的意思是'CMD' - '命令提示符'? – SteveFest