从字符串中提取数字

发布于 2024-11-30 23:22:04 字数 614 浏览 1 评论 0原文

我正在尝试操作一个字符串。

例如:

string DiagnosesString  "250.00 03 350.0001 450.00 01 550.00 02";

输出

250.00
350.00
450.00
550.00

我尝试使用 Split 方法。正如您在 350.0001 中的示例中看到的,其输出是 350.00。

我可以删除 03 、 02 等。

问题: 1.) 我如何删除 350.0001 的 o1 只得到 350.00?

这是我的代码:

string DiagnosisCodestemp = DiagnosesString.Replace(" 01 ", " ").Replace(" 02 ", " ").Replace(" 03 ", " ").Replace(" 04 ", " ").Replace(" 05 ", " ").Replace(" 06 ", " ").Replace(" 07 ", " ");
string[] DiagnosisCodesParts = DiagnosisCodestemp.Split();

I'm trying to manipulate a string..

For example:

string DiagnosesString  "250.00 03 350.0001 450.00 01 550.00 02";

Output

250.00
350.00
450.00
550.00

I tried to use a Split method. As you can see on my example in 350.0001 and its output is 350.00.

I can removed 03 , 02 , etc..

Question:
1.) How I will removed the o1 of 350.0001 get only 350.00?

here's my code:

