简体   繁体   中英

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:

string result = "";

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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