using System.Text.RegularExpressions;
public static void Main()
string[] numbers = new []{"0654767491", "+31654767491","074 245 5290", "0031654767491", "(074) 245 5290"};
foreach (var number in numbers){
Console.WriteLine("Number:" + number);
Console.WriteLine("\told: " + OldMethod(number));
Console.WriteLine("\tnew: " + NewMethod(number));
public static long NewMethod (string phoneNumber)
.RegexReplace("[^\\d]", "")
if (!number.StartsWith("31"))
number = string.Format("31{0}", number);
return number.TryParseLong(0);
public static long OldMethod (string phoneNumber)
var number = phoneNumber.Replace(" ", "").Replace("+", "").Replace("-", "").Trim();
if (number.StartsWith("0"))
number = number.Substring(1, number.Length - 1);
if (!number.StartsWith("31"))
number = string.Format("31{0}", number);
return number.TryParseLong(0);
public static class ext {
public static long TryParseLong(this string source, long defaultValue)
return source.TryParseLong() ?? defaultValue;
public static long? TryParseLong(this string source)
return long.TryParse(source, out result)
public static string Replace(this string inputString, string oldValue, string newValue, StringComparison comparison)
var stringBuilder = new StringBuilder();
index = inputString.IndexOf(oldValue, comparison);
.Append(inputString.Substring(previousIndex, index - previousIndex))
previousIndex = (index += oldValue.Length);
index = inputString.IndexOf(oldValue, index, comparison);
stringBuilder.Append(inputString.Substring(previousIndex));
return stringBuilder.ToString();
public static string RegexReplace(this string source, string regexPattern, string replace)
return RegexReplace(source, new Regex(regexPattern), c => replace);
public static string RegexReplace(this string source, Regex regex, string replace)
return regex.Replace(source, replace);
public static string RegexReplace(this string source, string regexPattern, MatchEvaluator matchEvaluator)
return RegexReplace(source, new Regex(regexPattern), matchEvaluator);
public static string RegexReplace(this string source, Regex regex, MatchEvaluator matchEvaluator)
return regex.Replace(source, matchEvaluator);