Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/283.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/37.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中查找并提取一个数字_C#_Asp.net_Asp.net Mvc - Fatal编程技术网

C# 从字符串C中查找并提取一个数字

C# 从字符串C中查找并提取一个数字,c#,asp.net,asp.net-mvc,C#,Asp.net,Asp.net Mvc,我需要查找并提取字符串中包含的数字 例如,从这些字符串: O:2275000 BF:3060000 D:326000 E:3472000 I:3918000 T:4247000 UF:4777000 A:4904000 AD:5010000 X:5243000 G:21280000 摘录: 1.2275000 2.3060000 3.3260000 ..我们可以在此处尝试使用字符串拆分: string input = "O:2275000 BF:3060000 D:3260000"; strin

我需要查找并提取字符串中包含的数字

例如,从这些字符串:

O:2275000 BF:3060000 D:326000 E:3472000 I:3918000 T:4247000 UF:4777000 A:4904000 AD:5010000 X:5243000 G:21280000

摘录:

1.2275000 2.3060000 3.3260000
..

我们可以在此处尝试使用字符串拆分:

string input = "O:2275000 BF:3060000 D:3260000";
string[] parts = input.Split(' ');
string[] numbers = parts
    .Select(s => s.Split(':')[1])
    .ToArray();

foreach (string n in numbers)
{
    Console.WriteLine(n);
}
这张照片是:

2275000
3060000
3260000

Tim Biegeleisen的答案是正确的,只是它不会产生您在问题中提到的浮点输出。在他的Ansare中,只需将foreach循环替换为for语句,如下所示:

string input = "O:2275000 BF:3060000 D:3260000";
string[] parts = input.Split(' ');
string[] numbers = parts
    .Select(s => s.Split(':')[1])
    .ToArray();

for(int i = 0; i < numbers.Length; i++)
{
    Console.WriteLine("{0}.{1}", i+1, numbers[i]);
}

首先,你提到了这些字符串,尽管你只给出了一个字符串。我不清楚这部分

第二,你所说的摘录是什么意思?是否要查找数字在字符串中的位置?如果是,那么您可以简单地使用字符串搜索,如下所示

            string str = "O:2275000 BF:3060000 D:3260000";
            int index = str.IndexOf("3060000");
            if (index != -1)
            {
                Console.WriteLine(index);
            }
            else
            {
                Console.WriteLine("Not Found");
            }
或者,如果问题是这样表述的:你被赋予一个字符串,你想从中提取数字,那么你可以这样做:

        List<decimal> findNumbers(string str)
        {
            List<decimal> x = new List<decimal>();
            string tokens = "";
            foreach (char ch in str)
            {
                if (Char.IsNumber(ch))
                {
                    tokens = tokens + ch;
                }
                if (!Char.IsNumber(ch) && !String.IsNullOrEmpty(tokens))
                {
                    decimal num = Convert.ToDecimal(tokens);                    
                    x.Add(Convert.ToDecimal(num));
                    tokens = "";
                }
            }
            if (String.IsNullOrEmpty(tokens))
            {
                x.Add(Convert.ToDecimal(tokens));
            }
            return x;
        }
此函数返回字符串中可用的数字列表。

它将是:

string temp = yourText;
List<int> numbers = new List<int>();
Regex re = new Regex(@"\d+");
Match m = re.Match(temp);
while (m.Success)
{
    numbers.Add(Convert.ToInt32(m.Value));
    temp = temp.Substring(m.Index + m.Length);
    m = re.Match(temp);
}

您可以使用Linq在一行中完成此操作

string numbers = "O:2275000 BF:3060000 D:3260000 E:3472000 I:3918000 T:4247000 UF:4777000 A:4904000 AD:5010000 X:5243000 G:21280000";

List<int> list = numbers.Split(' ').Select(x => Convert.ToInt32(string.Concat(x.Where(Char.IsDigit)))).ToList();

欢迎来到SO。你能告诉我们到目前为止你都做了些什么吗?如果您能在继续之前阅读本网站关于提问的帮助,我将不胜感激。我相信作者指的是您的第二个案例,Shakibuz。此外,您可以尝试使用正则表达式字符串拆分来解决此问题。这是不对的。OP没有浮点输出,这些前导数字只是一个计数,而不是数字的一部分。我可能会使正则表达式模式比\d+更严格一些,以避免误报。@TimBiegeleisen你是对的,它可能是:regex^[0-9]+$