字符串中提取数字

从字符串中提取数字,有两种方法:

(1)code:

String str ="abc.htm?id=16034$a=343";

 

            String count=null;
foreach (char c in str)
{
if (c >= '0' && c <= '9')
{
count
+= c.ToString();
}
else
{
if (count != null)
{
Response.Write(count
+"<br/>");
count
= null;
}
}
}
if (count != null)
{
Response.Write(count);
count
= null;
}
(2) regex:
String str = "abc.htm?id=16034$a=343";
Regex regex
= new Regex(@"\d+");
MatchCollection cc
= regex.Matches(str, 0);
foreach (Match match in cc)
{
Response.Write(match.Value
+"<br/>");
}

 

 

posted @ 2010-09-08 12:22  chenping2008  阅读(535)  评论(1编辑  收藏  举报