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