C# 在數組中判斷是否存在某個數組值


(1) 第一種方法:

1 int[] ia = {11,22,33};
2 int id = Array.IndexOf(ia,11); // 這里的1就是你要查找的值
3 if(id==-1)
4 // 不存在
5 else
6 // 存在

 

(2) 第二種方法:

1 string[] strArr = {"a","b","c","d","e"};
2 bool exists = ((IList)strArr).Contains("a");
3 if(exists)
4 // 存在
5 else
6 // 不存在

 

注意: 用IList需要using System.Collections;

數組中不能直接indexOf這個方法,需要 Array.IndexOf(x,x);這樣來使用 

 

字符串轉換成數組:str.split(",");括號內按照字符串中的符號來寫

數組轉換成字符串:Array.join("#"),#代表分隔的符號


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM