Skip to content

Instantly share code, notes, and snippets.

@eugen1408
Created June 17, 2020 07:52
Show Gist options
  • Save eugen1408/4213472ab572e8eab66e38c1a5e92a48 to your computer and use it in GitHub Desktop.
Save eugen1408/4213472ab572e8eab66e38c1a5e92a48 to your computer and use it in GitHub Desktop.
private static string FormatPhone(string phoneString)
{
if (string.IsNullOrWhiteSpace(phoneString)) return phoneString;
var phoneReplaced = phoneString.Replace(" ", "").Replace("-", "").Replace(")", "").Replace("(", "").Replace("+", "");
if (phoneReplaced.Length == 10 && (phoneReplaced[0] == '9')) return "+7" + phoneReplaced;
else if ((phoneReplaced.Length == 11 && (phoneReplaced[0] == '8'))) return "+7" + phoneReplaced.Substring(1);
else return phoneReplaced.StartsWith("7") ? $"+{phoneReplaced}" : phoneReplaced;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment