我收到了EmailIds输入,如'[email protected]','[email protected]',并且想要找到具有文本'companyby'并过滤它们的email-id,但我不知道它在哪里字符串。如何在字符串中找到子字符串?
我想检查c#代码。
我收到了EmailIds输入,如'[email protected]','[email protected]',并且想要找到具有文本'companyby'并过滤它们的email-id,但我不知道它在哪里字符串。如何在字符串中找到子字符串?
我想检查c#代码。
包含对大量数据的速度太慢,再加上它的域和在该中出现次数以及相匹配。
所以使用StartsWith
System.Data.DataTable dt = //Whatever
foreach(System.Data.DataRow dr in dt.Rows)
{
//string email = dr("email");
string email = "[email protected]";
if (email != null && email.StartsWith("companyby", StringComparison.OrdinalIgnoreCase)) {
// do whatever here
}
}
使用LINQ:
var filteredList = IEnumerable<Emails>.Where(email => email != null ? email.StartsWith("companyby", StringComparison.OrdinalIgnoreCase) : false)
对LINQ,你需要有.NET框架> 3.0,你需要添加
using System.Data;
using System.Linq;
因为在有扩展方法。
公平的说,OP说“我不知道它在哪里”。 – 2012-07-18 09:14:12
你是对的,希望我只能写点:) – Freeman 2012-07-18 09:15:21
@J。斯蒂恩:你说得对,我重读了那个。请注意,我错过了公司后的@我的情况... – 2012-07-18 09:35:19
'string.Contains','string.IndexOf' – CodesInChaos 2012-07-18 09:05:30
你甚至尝试过搜索吗? *请做必要*:在发布之前付出一些努力 – V4Vendetta 2012-07-18 09:07:10