Jquery中文网 www.jquerycn.cn
Jquery中文网 >  脚本编程  >  Asp.net  >  正文 怎么判断string字符串存在string[]数组里面

怎么判断string字符串存在string[]数组里面

发布时间:2018-09-10   编辑:www.jquerycn.cn
jquery中文网为您提供怎么判断string字符串存在string[]数组里面等资源,欢迎您收藏本站,我们将为您提供最新的怎么判断string字符串存在string[]数组里面资源
文章讲述了一个简单的关于怎么判断string字符串存在string[]数组里面,我们使用的是命名空间: using System.Collections;来操作的。

问题
从.NET Framework的Library中可以看到,Array有实作IList接口,所以Insus.NET想使用IList.Contains()方法来判断。需要使用命名空间: using System.Collections;。

<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('copy7187')">复制代码</td> </tr> <tr> <td height="auto" colspan="2" valign="top" bgcolor="#FFFFFF" style="padding:10px;" class="copyclass" id=copy7187>


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判断字符串是否重复

[关闭]