Search code examples
c#.netparsingintcustom-formatting

parsing a string into int/long using custom format strings


In C#.Net, here's a simple example of how to format numbers into strings using custom format strings: (example taken from: http://www.csharp-examples.net/string-format-int/)

String.Format("{0:+### ### ### ###}", 447900123456); // "+447 900 123 456"
String.Format("{0:##-####-####}", 8958712551);       // "89-5871-2551"

Is there a way to convert this formatted string back into a long/integer ? Is there someway to do this :

long PhoneNumber = Int32.Parse("89-5871-2551", "{0:##-####-####}");

I saw that DateTime has a method ParseExact which can do this work well. But I did not see any such thing for int/long/decimal/double.


Solution

  • Just Regex out all of the non-numeric characters, then parse that string.