code_junkie

Converting a normalized phone number to a user-friendly version

Nov 13th, 2011
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.15 KB | None | 0 0
  1. String.Format("{0:(###) ###-#### x ###}", double.Parse("1234567890123"))
  2.  
  3. (d{3})(d{3})(d{4})(d{4})
  4.  
  5. (1) 2-3 x4
  6.  
  7. class Program
  8. {
  9. static void Main(string[] args)
  10. {
  11. long phoneNumber = 12345678901234;
  12. string phoneNumberString = String.Format("{0:(000) 000-0000 x0000}", phoneNumber);
  13. Console.WriteLine(phoneNumberString);
  14. }
  15. }
  16.  
  17. public static string InsertStringAtPositions(this string str, string insertStr, IEnumerable<int> positions)
  18. {
  19. if (str != null && insertStr != null && positions != null)
  20. {
  21. string newString = string.Empty;
  22. int previousPos = 0;
  23. foreach (var pos in positions)
  24. {
  25. if (pos < str.Length)
  26. {
  27. newString += str.Substring(previousPos, pos - previousPos) + insertStr;
  28. previousPos = pos;
  29. }
  30. }
  31. if (positions.Last() < str.Length)
  32. {
  33. return newString + str.Substring(positions.Last(), str.Length - positions.Last());
  34. }
  35. return newString;
  36. }
  37. return str;
  38. }
  39.  
  40. // Will convert "0399998888" to "03 9999 8888"
  41. number.InsertStringAtPositions(" ", new[] {2, 6});
Add Comment
Please, Sign In to add comment