string DiagnosisCodestemp = DiagnosesString.Replace(" 01 ", " ").Replace(" 02 ", " ").Replace(" 03 ", " ").Replace(" 04 ", " ").Replace(" 05 ", " ").Replace(" 06 ", " ").Replace(" 07 ", " ");
string[] DiagnosisCodesParts = DiagnosisCodestemp.Split();

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(12

青柠芒果 2024-12-07 23:22:04
var DiagnosisCodesParts =
    DiagnosesString.Split(' ')
                   .Where(s => !s.StartsWith("0"))
                   .Select(s => Decimal.Parse(s))
                   .Select(d => d.ToString("0.00");
var DiagnosisCodesParts =
    DiagnosesString.Split(' ')
                   .Where(s => !s.StartsWith("0"))
                   .Select(s => Decimal.Parse(s))
                   .Select(d => d.ToString("0.00");
倾城月光淡如水﹏ 2024-12-07 23:22:04

如何执行 Decimal.TryParse 并格式化十进制数字。这也将清除不良价值观。

string DiagnosesString = "250.00 03 350.0001 450.00 01 550.00 02"; 
var diagnoses = DiagnosesString.Split(' ');
foreach(var diagnosis in diagnoses)
{
  decimal temp;
  if(diagnosis.Contains(".") && Decimal.TryParse(diagnosis, out temp))
  {
    // do something
    var value = temp.ToString("0.00");
  }
}

How about doing a Decimal.TryParse and format the decimal numbers. THis will also weed out the bad values.

string DiagnosesString = "250.00 03 350.0001 450.00 01 550.00 02"; 
var diagnoses = DiagnosesString.Split(' ');
foreach(var diagnosis in diagnoses)
{
  decimal temp;
  if(diagnosis.Contains(".") && Decimal.TryParse(diagnosis, out temp))
  {
    // do something
    var value = temp.ToString("0.00");
  }
}
心房的律动 2024-12-07 23:22:04

如果您正在处理数值,我建议将它们解析为实际的数值类型,例如使用 Decimal.ParseDecimal.TryParse。然后,您可以使用格式将其转换回字符串,例如 myNumber.ToString("N2")

If you are dealing with numeric values, I would recommend parsing them into actual numeric types, e.g. using Decimal.Parse or Decimal.TryParse. You can then use formatting to convert it back to a string, e.g. myNumber.ToString("N2")

娜些时光,永不杰束 2024-12-07 23:22:04

您可以将它们解析为双精度。我喜欢使用 XmlConvert 来实现此目的:

foreach (var s in diagnosisCodesParts)
{
    var d = XmlConvert.ToDouble(s);
    Console.WriteLine(string.Format({0:2}, d));
}

You can parse them as a double. I like to use XmlConvert for this:

foreach (var s in diagnosisCodesParts)
{
    var d = XmlConvert.ToDouble(s);
    Console.WriteLine(string.Format({0:2}, d));
}
旧城空念 2024-12-07 23:22:04

列表列表=新列表();

       string DiagnosesString  =  "250.00 03 350.0001 450.00 01 550.00 02";

       string DiagnosisCodestemp = DiagnosesString.Replace(" 01 ", " ").Replace(" 02 ", " ").Replace(" 03 ", " ").Replace(" 04 ", " ").Replace(" 05 ", " ").Replace(" 06 ", " ").Replace(" 07 ", " ");

        string[] DiagnosisCodesParts = DiagnosisCodestemp.Split();

        foreach (var item in DiagnosisCodesParts)

        {

            string[] parts = item.Split('.');
            string num = parts[1];
            string finalValue = string.Empty;

            if (num.Length > 2)
            {
                num = num.Replace(num, "00");
                finalValue =  parts[0]+"."+num;
            }
            else
            {
                finalValue = parts[0] + "." + num;
            }

            list.Add(finalValue);
        }

输出:250.00 350.00 450.00 550.00

List list = new List();

       string DiagnosesString  =  "250.00 03 350.0001 450.00 01 550.00 02";

       string DiagnosisCodestemp = DiagnosesString.Replace(" 01 ", " ").Replace(" 02 ", " ").Replace(" 03 ", " ").Replace(" 04 ", " ").Replace(" 05 ", " ").Replace(" 06 ", " ").Replace(" 07 ", " ");

        string[] DiagnosisCodesParts = DiagnosisCodestemp.Split();

        foreach (var item in DiagnosisCodesParts)

        {

            string[] parts = item.Split('.');
            string num = parts[1];
            string finalValue = string.Empty;

            if (num.Length > 2)
            {
                num = num.Replace(num, "00");
                finalValue =  parts[0]+"."+num;
            }
            else
            {
                finalValue = parts[0] + "." + num;
            }

            list.Add(finalValue);
        }

o/p : 250.00 350.00 450.00 550.00

甜心 2024-12-07 23:22:04

尝试在空间上进行分割。

string DiagnosisCodestemp = DiagnosesString.Replace(" 01 ", " ").Replace(" 02 ", " ").Replace(" 03 ", " ").Replace(" 04 ", " ").Replace(" 05 ", " ").Replace(" 06 ", " ").Replace(" 07 ", " "); 
string[] DiagnosisCodesParts = DiagnosisCodestemp.Split(' '); 

这应该会生成一个包含值的数组,您可以过滤所需的值。然后,您可以将这些值转换为小数并四舍五入到 2 位以删除多余的值。

Try aplitting on the space.

string DiagnosisCodestemp = DiagnosesString.Replace(" 01 ", " ").Replace(" 02 ", " ").Replace(" 03 ", " ").Replace(" 04 ", " ").Replace(" 05 ", " ").Replace(" 06 ", " ").Replace(" 07 ", " "); 
string[] DiagnosisCodesParts = DiagnosisCodestemp.Split(' '); 

This should produce an array with values and you can filter for the ones you need. You can then convert the values to decimal and round to 2 places to remove the extra values.

怪我鬧 2024-12-07 23:22:04

1)将字符串转换为十进制
2) 将小数点四舍五入至小数点后 2 位
3)将其转换回字符串

1) convert the string to a decimal
2) round the decimal to 2 decimal places
3) convert it back to a string

待"谢繁草 2024-12-07 23:22:04

字符串DiagnosesString =“250.00 03 350.0001 450.00 01 550.00 02”;

string DiagnosisCodestemp = DiagnosesString.Replace("01","").Replace("02","").Replace("03","").Replace("04","").Replace("05" ,“ ”)。替换(“ 06 ”,“ ”)。替换(“ 07 ”,“ ”);

        string[] DiagnosisCodesParts = DiagnosisCodestemp.Split();

        foreach (var item in DiagnosisCodesParts)

        {

            string[] parts = item.Split('.');
            string num = parts[1];
            if (num.Length > 2)
            {
                num = num.Replace(num, "00");
            }

        }

string DiagnosesString = "250.00 03 350.0001 450.00 01 550.00 02";

