How to extract the last number in each string from an array of strings in C#

1 Answer

0 votes
using System;
using System.Text.RegularExpressions;
 
class Program
{
    static void Main() {
        string[] array = new[] {"15 c# 1", "18 vb 5", "900 c 10", "1380 c++ 129"};
         
        foreach (var str in array) {
            string[] numbers = Regex.Split(str, @"\D+");
            Console.WriteLine("{0}", numbers[numbers.Length - 1]);
       }
    }
}
 
 
 
 
/*
run:
 
1
5
10
129
 
*/

 

 



answered Aug 31, 2023 by avibootz
edited Aug 31, 2023 by avibootz
...