我正在尝试从具有大量格式的字符串中提取不同长度的整数。有问题的字符串可能如下所示:
string s = "Hallo (221122321 434334 more text3434 even mor,34343 343421.343sf 343";
我正在寻找的输出是一个数组:
{221122321,434334,3434,34343,343421,343,343}
var result = new Regex(@"\d+").Matches(s)
.Cast<Match>()
.Select(m => Int32.Parse(m.Value))
.ToArray();
使用这样的 foreach 循环:
string result = "";
foreach (string str in s)
{
int number;
if (int.TryParse(str, out number))
result += s;
else
result += ",";
}