/// <summary>
/// 20130118
/// 搜索字符串
/// 塗聚文 締友計算機信息技術有限公司
/// 捷為工作室
/// </summary>
public class StringSearchClass
{
/// <summary>
/// 查找字符串
/// StringIndexOf("1,2,3,4,5,6", "1,8");
/// </summary>
/// <param name="str1">字符串</param>
/// <param name="judgestr">找查的字符串</param>
/// <returns>返回是否存在</returns>
public static bool StringIndexOf(string str1, string searchstr)
{
bool isExist = true;
str1 = "," + str1 + ",";
string[] strs = searchstr.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
foreach (string strtemp in strs)
{
if (str1.IndexOf(',' + strtemp + ',') == -1)
{
isExist = false;
break;
}
}
return isExist;
}
/// <summary>
/// 查找字符串
/// 塗聚文
/// </summary>
/// <param name="str1">字符串</param>
/// <param name="judgestr">找查的字符串</param>
/// <returns></returns>
public static bool StringContains(string str1, string searchstr)
{
bool isExist = true;
str1 = "," + str1 + ",";
string[] strs = searchstr.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
foreach (string strtemp in strs)
{
if (!str1.Contains(',' + strtemp + ','))
{
isExist = false;
break;
}
}
return isExist;
}
/// <summary>
/// 查找字符串
/// 塗聚文
/// </summary>
/// <param name="str1">字符串</param>
/// <param name="judgestr">找查的字符串</param>
/// <returns></returns>
public static bool StringRegex(string str1, string searchstr)
{
bool isExist = true;
str1 = "," + str1 + ",";
string[] strs = searchstr.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
foreach (string strtemp in strs)
{
if (!Regex.Match(str1, strtemp, RegexOptions.IgnoreCase).Success)
{
isExist = false;
}
}
return isExist;
}
}
測出試:
//測試
Stopwatch sw = new Stopwatch();
sw.Start();
// bool s = StringIndexOf("1,2,3,4,5,6", "1,6");//True,所耗時間:00:00:00.0003557
// bool s = StringContains("1,2,3,4,5,6", "1,6");//True,所耗時間:00:00:00.0003625
bool s = StringRegex("1,2,3,4,5,6", "1,6");//False,所耗時間:00:00:00.0286617 //True,所耗時間:00:00:00.0008295 //True,所耗時間:00:00:00.0008237
sw.Stop();
this.textBox1.Text=s.ToString()+",所耗時間:"+sw.Elapsed.ToString();