批量比较一个字符串

时间:2016-03-18 20:53:43

标签: batch-file compare

我想将输入字符串与另一个字符串进行比较,如果它不匹配,我希望它将其写入文档。我想出了这个:

echo Correct this sentence:
echo ----------------------
echo.
echo 'me has been shoppping with my freinds'

set sentence1=
set /p sentence1=Answer:
echo Your answer was: "%sentence1%"
pause

那里有人有提示吗?

1 个答案:

答案 0 :(得分:0)

如果您将句子保存到文件,则可以使用简单的%sentence1%==%sentence2%或使用find

if not %sentence1%==%sentence2% (
    echo %sentence1% > file.txt
    ::to append use ">>"
)

echo 'me has been shoppping with my freinds' > file.txt
find "%sentence1%" in "file.txt"
if %errorlevel%==1 (
    echo Sentence is not the same
)

并且find也只有一个单词就足够了,如果文件中有一个单词,它就会像找到句子那样表现。例如,"me"会将句子标记为相同,但"me with"不会。如果您想要针对错误定位特定的字/单词组合,则secnd选项可能很有用。 "me has"