2016-02-18 32 views

回答

4

以同样的方式,你会做手工:取每一个元素,检查它是否已经在输出,如果没有,追加它:

@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

编辑为处理完整的单词而不是(可能的)子字符串。

+0

如果某个单词作为其他单词的一部分被包含,则该方法失败;例如:'set“string = test1 test2 tes test1 test3 test2 test3”'。 'tes'既没有'test'字也没有插在输出中 – Aacini

+1

这很容易通过在'findstr/i'中添加字边界来解决'\'我想。 – rojo

2

有几种方法可以做到这一点;例如:

@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%" 
相关问题