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

后端 未结 14 773
一整个雨季
一整个雨季 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:14

    You can use a regular expression to remove all non-digit characters:

    string phoneNumber = "(123) 456-7890";
    phoneNumber = Regex.Replace(phoneNumber, @"[^\d]", "");
    

    Then further on - depending on your requirements - you can either store the number as a string or as an integer. To convert the number to an integer type you will have the following options:

    // throws if phoneNumber is null or cannot be parsed
    long number = Int64.Parse(phoneNumber, NumberStyles.Integer, CultureInfo.InvariantCulture);
    
    // same as Int64.Parse, but returns 0 if phoneNumber is null
    number = Convert.ToInt64(phoneNumber);
    
    // does not throw, but returns true on success
    if (Int64.TryParse(phoneNumber, NumberStyles.Integer, 
           CultureInfo.InvariantCulture, out number))
    {
        // parse was successful
    }
    
    0 讨论(0)
  • 2021-02-05 01:14

    Since nobody did a for loop.

    long GetPhoneNumber(string PhoneNumberText)
    {
        // Returns 0 on error
    
        StringBuilder TempPhoneNumber = new StringBuilder(PhoneNumberText.Length);
        for (int i=0;i<PhoneNumberText.Length;i++)
        {
            if (!char.IsDigit(PhoneNumberText[i]))
                continue;
    
            TempPhoneNumber.Append(PhoneNumberText[i]);
        }
    
        PhoneNumberText = TempPhoneNumber.ToString();
        if (PhoneNumberText.Length == 0)
            return 0;// No point trying to parse nothing
    
        long PhoneNumber = 0;
        if(!long.TryParse(PhoneNumberText,out PhoneNumber))
            return 0; // Failed to parse string
    
        return PhoneNumber;
    }
    

    used like this:

    long phoneNumber = GetPhoneNumber("(123) 456-7890"); 
    


    Update
    As pr commented many countries do have zero's in the begining of the number, if you need to support that, then you have to return a string not a long. To change my code to do that do the following:

    1) Change function return type from long to string.
    2) Make the function return null instead of 0 on error
    3) On successfull parse make it return PhoneNumberText

    0 讨论(0)
  • 2021-02-05 01:14
    'you can use module / inside sub main form VB.net
    Public Function ClearFormat(ByVal Strinput As String) As String
        Dim hasil As String
        Dim Hrf As Char
        For i = 0 To Strinput.Length - 1
            Hrf = Strinput.Substring(i, 1)
            If IsNumeric(Hrf) Then
                hasil &= Hrf
            End If
        Next
        Return Strinput
    End Function
    'you can call this function like this
    ' Phone= ClearFormat(Phone)
    
    0 讨论(0)
  • 2021-02-05 01:18
    string digits = Regex.Replace(formatted, @"\D", String.Empty, RegexOptions.Compiled);
    
    0 讨论(0)
  • 2021-02-05 01:20

    As many answers already mention, you need to strip out the non-digit characters first before trying to parse the number. You can do this using a regular expression.

    Regex.Replace("(123) 456-7890", @"\D", String.Empty) // "1234567890"
    

    However, note that the largest positive value int can hold is 2,147,483,647 so any number with an area code greater than 214 would cause an overflow. You're better off using long in this situation.

    Leading zeros won't be a problem for North American numbers, as area codes cannot start with a zero or a one.

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

    This is basically a special case of C#: Removing common invalid characters from a string: improve this algorithm. Where your formatng incl. White space are treated as "bad characters"

    0 讨论(0)
提交回复
热议问题