So How do you format phone numbers and things like that in C#? In perl it would be a pretty simple regular expression to format 4445556666 into (444)-555-6666 but I haven't easily found how to do that with C# in Visual Studio 2005 yet.

Learning is fun. :confused:

Dani AI

Generated

Short summary and practical guidance.

The reason the format-call failed in the original post is that the overload which accepts a format string exists on numeric types, not on System.String. That’s why parsing the DB value to a numeric type made the numeric ToString overload work — as noted — and why ’s idea only succeeds when the value is numeric. ’s Regex suggestion is the right direction when you want to keep the value as a string.

Best practice: treat phone numbers as strings in storage, not numbers. Phone values are identifiers, not quantities — they can include leading zeros, a leading “+”, extensions, or formatting characters. If you need reliable searching, store a canonical digits-only column (index that) and apply formatting only when displaying.

A simple, robust formatter that works from arbitrary input (handles existing punctuation, optional leading country “1”) — strip non-digits, then apply a replacement based on digit count:

using System.Text.RegularExpressions;

string FormatAsUsPhone(string input)
{
    var digits = Regex.Replace(input ?? "", @"\D", "");
    if (digits.Length == 10)
        return Regex.Replace(digits, @"(\d{3})(\d{3})(\d{4})", "($1) $2-$3");
    if (digits.Length == 11 && digits.StartsWith("1"))
        return Regex.Replace(digits, @"1(\d{3})(\d{3})(\d{4})", "+1 ($1) $2-$3");
    return input; // fallback: return original if it doesn't match expected patterns
}

Extra tips: handle null/empty values, preserve extensions (store separately or append), and validate length before formatting. For international numbers or heavy validation/formatting, use a phone-number library (Google’s libphonenumber ports). Finally, if you parse to numeric types, remember 10-digit values can exceed 32-bit int, so use long when necessary.

Recommended Answers

All 7 Replies

Give this a shot:
phone.ToString("(###) ### - ####");

this can be achieved by use of "System.Text.Regularexpression"namespace and by using "Regex" class of this namespace.you can use meyhods of this class to help fulfill tasks :regards

this can be achieved by use of "System.Text.Regularexpression"namespace and by using "Regex" class of this namespace.you can use methods of this class to help fulfill tasks :regards

Give this a shot:
phone.ToString("(###) ### - ####");

I'd tried that and gotten an error:

Error 1 The best overloaded method match for 'string.ToString(System.IFormatProvider)' has some invalid arguments


Error 2 Argument '1': cannot convert from 'string' to 'System.IFormatProvider'

I looked around the Internet and found alot of people discussing similar problems so I cast my 10 digit number I was storing as a string in the database to long and then tried the ToString method and it worked fine. Anyone have any idea what I'm doing wrong for the string? Here is the code. An error is thrown when I try to use number2 with the formatting arguments:

long number1 = long.Parse(thisCustomer["HomePhone"].ToString());
string number2 = String.Copy(thisCustomer["HomePhone"].ToString());
Details += ", " + number2.ToString("(###)-###-####");

Thanks all for your help.

Member Avatar for Member #46692

It's pretty simple...

4445556666

(444)-555-6666

first print a "(" then print the first three characters of your string "444" then print a ")-" then print the next three characters of your string "555"...

What's the problem?

Error 1 The best overloaded method match for 'string.ToString(System.IFormatProvider)' has some invalid arguments

This is because you call ToString on type string instead of long.

Make sure your code looks like this:

long num = 4445556666;
string s = num.ToString("(###)-###-####");

This is because you call ToString on type string instead of long.

Make sure your code looks like this:

long num = 4445556666;
string s = num.ToString("(###)-###-####");

Hi and thanks for your reply! I've solved my problem.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.