Faster way to get number from string : Number : Data Types C# Source Code


Custom Search

C# Source Code » Data Types » Number »

 

Faster way to get number from string









    


using System;
using System.Data;
using System.Text.RegularExpressions;

class Class1{
        static void Main(string[] args){
      string IsNotNum = "111west";
      string IsNum = "  +111  ";
      string IsFloat = "  23.11  ";
      string IsExp = "  +23 e+11  ";
      
      Console.WriteLine(GetNumberFromStrFaster(IsNum));    // +111
      Console.WriteLine(GetNumberFromStrFaster(IsNotNum));  // 
      Console.WriteLine(GetNumberFromStrFaster(IsFloat));    // 23.11
      Console.WriteLine(GetNumberFromStrFaster(IsExp));    // 
        }
    public static string GetNumberFromStrFaster(string str)
    {
      str = str.Trim();
      Match m = new Regex(@"^[\+\-]?\d*\.?[Ee]?[\+\-]?\d*$", RegexOptions.Compiled).Match(str);
      return (m.Value);
    }

}

           
       
    
   
  
   







HTML code for linking to this page:

Follow Navioo On Twitter

C# Source Code

 Navioo Data Types
» Number