c# 获取字符串中的数字

c# 获取字符串中的数字
///
/// 获取字符串中的数字
///
/// 字符串
/// 数字
public static decimal GetNumber(string str)
{
    decimal result = 0;
    if (str != null && str != string.Empty)
   {
// 正则表达式剔除非数字字符(不包含小数点.)
    str = Regex.Replace(str, @"[^\d.\d]", "");
// 如果是数字,则转换为decimal类型
    if (Regex.IsMatch(str, @"^[+-]?\d*[.]?\d*$"))
    {
    result = decimal.Parse(str);
    }
 }
   return result;
}
///
/// 获取字符串中的数字
///
/// 字符串
/// 数字
public static 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;
}


本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/jiaao/archive/2008/07/09/2628982.aspx

posted @ 2009-11-12 16:52  独行剑  阅读(1189)  评论(0编辑  收藏  举报