string DiagnosisCodestemp = DiagnosesString.Replace(" 01 ", " ").Replace(" 02 ", " ").Replace(" 03 ", " ").Replace(" 04 ", " ").Replace(" 05 ", " ").Replace(" 06 ", " ").Replace(" 07 ", " ");

        string[] DiagnosisCodesParts = DiagnosisCodestemp.Split();

        foreach (var item in DiagnosisCodesParts)

        {

            string[] parts = item.Split('.');
            string num = parts[1];
            if (num.Length > 2)
            {
                num = num.Replace(num, "00");
            }

        }
梦言归人 2024-12-07 23:22:04

好吧,我不确定我完全理解你的问题,但我会尝试。

  1. 你们像以前一样分手了。
  2. 您过滤/忽略长度为 2 或更小的所有字符串。
  3. 将数组中剩下的字符串转换为双精度。
  4. 您将这些值四舍五入到小点后的 2 个数字。

Ok, I'm not sure I completely understood your question, but I'll try.

  1. You split as you've done before.
  2. You filter/ignore all strings with a length of 2 or less.
  3. You convert the strings left in your array to double.
  4. You round those values to 2 numbers after the point.
落叶缤纷 2024-12-07 23:22:04

您需要将字符串转换为数值,我选择十进制,因为它不会像双精度或浮点那样丢失精度。然后您可以使用格式字符串说明符将其转换为您想要的十进制形式:

var list = List<string>();
foreach(var s in splitString) {

  // code to rule out 02, 01, etc. goes here

  decimal tmp;
  if (decimal.TryParse(s, out tmp)) {
     var code = tmp.ToString("0.00");
     list.Add(code);
  } else {
     // something went wrong, handle it here
  }
}

You will need to convert the string to a numerical value, I choose decimal as it does not lose precision like double or float. Then you can use the format string specifier to convert it to the decimal form you want:

var list = List<string>();
foreach(var s in splitString) {

  // code to rule out 02, 01, etc. goes here

  decimal tmp;
  if (decimal.TryParse(s, out tmp)) {
     var code = tmp.ToString("0.00");
     list.Add(code);
  } else {
     // something went wrong, handle it here
  }
}
篱下浅笙歌 2024-12-07 23:22:04

您可以使用正则表达式来匹配数字,

/\d+\.\d+/

但这取决于您是否可以更精确地定义您的数字。

decimal number;
string DiagnosesString = "250.00 03 350.0001 450.00 01 550.00 02";

foreach(var num in Regex.Matches(DiagnosesString, @"\d+\.\d+"))
{
    Decimal.TryParse(num.ToString(), out number);
    Console.WriteLine(number.ToString("0.00"));
}

也许您甚至需要这样的正则表达式:

/(\d+.\d\d)\d*/

仅匹配格式为 0.00 的数字...您应该提供更多您希望如何解析数据的数据..

you could match the numbers using a regex

/\d+\.\d+/

but it depends whether you can define your number more precisely or not.

decimal number;
string DiagnosesString = "250.00 03 350.0001 450.00 01 550.00 02";

foreach(var num in Regex.Matches(DiagnosesString, @"\d+\.\d+"))
{
    Decimal.TryParse(num.ToString(), out number);
    Console.WriteLine(number.ToString("0.00"));
}

probably you even need a regex like this:

/(\d+.\d\d)\d*/

to match only numbers in the format 0.00... you should provide more data how you want your data to be parsed..

流云如水 2024-12-07 23:22:04
using System;
using System.Text.RegularExpressions;
class Program
{
    static void Main()
    {
        string input = "250.00 03 350.0001 450.00 01 550.00 02";
        Match match = Regex.Match(input, @"[0-9]+[/.][0-9]+", RegexOptions.IgnoreCase);
        while (match.Success) 
        {
            string key = match.Value;
            Console.WriteLine(String.Format("{0:0.00}", Convert.ToDecimal(key, new CultureInfo("en-US"))));
            match = match.NextMatch();
        }
    }
}
using System;
using System.Text.RegularExpressions;
class Program
{
    static void Main()
    {
        string input = "250.00 03 350.0001 450.00 01 550.00 02";
        Match match = Regex.Match(input, @"[0-9]+[/.][0-9]+", RegexOptions.IgnoreCase);
        while (match.Success) 
        {
            string key = match.Value;
            Console.WriteLine(String.Format("{0:0.00}", Convert.ToDecimal(key, new CultureInfo("en-US"))));
            match = match.NextMatch();
        }
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文