How to extract the first number from string in C#

1 Answer

0 votes
using System;
using System.Text.RegularExpressions;

public class Program {
    public static void Main() {
        string s = "abc 9836 xy%^(s 21 * 9 ppp 100";
        
        var number = Regex.Match(s, @"\d+").Value;
        
        Console.WriteLine(number);
    }
}



/*
run:

9836

*/

 



answered Dec 16, 2020 by avibootz
...