如何从字符串中提取多个十进制值 [英] how to extract multiple decimal value from a string

查看:83
本文介绍了如何从字符串中提取多个十进制值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨朋友们,

如何从字符串中提取小数值

我的字符串格式如下:

Hi Friends,
How to extract decimal values from a string
My string format is given below

string message = "Validated AMT_RETENT in TBILL_DETAIL Table: [E]64.45 - [A]64.46";





我需要一个返回两个类似于这些值的函数



I need a function which return two values similar to these

public static ArrayList GetTwoNumbers(string message)
       {
          ArrayList ar = new ArrayList();
          ar[0] =64.45;
          ar[1] = 64.46;
          return ar;
       }

推荐答案

使用正则表达式。

这种情况​​下的Regex模式是

Use Regex.
The Regex pattern in this case is
\d+.\d+



表示由点分隔的一个或多个数字(\d +)。如果你想要固定的小数,比如说2,那么就把{2}添加到最后一个,如下所示:


which means one or more number of digits (\d+) separated by a dot. If you want to have fixed number of decimal, say 2, then add {2} to the last one like this:

\d+.\d+{2}



尝试适应示例:


Try to adapt from the example:

using System;
using System.Text.RegularExpressions;

public class Program
{
    public static void Main()
    {
        string str = "Validated AMT_RETENT in TBILL_DETAIL Table: [E]64.45 - [A]64.46";
        Match match = Regex.Match(str, @"\d+.\d+");
        while (match.Success)
        {
            Console.WriteLine(match.Value);
            match = match.NextMatch();
        }
    }
}



结果将是:


The outcome will be:

64.45
64.46



了解详情:

1. 30分钟正则表达式教程 [ ^ ]

2. http://www.dotnetperls.com/regex-match [ ^ ]


这篇关于如何从字符串中提取多个十进制值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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