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:

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 iamthwee

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.