很简单的字符串拆分问题 如何拆分出像"123.45mps","305.5KB"中的数字?请高手帮忙. 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 // 获取字符串中的数字 public static int GetNumberInt(string str) { int result = 0; if (str != null && str != string.Empty) { // 正则表达式剔除非数字字符(不包含小数点.) str = Regex.Replace(str, @"[^\d.\d]", ""); // 如果是数字,则转换为int类型 if (Regex.IsMatch(str, @"^[+-]?\d*[.]?\d*$")) { result = int.Parse(str); } } return result; }用这个方法,把你的字符串传进去就是了 下面代码可以取出字符串中的数字(包括带小数的数字),字符串中包含多个数字也能取出。public static string[] GetNums(string strIn){ string[] strOut = null; if (!string.IsNullOrEmpty(strIn)) { MatchCollection mc = Regex.Matches(strIn, @"\d+[.]*\d*"); if (mc.Count > 0) { strOut = new string[mc.Count]; for (int i = 0; i < mc.Count; i++) { strOut[i] = mc[i].Value; } } } return strOut;} public static double GetNumber(string str) { double result = 0; if (str != null && str != string.Empty) { // 正则表达式剔除非数字字符(不包含小数点.) str = Regex.Replace(str, @"[^\d.\d]", ""); // 如果是数字,则转换为int类型 if (Regex.IsMatch(str, @"^[+-]?\d*[.]?\d*$")) { result = double.Parse(str); } } return result; } treeview展开后,怎么隐藏节点的图片? String的split方法如何使用 C#实现服务端生成 C#控制台程序改为winForm 就不能用了 Socket程序 excel读取工作部名称? crystalreport 预览与实际打印不一样 关于silverlight的安装问题 新手用vs2005的两个困惑 高手指点迷津阿!! 部署ASP.NET Ajax web site 出现的问题。急!! C#中如何判断字符串路径是否合法 C# 视频
// 获取字符串中的数字
public static int GetNumberInt(string str)
{
int result = 0;
if (str != null && str != string.Empty)
{
// 正则表达式剔除非数字字符(不包含小数点.)
str = Regex.Replace(str, @"[^\d.\d]", "");
// 如果是数字,则转换为int类型
if (Regex.IsMatch(str, @"^[+-]?\d*[.]?\d*$"))
{
result = int.Parse(str);
}
}
return result;
}
用这个方法,把你的字符串传进去就是了
public static string[] GetNums(string strIn)
{
string[] strOut = null; if (!string.IsNullOrEmpty(strIn))
{
MatchCollection mc = Regex.Matches(strIn, @"\d+[.]*\d*"); if (mc.Count > 0)
{
strOut = new string[mc.Count]; for (int i = 0; i < mc.Count; i++)
{
strOut[i] = mc[i].Value;
}
}
} return strOut;
}
{
double result = 0;
if (str != null && str != string.Empty)
{
// 正则表达式剔除非数字字符(不包含小数点.)
str = Regex.Replace(str, @"[^\d.\d]", "");
// 如果是数字,则转换为int类型
if (Regex.IsMatch(str, @"^[+-]?\d*[.]?\d*$"))
{
result = double.Parse(str);
}
}
return result;
}