关于网友提出的“ aspnet中字符串截取问题,急”问题疑问,本网通过在网上对“ aspnet中字符串截取问题,急”有关的相关答案进行了整理,供用户进行参考,详细问题解答如下:
问题: aspnet中字符串截取问题,急描述:
string pic="[1:15.00%];[2:0.00%];[3:0.00%];[4:0.00%];[5:0.00%];[6:0.00%];[7:0.00%];[8:0.00%];[9:0.00%];[10:0.00%];[11:0.00%];[12:0.00%];[13:0.00%];[14:0.00%];[15:0.00%];[16:0.00%];[17:0.00%];[18:0.00%];[19:0.00%];[20:0.00%];[21:0.00%];[22:0.00%];[23:0.00%];[24:0.00%];[25:0.00%];[26:0.00%];[27:0.00%];[28:0.00%];[29:0.00%];[30:0.00%];[31:0.00%];";
如何把这样的一个字符串把“:”前后的值分别截取出来?
最好能储存到List中
解决方案1:
少了个后面的
string pic = "[1:15.00%];[2:0.00%];[3:0.00%];[4:0.00%];[5:0.00%];[6:0.00%];[7:0.00%];[8:0.00%];[9:0.00%];[10:0.00%];[11:0.00%];[12:0.00%];[13:0.00%];[14:0.00%];[15:0.00%];[16:0.00%];[17:0.00%];[18:0.00%];[19:0.00%];[20:0.00%];[21:0.00%];[22:0.00%];[23:0.00%];[24:0.00%];[25:0.00%];[26:0.00%];[27:0.00%];[28:0.00%];[29:0.00%];[30:0.00%];[31:0.00%];";
Regex regex1 = new Regex(@"(?<=\[)\d+(?=:)");
Regex regex2 = new Regex(@"(?<=:).+?(?=%)");
Listlist1 = new List ();
Listlist2 = new List ();
foreach (Match match in regex1.Matches(pic))
{
list1.Add(Convert.ToInt32(match.Value));
Response.Write(match.Value + " ");
//1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
}
foreach (Match match in regex2.Matches(pic))
{
list2.Add(match.Value + " ");
Response.Write(match.Value + " ");
//15.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00
}