How to get the number from a String in C#
To get the number from a string in C#, we can use the Regex.Match() methOd by passing a
\d+
flag to it.
Here is an example, that gets the number 2 from the below string.
using System;
using System.Text.RegularExpressions;
class HelloWorld {
static void Main() {
string name = "Hello 2 World";
string num = Regex.Match(name, @"\d+").Value;
Console.WriteLine(num);
}
}
Output:
2