c# 获取字符串中的数字
来源:互联网 发布:java连接池用线程写 编辑:程序博客网 时间:2024/06/10 03:03
c# 获取字符串中的数字
///
/// 获取字符串中的数字
///
/// 字符串
/// 数字
例子1:
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;
}
例子2:
///
/// 获取字符串中的数字
///
/// 字符串
/// 数字
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
离子3:
public string IsNum(String str)
{
string ss="";
for (int i = 0; i < str.Length; i++)
{
if (Char.IsNumber(str, i) == true)
{
ss += str.Substring(i, 1);
}
else
{
if (str.Substring(i, 1) == ",")
{
ss += str.Substring(i, 1);
}
}
}
return ss;
}
Console.Write(IsNum("aaa139504928bbb11ccc,888"));//你们看下效果就知道了.
说明:
使用正则表达式的效果:
string str1 = "Cycle Layer: 0";
string str11 = "Cycle Nr.: 0";
string str2 = Regex.Replace(str1, @"[^\d.\d]", "");//包括小数点
string str22 = Regex.Replace(str11, @"[^\d\d]", "");//不包括小数点
DateTime.Now.ToLongDateString() //日期
DateTime.Now.ToLongTimeString() //时间
DateTime datetime = DateTime.Now.ToLocalTime(); //{2010-1-22 18:36:35}
string str0 = datetime.Date.ToString();//"2010-1-22 0:00:00"
string strf1 = DateTime.Now.ToLongDateString(); // "2010年1月22日"
string strf2 = DateTime.Now.ToLongTimeString(); //"18:38:32"
DateTime str3 = datetime.Date; //{2010-1-22 0:00:00}
string str4 = DateTime.Now.ToShortDateString(); //"2010-1-22"
- c# 获取字符串中的数字
- c# 获取字符串中的数字
- C# 获取字符串中的数字
- c# 获取字符串中的数字
- c# 获取字符串中的数字
- C#正则获取字符串中的数字
- 获取字符串中的数字
- 函数--获取字符串中的数字
- matlab 获取字符串中的数字
- 获取 Java 字符串中的数字
- Java 获取String中的数字:获取最后一串数字字符串
- oracle 获取中文中的数字、字符串中的数字
- as.net 获取字符串中的数字
- jq获取一串字符串中的数字
- C#取出字符串中的数字或字母
- C#去除字符串中的中文、字母、数字
- C#中的字符串转换为数字
- C#中如何获取numericUpDown中的数字
- PS快捷键
- linux块设备基础知识(来源于国嵌)
- 选项卡tab页jquery实现
- 编写一个程序,开启3个线程,这3个线程的ID分别为A、B、C,每个线程将自己的ID在屏幕上打印10遍,要求输出结果必须按ABC的顺序显示;如:ABCABC….依次递推。
- gtest安装
- c# 获取字符串中的数字
- 块设备驱动程序框架
- PCA人脸识别
- The Destroy/Create Cycle of Activities
- 写一函数,取汉字
- 块设备驱动程序分析之框架构建
- How to Uninstall Acoustica CD Label Maker
- 块设备驱动程序分析之内存模拟磁盘
- JavaScript共同工具类库jutil