It is required to format the lines containing telephone numbers so that they can be compared later. For example:

8 (495) 430-23-97
+ 7-4-9-5-43-023-97
4-3-0-2-3-9-7

are the same phone numbers. How to bring them to the form 8 <code> <number>, so that later they can be compared and say whether this number matches the entered one or not? PS +7 = 8. If the code is not specified, then it is equal to 495 by default.

  • 2
    Just do what you just wrote. Or write, what exactly is the problem with the example code - Vitaly

1 answer 1

This function formats everything to the desired view by removing / adding the necessary elements.

static string Remove(string s) { s = s.Replace("-", ""); s = s.Replace("--", ""); s = s.Replace("+7", ""); s = s.Replace("8", ""); s = s.Replace("(", ""); s = s.Replace(")", ""); s = s.Replace(" ", ""); if (s.Length < 7) s = s.Insert(0, "495"); return s; } 
  • s = s.Replace("8", ""); and if you entered a number containing 8 not only in the first place, for example, 8-800-300-83-83 , 8-800-224-22-22 , +7 (495) 640-10-88 ? - Alias
  • Then you just need to check whether 8 is in the first place - Maxim Sorokin
  • Well, supplement your code with this check by editing the answer :) we will see further what it will give you; for now you are simply trying to remove the country code from the first place , which in any case is not a reduction to type 8 <code> <number> - Alias