在大字符串中查找浮点数 [英] Finding floating points in a large string

查看:159
本文介绍了在大字符串中查找浮点数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个大字符串,在字符串中有一系列浮点。典型的字符串将具有项目X $ 4.50项目描述\r\\\
\r\\\
项目Z $ 4.75 ...
真的没有韵律或理由文本。我已经有最低的,我需要找到字符串中的所有值。所以如果它是 10.00 ,它会发现$ code> 10.05 或更少的每个值。我会假设某种正则表达式涉及找到值,然后我可以把它们放在一个数组中,然后对它们进行排序。



所以这样就是这样的找到哪些值适合我的标准。

  int [] array; 
int arraysize;
int lowvalue;
int total;

for(int i = 0; i {
if(array [i] == lowvalue * 1.05)++ total;
}

我的问题是在数组中获取这些值。我已经阅读过这个,但d +并不适用于浮点数。 p>

解决方案

您应该使用RegEx:

  Regex r = new RegEx([0-9] + \。[0-9] +); 
匹配m = r.Match(myString);

这样的东西。然后你可以使用:

  float f = float.Parse(m.value); 

如果您需要一个数组:

  MatchCollection mc = r.Matches(myString); 
string [] myArray = new string [mc.Count];
mc.CopyTo(myArray,0);

编辑



我刚刚创建了一个小Joe的示例应用程序我编译它,它在我的机器上使用你的问题的输入行工作正常。如果您有问题,请发布您的InputString,以便我可以尝试。这是我写的代码:

  static void Main(string [] args)
{
const字符串InputString =项目X $ 4.50项目描述\r\\\
\r\\\
项目Z $ 4.75;

var r = new Regex(@[0-9] + \。[0-9] +);
var mc = r.Matches(InputString);
var matches = new Match [mc.Count];
mc.CopyTo(matches,0);

var myFloats = new float [matches.Length];
var ndx = 0;
foreach(match m in matches)
{
myFloats [ndx] = float.Parse(m.Value);
ndx ++;
}

foreach(myFloats中的float f)
Console.WriteLine(f.ToString());

// myFloats现在应该具有所有浮点值
}


I have a large string that has a series of floating points in string. A typical string would have Item X $4.50 Description of item \r\n\r\n Item Z $4.75... There is really no rhyme or reason for the text. I have the lowest already and I need to find all the values in the string. So if it was 10.00 it would find every value that is 10.05 or less. I would assume that some sort of regex would involved to find the values and then I could put them in an array then sort them.

So it would be something like this to find the which of those values fit my criteria.

int [] array;
int arraysize;
int lowvalue;
int total;

for(int i = 0; i<arraysize; ++i)
{
    if(array[i] == lowvalue*1.05) ++total;
}

My problem is getting those values in the array. I have read this but d+ does not really work with floating points.

解决方案

You should use RegEx:

Regex r = new RegEx("[0-9]+\.[0-9]+");
Match m = r.Match(myString);

Something like that. Then you can just use:

float f = float.Parse(m.value);

If you need an array:

MatchCollection mc = r.Matches(myString);
string[] myArray = new string[mc.Count];
mc.CopyTo(myArray, 0);

EDIT

I just created a small sample application for you Joe. I compiled it and it worked fine on my machine using the input line from your question. If you are having problems, post your InputString so I can try it out with that. Here is the code I wrote:

static void Main(string[] args)
{
    const string InputString = "Item X $4.50 Description of item \r\n\r\n Item Z $4.75";

    var r = new Regex(@"[0-9]+\.[0-9]+");
    var mc = r.Matches(InputString);
    var matches = new Match[mc.Count];
    mc.CopyTo(matches, 0);

    var myFloats = new float[matches.Length];
    var ndx = 0;
    foreach (Match m in matches)
    {
        myFloats[ndx] = float.Parse(m.Value);
        ndx++;
    }

    foreach (float f in myFloats)
        Console.WriteLine(f.ToString());

    // myFloats should now have all your floating point values
}

这篇关于在大字符串中查找浮点数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