using System;
using System.Text.RegularExpressions;
class PhoneNumberFormatter
{
public static string FormatUniversalPhoneNumber(string phoneNumber)
{
// Remove non-numeric characters from the input
string cleanedNumber = Regex.Replace(phoneNumber, "[^0-9]", "");
// Check if the number has a country code and format accordingly
if (cleanedNumber.Length > 10)
{
return $"+{cleanedNumber.Substring(0, cleanedNumber.Length - 10)} " +
$"{cleanedNumber.Substring(cleanedNumber.Length - 10, 3)}-" +
$"{cleanedNumber.Substring(cleanedNumber.Length - 7, 3)}-" +
$"{cleanedNumber.Substring(cleanedNumber.Length - 4, 4)}";
}
else if (cleanedNumber.Length == 10)
{
return $"{cleanedNumber.Substring(0, 3)}-{cleanedNumber.Substring(3, 3)}-" +
$"{cleanedNumber.Substring(6, 4)}";
}
else
{
// If the number doesn't follow the standard length, return the cleaned number as is
return cleanedNumber;
}
}
static void Main()
{
string phoneNumber = "+1 (555) 123-4567";
string formattedNumber = FormatUniversalPhoneNumber(phoneNumber);
Console.WriteLine($"Formatted number: {formattedNumber}");
}
}
using System;
using System.Text.RegularExpressions;
class PhoneNumberFormatter
{
public static string FormatUniversalPhoneNumber(string phoneNumber)
{
// Remove non-numeric characters from the input
string cleanedNumber = Regex.Replace(phoneNumber, "[^0-9]", "");
// Check if the number has a country code and format accordingly
if (cleanedNumber.Length > 10)
{
return $"+{cleanedNumber.Substring(0, cleanedNumber.Length - 10)} " +
$"{cleanedNumber.Substring(cleanedNumber.Length - 10, 3)}-" +
$"{cleanedNumber.Substring(cleanedNumber.Length - 7, 3)}-" +
$"{cleanedNumber.Substring(cleanedNumber.Length - 4, 4)}";
}
else if (cleanedNumber.Length == 10)
{
return $"{cleanedNumber.Substring(0, 3)}-{cleanedNumber.Substring(3, 3)}-" +
$"{cleanedNumber.Substring(6, 4)}";
}
else
{
// If the number doesn't follow the standard length, return the cleaned number as is
return cleanedNumber;
}
}
static void Main()
{
string phoneNumber = "+1 (555) 123-4567";
string formattedNumber = FormatUniversalPhoneNumber(phoneNumber);
Console.WriteLine($"Formatted number: {formattedNumber}");
}
}