当前位置导航:炫浪网>>网络学院>>网页制作>>ASP.NET教程

字符串中提取数字

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

  (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/>");

  }

相关内容
赞助商链接