What's Here?
- Members: 244,263
- Replies: 693,079
- Topics: 113,149
- Snippets: 3,863
- Tutorials: 935
- Total Online: 1,252
- Members: 70
- Guests: 1,182
|
This is a snippet I use when needing to format a US phone number into the format (555) 555-1212
|
Submitted By: PsychoCoder
|
|
Rating:
 
|
|
Views: 7,104 |
Language: C#
|
|
Last Modified: April 16, 2008 |
|
Instructions: Add a reference to System.Text.RegularExpressions, then pass the method the string you want formatted |
Snippet
//Namespace Reference
using System.Text.RegularExpressions
/// <summary>
/// method to format a US phone number
/// </summary>
/// <param name="num">phone number to format</param>
/// <returns></returns>
public static string FormatUSPhone(string num)
{
//first we must remove all non numeric characters
num = num.Replace("(", "").Replace(")","").Replace("-","");
string results = string.Empty;
string formatPattern = @"(\d{3})(\d{3})(\d{4})";
results = Regex.Replace(num, formatPattern, "($1) $2-$3");
//now return the formatted phone number
return results;
}
Copy & Paste
|
|
|
Be Social
Reference Sheets
Bye Bye Ads
Monthly Drawing
Top Contributors
Top 10 Kudos This Month
|