0
点赞
收藏
分享

微信扫一扫

C#判断是否含有某个值


C#中怎么判断一个数组中是否存在某个数组值

(1) 第一种方法:

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




(2) 第二种方法:

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




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



c#字符串是否包含某个字符

string str = "abCdefg";
bool result = str.Contains('C'); // 包含大写字符C 返回 true
int intResult = str.IndexOf("C"); // 包含大写字符串C 返回该字符串所在的起始位置,即:2。若不包含则返回-1


因此也可以这样表示是否包含某内容:
if(intResult > -1) {
    // 找到! 
}




举报

相关推荐

0 条评论