从字符串c#中提取double值 [英] Extracting double value from a string c#
本文介绍了从字符串c#中提取double值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想从我的字符串中提取双倍
I want to extract the double from my string
buff = "VA VV_CELL1 3.55"
当我使用以下代码时:
When i use the following code:
private void GetLine(string msg, string buff, double numb)
{
comPort.WriteLine(msg);
Thread.Sleep(50);
buff = comPort.ReadExisting();
Thread.Sleep(50);
MatchCollection matches = Regex.Matches(buff, @".*?([-]{0,1} *\d+.\d+)");
List<double> doubles = new List<double>();
foreach (Match match in matches)
{
string value = match.Groups[1].Value;
value = value.Replace(" ", "");
doubles.Add(double.Parse(value));
Thread.Sleep(200);
numb = doubles[0];
}
此代码适用于我的其他字符串,但"CELL1"包含一个数字,所以我没有得到想要的值"3.55"任何想法?
This code work for my other strings but "CELL1" contains a number so i dont get the wanted value "3.55" any ideas?
推荐答案
这将反对buff中提供的值。
This will work against the value provide in buff.
var buff = "VA VV_CELL1 3.55";
var result = System.Text.RegularExpressions.Regex.Replace(buff, "[^0-9.]", "");
double value;
if (double.TryParse(result, out value))
{
MessageBox.Show(
" {value}");
}
else
{
MessageBox.Show ("不是有效的双倍");
}
"{value}"); } else { MessageBox.Show("Not a valid double"); }
这篇关于从字符串c#中提取double值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文