Remove formatting from a string: “(123) 456-7890” => “1234567890”?

后端 未结 14 724
一整个雨季
一整个雨季 2021-02-05 01:12

I have a string when a telephone number is inputted - there is a mask so it always looks like \"(123) 456-7890\" - I\'d like to take the formatting out before saving it to the D

相关标签:
14条回答
  • 2021-02-05 01:25

    You could use a regular expression or you could loop over each character and use char.IsNumber function.

    0 讨论(0)
  • 2021-02-05 01:28

    You can make it work for that number with the addition of a simple regex replacement, but I'd look out for higher initial digits. For example, (876) 543-2019 will overflow an integer variable.

    0 讨论(0)
  • 2021-02-05 01:28

    Try this:

    string s = "(123) 456-7890";
    UInt64 i = UInt64.Parse(
        s.Replace("(","")
         .Replace(")","")
         .Replace(" ","")
         .Replace("-",""));
    

    You should be safe with this since the input is masked.

    0 讨论(0)
  • 2021-02-05 01:28

    You would be better off using regular expressions. An int by definition is just a number, but you desire the formatting characters to make it a phone number, which is a string.

    There are numerous posts about phone number validation, see A comprehensive regex for phone number validation for starters.

    0 讨论(0)
  • 2021-02-05 01:31

    Alternative using Linq:

    string phoneNumber = "(403) 259-7898";
    var phoneStr = new string(phoneNumber.Where(i=> i >= 48 && i <= 57).ToArray());
    
    0 讨论(0)
  • 2021-02-05 01:32

    Aside from all of the other correct answers, storing phone numbers as integers or otherwise stripping out formatting might be a bad idea.

    Here are a couple considerations:

    • Users may provide international phone numbers that don't fit your expectations. See these examples So the usual groupings for standard US numbers wouldn't fit.
    • Users may NEED to provide an extension, eg (555) 555-5555 ext#343 The # key is actually on the dialer/phone, but can't be encoded in an integer. Users may also need to supply the * key.
    • Some devices allow you to insert pauses (usually with the character P), which may be necessary for extensions or menu systems, or dialing into certain phone systems (eg, overseas). These also can't be encoded as integers.

    [EDIT]

    It might be a good idea to store both an integer version and a string version in the database. Also, when storing strings, you could reduce all punctuation to whitespace using one of the methods noted above. A regular expression for this might be:

    // (222) 222-2222 ext# 333   ->   222 222 2222 # 333
    phoneString = Regex.Replace(phoneString, @"[^\d#*P]", " ");
    
    // (222) 222-2222 ext# 333   ->   2222222222333 (information lost)
    phoneNumber = Regex.Replace(phoneString, @"[^\d]", "");
    
    // you could try to avoid losing "ext" strings as in (222) 222-2222 ext.333 thus:
    phoneString = Regex.Replace(phoneString, @"ex\w+", "#");
    phoneString = Regex.Replace(phoneString, @"[^\d#*P]", " ");
    
    0 讨论(0)
提交回复
热议问题