C# 一个字符串是否在另外一个字符串数组里 Array.Exists 的用法 Array.IndexOf 用法
2011-08-22 21:15 通用C#系统架构 阅读(9588) 评论(2) 编辑 收藏 举报其实一行代码就可以了,不用写那么多循环,先看看老外写的代码
using System;
class Program
{
static void Main()
{
string[] array = { "cat", "dot", "perls" };
// Use Array.Exists in different ways.
bool a = Array.Exists(array, element => element == "perls");
bool b = Array.Exists(array, element => element == "python");
bool c = Array.Exists(array, element => element.StartsWith("d"));
bool d = Array.Exists(array, element => element.StartsWith("x"));
// Output.
Console.WriteLine(a);
Console.WriteLine(b);
Console.WriteLine(c);
Console.WriteLine(d);
}
class Program
{
static void Main()
{
string[] array = { "cat", "dot", "perls" };
// Use Array.Exists in different ways.
bool a = Array.Exists(array, element => element == "perls");
bool b = Array.Exists(array, element => element == "python");
bool c = Array.Exists(array, element => element.StartsWith("d"));
bool d = Array.Exists(array, element => element.StartsWith("x"));
// Output.
Console.WriteLine(a);
Console.WriteLine(b);
Console.WriteLine(c);
Console.WriteLine(d);
}
string[] SetSelectIds = string[] { "aa", "bb", "cc" };
string id = string "bb";
if (Array.Exists(SetSelectIds, element => element.Equals(id)))
Array.IndexOf(SetSelectIds, id) >= 0;
将权限管理、工作流管理做到我能力的极致,一个人只能做好那么很少的几件事情。