by @saigowthamr

How to get the number from a String in C#

Home » c-sharp » How to get the number from a String in C#

To get the number from a string we can use the regex with a \d+ flag in C#.

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