获取字符串中数字

/// <summary>
/// 获取字符串中数字
/// </summary>
/// <param name="str"></param>
/// <returns></returns>
public int GetNumberInt(string str)
{
int result = 0;
if (str != null && str != string.Empty)
{
// 正则表达式剔除非数字字符(不包含小数点.)
str = Regex.Replace(str, @"[^\d.\d]", "");
// 如果是数字,则转换为decimal类型
if (Regex.IsMatch(str, @"^[+-]?\d*[.]?\d*$"))
{
result
= int.Parse(str);
}
}
return result;
}
posted @ 2011-06-14 12:06  biubiubiu  阅读(302)  评论(0编辑  收藏  举报