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("#"),#代表分隔的符号

posted @ 2017-01-05 09:44  骚年丶勿忘初心。  阅读(26205)  评论(0编辑  收藏  举报