我有一个包含此文本的字符串...
BEGIN Fin Bal -461.000 第 4 天结束
BEGIN Fin Bal 88861.000 第 2 天结束
BEGIN Fin Bal 456461.000 第 1 天结束
BEGIN Fin Bal -44561.000 第 0 天结束
我需要提取价值
-461.000
包括它是否为负。
我一直在用这个...
static string ExtractNumbers(string expr)
{
//removes all text from string
return string.Join(null, System.Text.RegularExpressions
.Regex.Split(expr, "[^\\d]"));
}
Run Code Online (Sandbox Code Playgroud)
问题是这会删除负号并保留 4 与日值。
有没有什么办法可以有效地获取Bal这个词后面的数值?排除所需值后的任何文本?
谢谢,保罗。
对于获取第一个数字的 LINQ 解决方案:
string str = "BEGIN Fin Bal -461.000 Day 4 END";
decimal d;
string n = str.Split(' ').Where(s => decimal.TryParse(s, out d)).FirstOrDefault();
Console.WriteLine(n == null ? "(none)" : decimal.Parse(n).ToString());
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
6518 次 |
| 最近记录: |