批處理腳本如何比較字符串數組 - Batch script how to compare array of strings -开发者知识库

批處理腳本如何比較字符串數組 - Batch script how to compare array of strings -开发者知识库,第1张

I have array like a[0]= ABC , a[1]=ABC , a[2]=ABC and I want to compare it using :

我有一個像[0] = ABC,一個[1] = ABC,一個[2] = ABC的數組,我想用它來比較它:

if  %a[0]%==%a[1]%=%a[2]% ( echo Equal)

Comparing three is not working but comparing two works fine.

比較三個不起作用,但比較兩個工作正常。

if  %a[0]%==%a[1]% ( echo Equal)

any suggestion, please ?

有什么建議嗎?

2 个解决方案

#1


1  

To check multiple (pseudo-)array elements for equality, you could try this:

要檢查多個(偽)數組元素是否相等,您可以嘗試這樣做:

setlocal EnableDelayedExpansion
set "FLAG=#"   & rem // (flag that is going to be cleared in case of an encountered inequality)
set /A "MAX=9" & rem // (greatest array index number to be included in the comparison loop)
for /L %%I in (1,1,%MAX%) do if not "!a[0]!"=="!a[%%I]!" set "FLAG="
if defined FLAG echo All array elements from index 0 to %MAX% are equal.
endlocal

最佳答案:

本文经用户投稿或网站收集转载,如有侵权请联系本站。

发表评论

0条回复