7

我正在尝试从具有大量格式的字符串中提取不同长度的整数。有问题的字符串可能如下所示:

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

我正在寻找的输出是一个数组:

{221122321,434334,3434,34343,343421,343,343}
4

2 回答 2

25
var result = new Regex(@"\d+").Matches(s)
                              .Cast<Match>()
                              .Select(m => Int32.Parse(m.Value))
                              .ToArray();
于 2013-11-04T13:23:57.363 回答
-1

使用这样的 foreach 循环:

string result = "";

foreach (string str in s)
{
    int number;
    if (int.TryParse(str, out number))
       result += s;
    else
       result += ",";
}
于 2013-11-04T13:25:15.700 回答