111,129
社区成员
发帖
与我相关
我的任务
分享 string strText = "我爱CSDN我爱CSDN我爱CSDN我爱和 谐CSDN我爱CSDN我爱CSDN";
string key = "和谐";
string key2 = "(?is)";
for (int i = 0; i < key.Length; i++)
{
key2 += key[i].ToString();
if(i < key.Length - 1) key2 += @"\s*";
}
MatchCollection mc = Regex.Match(strText, key2);
foreach (Match m in mc)
{
m.Value;//匹配出来的值
}
string strText = "我爱CSDN我爱CSDN我爱CSDN我爱和 谐CSDN我爱CSDN我爱CSDN";
List<string> listKey = new List<string>() { "和谐", "和 谐" };
List<int> indexs = new List<int>();
listKey.ForEach(s => indexs.Add(strText.IndexOf(s)));
indexs.ForEach(s => { if (s > -1) Console.WriteLine(s); });