[英]Convert.ToDouble non-numeric string
I want to extract the number out of a string that might contain non-numeric characters 我想从可能包含非数字字符的字符串中提取数字
I'm thinking Convert.ToDouble(Regex.Replace(data[i].ToString(), @"ANSWER HERE", ""));
我在想
Convert.ToDouble(Regex.Replace(data[i].ToString(), @"ANSWER HERE", ""));
Example string: "wd123.321dw"
Want to get 123.321 out of that. 示例字符串:
"wd123.321dw"
想得到123.321。
Also for negatives: "we-123.321ew"
for -123.321 也适用于底片:
"we-123.321ew"
Any clues? 有什么线索吗?
Something like this ? 像这样吗?
string n = "wd123.321dw";
var regNumber = new Regex(@"\-?\d+\.?\d+");
var match = regNumber.Match(n);
if ( match.Success )
{
double d;
if ( Double.TryParse(match.Value, out d) )
{
Console.WriteLine("The number is: {0}",d);
}
}
double d_val = Convert.ToDouble(Regex.Replace(data[i].ToString(), @"[^0-9\.\-]", ""));
Try this as your regular expression: 尝试将此作为您的正则表达式:
"0+" + Regex.Escape(CultureInfo.CurrentUICulture.NumberFormat.NumberDecimalSeparator) + "0+"
This will use the decimal separator of your current regional settings from Windows. 这将使用Windows当前区域设置的小数点分隔符。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.