[.NET] - How to format a phone number to '90 xxx xxx xx xx' | SheCodes

[.NET] - How to format a phone number to '90 xxx xxx xx xx' in .NET 6

Learn how to format any phone number to '90 xxx xxx xx xx' format using C# in .NET 6.

👩‍💻 .NET Code instructions

Can you generate me a method for formatting any given set of phone numbers to "90 xxx xxx xx xx" format? I am using .net 6

🤖 SheCodes Athena says:

Answered in 4.38 seconds

C# code:

csharp
Copy code
public string FormatPhoneNumber(string phoneNumber) { // Remove all non-numeric characters from the phone number var cleanedPhoneNumber = new string(phoneNumber.Where(char.IsDigit).ToArray()); // If the cleaned phone number has less than 10 digits, return it as is if (cleanedPhoneNumber.Length < 10) return cleanedPhoneNumber; // If the cleaned phone number has more than 10 digits, keep only the last 10 digits if (cleanedPhoneNumber.Length > 10) cleanedPhoneNumber = cleanedPhoneNumber.Substring(cleanedPhoneNumber.Length - 10); // Format the phone number as "90 xxx xxx xx xx" return string.Format("90 {0} {1} {2} {3}", cleanedPhoneNumber.Substring(0, 3), cleanedPhoneNumber.Substring(3, 3), cleanedPhoneNumber.Substring(6, 2), cleanedPhoneNumber.Substring(8, 2)); }

This method takes a phoneNumber string as input, removes all non-numeric characters from it, and then formats it as "90 xxx xxx xx xx". If the phone number has less than 10 digits, it is returned as is. If it has more than 10 digits, only the last 10 digits are kept for formatting.

👩‍💻 This is not what I want. where x is a digit. and 90 is the constant and phone should be start like that. In the simplest manner if I give 05554445544 or 5554445544 formatted numbers it will convert them to 905554445544 format. And I will provide a string list of phone numbers and I want all of them to formatted that way.

🤖 SheCodes Athena says:

Answered in 4.85 seconds

Here is the updated C# code to format a list of phone numbers:

csharp
Copy code
public List<string> FormatPhoneNumbers(List<string> phoneNumbers) { var formattedPhoneNumbers = new List<string>(); foreach (var phoneNumber in phoneNumbers) { // Remove all non-numeric characters from the phone number var cleanedPhoneNumber = new string(phoneNumber.Where(char.IsDigit).ToArray()); // If the cleaned phone number has less than 10 digits, skip to the next number if (cleanedPhoneNumber.Length < 10) continue; // If the cleaned phone number has more than 10 digits, keep only the last 10 digits if (cleanedPhoneNumber.Length > 10) cleanedPhoneNumber = cleanedPhoneNumber.Substring(cleanedPhoneNumber.Length - 10); // Format the phone number as "90 xxx xxx xx xx" and add it to the list of formatted phone numbers formattedPhoneNumbers.Add(string.Format("90{0}", cleanedPhoneNumber)); } return formattedPhoneNumbers; }

In this code, the FormatPhoneNumbers method takes a list of phone numbers as input and returns a list of formatted phone numbers. For each phone number in the input list, it removes all non-numeric characters, keeps only the last 10 digits (if there are more than 10), and formats the number as "90 xxx xxx xx xx". If a phone number has less than 10 digits, it is skipped. The formatted phone numbers are added to a new list, which is returned when all phone numbers have been processed.