C#字符串模糊匹配方法总结

对应的字符串形式
方法一
List list = new List();
string s = “{1}{2}{3}”;
int i = s.IndexOf("{");
int j = s.IndexOf("}");
while (i > -1 && j > -1)
{
string ss = s.Substring(i + 1, j - i -1);
list.Add(ss);
s = s.Substring(j + 1);
i = s.IndexOf("{");
j = s.IndexOf("}");
}
foreach (string str in list) MessageBox.Show("1 "+str);

//方法二
List list = new List();
s = “{1}{2}{3}”;
MatchCollection mc= Regex.Matches(s, @"{(.*?)}");
foreach (Match m in mc)
list.Add(m.Groups[1].Value);
foreach (string str in list)
MessageBox.Show("2 "+str);

对应的字符数组形式
string[] str = { “[1]”, “[2]”, “[3]”};
string[] str2 = str.Where(s => s.StartsWith("[") && s.EndsWith("]")).Select(s => s.Substring(1, s.Length - 2)).ToArray();

猜你喜欢

转载自blog.csdn.net/qq_30725967/article/details/88970181