怎么判断string字符串存在string[]数组里面
问题
从.NET Framework的Library中可以看到,Array有实作IList接口,所以Insus.NET想使用IList.Contains()方法来判断。需要使用命名空间: using System.Collections;。
string a = "Q";
string[] F = { "A", "B", "C" };
bool exist = ((IList)F).Contains(a);
解决办法
Array继承了IEnumerable接口,就可以用Contains方法,这个方法是Enumerable类的,
<table width="620" align="center" border="0" cellpadding="1" cellspacing="1" style="background:#FB7"> <tr> <td width="464" height="27" bgcolor="#FFE7CE"> 代码如下</td> <td width="109" align="center" bgcolor="#FFE7CE" style="cursor:pointer;" onclick="doCopy('copy5331')">复制代码</td> </tr> <tr> <td height="auto" colspan="2" valign="top" bgcolor="#FFFFFF" style="padding:10px;" class="copyclass" id=copy5331>public static bool Contains<TSource>(this IEnumerable<TSource> source, TSource value){
ICollection<TSource> is2 = source as ICollection<TSource>;
if (is2 != null)
{
return is2.Contains(value);
}
return source.Contains<TSource>(value, null);
}
这是这个方法的内部代码,用reflector看Enumerable类提供的方法.
您可能感兴趣的文章:
怎么判断string字符串存在string[]数组里面
ASP中字符与数字操作函数总结
python 怎么判断字符串开头
PHP字符串操作的一些函数
C# 判断字符串为空有哪几种方法
php 字符编码转换方法
python如何判断字符串以什么结尾
数字、浮点、布尔型、字符串和数组(php教程一)
Scala 字符串
php判断字符串是否重复