首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > .NET > C# >

判断 一个字符串数组 是否能匹配 一个ArrayList 里的任何一个元素.Tostring(),该如何处理

2012-01-02 
判断 一个字符串数组 是否能匹配 一个ArrayList 里的任何一个元素.Tostring()判断 一个字符串数组 是否能

判断 一个字符串数组 是否能匹配 一个ArrayList 里的任何一个元素.Tostring()
判断 一个字符串数组 是否能匹配 一个ArrayList 里的任何一个元素.Tostring() 

例如:
一个
string[] filePath; //绝对文件全路径
ArrayList _fileSuffix = new ArrayList { "jpg", "jpeg", "jpe", "jfif", "bmp", "png", "tif", "tiff", "gif" };; // 后缀列表

只要 filePath的任何一个元素能等于fileSuffix 的任何一个元素,就返回为true(封装到方法里)



[解决办法]
数据量不大就搞个双循环:

C# code
string[] filePath = { "zswang.txt", "csdn.exe", "a.1" }; //绝对文件全路径ArrayList _fileSuffix = new ArrayList();_fileSuffix.Add("jpg"); // 后缀列表 _fileSuffix.Add("1"); // 后缀列表 bool exists = false;for (int j = 0; j < filePath.Length; j++){    for (int i = 0; i < _fileSuffix.Count; i++)    {        if (string.Compare(Path.GetExtension(filePath[j]),             "." + _fileSuffix[i].ToString(), true) == 0)        {            exists = true;            break;        }    }}Console.WriteLine(exists);
[解决办法]
看你的写法也像C# 3.0吧,那干脆用Linq吧。。。。

string[] filePath = { "txt", "exe", "jpg" }; 
List<string> _fileSuffix = new List<string> { "jpg", "jpeg", "jpe", "jfif", "bmp", "png", "tif", "tiff", "gif" }; ; // 后缀列表
bool b = (from t1 in filePath where _fileSuffix.Contains(t1) select t1).Count() > 0;

[解决办法]
不需要用两次循环,一次就可以了。

string[] filePath;
ArrayList _fileSuffix = new ArrayList();

public bool check()
{
bool b = false;
for (int i = 0; i < filePath.Length; i++)
{
if (_fileSuffix.IndexOf(filePath[i]) > -1)
{
b = true;
break;
}
}
return b;
}


[解决办法]
用ArrayList..::.IndexOf 方法就行啊.

foreach (string s in filePath)
if(_fileSuffix.IndexOf(s)>=0 ) 
return true;
return false;

热点排行