c# 获取字符串中的数字
2017-02-24 13:40
176 查看
x
x
→原文链接←
x
对原文的修改:
/// <summary> /// 获取字符串中的数字 /// </summary> /// <param name="str">字符串</param> /// <returns>数字</returns> public static decimal GetNumber(string str) { decimal result = 0; if (str != null && str != string.Empty) { // 正则表达式剔除非数字字符(不包含小数点.) //str = Regex.Replace(str, @"[^/d./d]", ""); str = Regex.Replace(str, @"[^\d.\d]", ""); // 如果是数字,则转换为decimal类型 if (Regex.IsMatch(str, @"^[+-]?\d*[.]?\d*$")) { result = decimal.Parse(str); } } return result; } /// <summary> /// 获取字符串中的数字 /// </summary> /// <param name="str">字符串</param> /// <returns>数字</returns> 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; }
x
→原文链接←
x
对原文的修改:
str = Regex.Replace(str, @"[^/d./d]", ""); //修改:"/"修改为"\" str = Regex.Replace(str, @"[^\d.\d]", "");
相关文章推荐
- c# 获取字符串中的数字
- C# 获取字符串中的数字
- c# 获取字符串中的数字
- c# 获取字符串中的数字
- c# 获取字符串中的数字[转载]
- c# 获取字符串中的数字
- C#算法函数:获取一个字符串中的最大长度的数字
- c# 获取字符串中的数字
- C# 获取字符串中的数字
- c# 获取字符串中的数字
- 判断一个字符串是否全是数字的多种方法及其性能比较(C#实现)
- C#中处理字符串和数字
- 类库------对一些字符串进行操作的类1.判断输入是否数字2.截取字符串函数3.过滤输入信息4.生成随机数5.生成验证码图片6.获取汉字第一个拼音7.半角转全角8.全角转半角
- 16进制字符串转数字(C/C++,VB/VB.net,C#)
- C#(IsNumeric) 字符串转换为数字的4种方法(转)
- [C#]将普通数字转换成带前置0的字符串的简便方法
- c#中如何判断字符串是否为数字
- C#中判断字符串是否由数字组成
- C#判断字符串为数字
- C#(IsNumeric) 字符串转换为数字的4种方法(转)