Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/309.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 需要从字符串中解析十进制值的建议_C#_.net_Parsing_String - Fatal编程技术网

C# 需要从字符串中解析十进制值的建议

C# 需要从字符串中解析十进制值的建议,c#,.net,parsing,string,C#,.net,Parsing,String,我需要将数量解析为十进制,将货币代码解析为字符串 输入字符串 302 600.00 RUB 10 000.00 USD 模式是 数量->空间->货币代码 两位小数 空格为千位分隔符,点为十进制分隔符 这是没有测试,但你可以这样做 string text = "302 600.00 RUB"; decimal amount; string type; var lastspace = text.lastIndexOf(" "); decimal.TryParse(text.substri

我需要将数量解析为十进制,将货币代码解析为字符串

输入字符串

302 600.00 RUB
10 000.00 USD
模式是

  • 数量->空间->货币代码
  • 两位小数
  • 空格为千位分隔符,点为十进制分隔符

  • 这是没有测试,但你可以这样做

    string text = "302 600.00 RUB";
    decimal amount;
    string type;
    
    var lastspace = text.lastIndexOf(" ");    
    decimal.TryParse(text.substring(0, lastspace - 1), out amount);
    type = text.substring(lastspace + 1);
    
    希望这有帮助

    using System.Text.RegularExpressions;
    
    [……]

    string sInput=“302 10 600.00 RUB”;
    //字符串sInput=“302 600.00摩”;
    //字符串sInput=“10000.00美元”;
    var rgmResult=Regex.Match(sInput,@“^(?\d+)(\d{1,3}(\d{3})*\.\d{2})(?[A-Z]{3})$”;
    字符串sQuantity=rgmResult.Groups[“数量”]值;
    字符串sPrice=rgmResult.Groups[“price”].Value;
    字符串sCurrency=rgmResult.Groups[“cur”].Value;
    
    您可以将正则表达式与累加器一起使用,自定义数字格式

    string pattern = @"(?<decimal>[0-9]{0,3}(\s[0-9]{3})*(.[0-9]+){0,1})\s" +
                     @"(?<currency>[a-zA-Z]+){1}";
    
    string input = "302 600.00 RUB\r\n10 000.00 USD"; 
    
    // Get text that matches regular expression pattern.
    MatchCollection matches = Regex.Matches(input, pattern, RegexOptions.IgnoreCase);
    
    NumberFormatInfo format = new NumberFormatInfo();
    format.NumberGroupSeparator = " ";
    format.NumberDecimalSeparator = ".";
    format.NumberDecimalDigits = 2;
    
    Dictionary<string, decimal> dictionary = new Dictionary<string, decimal>();
    foreach (Match match in matches)
    {
        dictionary.Add(match.Groups["currency"].Value, Decimal.Parse(match.Groups["decimal"].Value, format));      
    }
    if (dictionary.Count > 0)
    {
        foreach (KeyValuePair<string, decimal> item in dictionary)
        {
            Console.WriteLine("Currency : {0} Amount: {1}", item.Key, item.Value);
        }
    }
    

    您可能需要一个正则表达式来表示thisHenk,在这个问题上,我问的是如何解析值和标记。这里我问的是不同的问题,如何解析特定的值。我认为最好将其分为两个问题,如果您认为最好将其作为一个问题,我将删除该问题并更新第一个问题。在我看来,它们就像一个已经非常本地化的问题中的两个步骤。所以,是的,关闭一个,并尝试具体的格式。现在猜得太多了,太好了。String.LastIndex,这就是我需要的。现在,第二个问题:“302600.00摩擦”。这里text.lastIndexOf不起作用。哎哟,“^(?[0-9]+)(?[0-9]{1,3}([0-9]{3})*\.[0-9]{2})(?[A-Z]{3})$”看起来很恶心!这可能是唯一的方法,但这是正确的代价(没有双关语!)
    string pattern = @"(?<decimal>[0-9]{0,3}(\s[0-9]{3})*(.[0-9]+){0,1})\s" +
                     @"(?<currency>[a-zA-Z]+){1}";
    
    string input = "302 600.00 RUB\r\n10 000.00 USD"; 
    
    // Get text that matches regular expression pattern.
    MatchCollection matches = Regex.Matches(input, pattern, RegexOptions.IgnoreCase);
    
    NumberFormatInfo format = new NumberFormatInfo();
    format.NumberGroupSeparator = " ";
    format.NumberDecimalSeparator = ".";
    format.NumberDecimalDigits = 2;
    
    Dictionary<string, decimal> dictionary = new Dictionary<string, decimal>();
    foreach (Match match in matches)
    {
        dictionary.Add(match.Groups["currency"].Value, Decimal.Parse(match.Groups["decimal"].Value, format));      
    }
    if (dictionary.Count > 0)
    {
        foreach (KeyValuePair<string, decimal> item in dictionary)
        {
            Console.WriteLine("Currency : {0} Amount: {1}", item.Key, item.Value);
        }
    }
    
    Currency : RUB Amount: 302600,00
    Currency : USD Amount: 10000,00