简体   繁体   English

c#从字符串中提取多个数字

[英]c# Extract multiple numbers from a string

I'm trying to extract whole numbers of different length from a string with lots of formatting. 我试图从具有大量格式的字符串中提取不同长度的整数。 The string in question could look like this: 有问题的字符串可能如下所示:

string s = "Hallo (221122321 434334 more text3434 even mor,34343 343421.343sf 343";

The output I'm looking for is an array of: 我正在寻找的输出是一个数组:

{221122321,434334,3434,34343,343421,343,343}
var result = new Regex(@"\d+").Matches(s)
                              .Cast<Match>()
                              .Select(m => Int32.Parse(m.Value))
                              .ToArray();

Use a foreach loop like this: 使用这样的foreach循环:

string result = "";

foreach (string str in s)
{
    int number;
    if (int.TryParse(str, out number))
       result += s;
    else
       result += ",";
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM