string 在 vb.net 中将字符串格式化为美国电话号码
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3088318/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
format a string to usa phone number in vb.net
提问by lkewo
just as the question says. I get numbers like 2125550938 or 20298277625552. these should change to (212) 555-0938 and (202) 982-7762 x 5552 respectively. this is in vb.net
正如问题所说。我得到像 2125550938 或 20298277625552 这样的数字。这些应该分别更改为 (212) 555-0938 和 (202) 982-7762 x 5552。这是在 vb.net
回答by atconway
Try the following when using String.Format in VB.NET:
在 VB.NET 中使用 String.Format 时尝试以下操作:
String.Format("{0:(###) ###-####}", Long.Parse(PhoneString))
回答by 2GDave
Public Shared Function PhoneFormat(ByVal strPhoneNumber As String) As String
公共共享函数 PhoneFormat(ByVal strPhoneNumber As String) As String
' Remove any style characters from the user input
strPhoneNumber = Replace(strPhoneNumber, ")", "")
strPhoneNumber = Replace(strPhoneNumber, "(", "")
strPhoneNumber = Replace(strPhoneNumber, "-", "")
strPhoneNumber = Replace(strPhoneNumber, ".", "")
strPhoneNumber = Replace(strPhoneNumber, Space(1), "")
Dim strFormatedNumber As String = CLng(strPhoneNumber).ToString("(###) ###-####")
Return strFormatedNumber
End Function
结束函数
回答by knslyr
I would probably go with an implementation of regular expressions, something like this:
我可能会使用正则表达式的实现,如下所示:
Dim phoneNumbers() As String = {"2125550938", _
"20298277625552", _
"2025551212378", _
"202555131345943"}
Dim ext As String = ""
Dim r As New Regex("^(?<AC>\d{3})(?<First>\d{3})(?<Last>\d{4})(?<Ext>\d*$)")
Dim m As Match
For i As Int32 = 0 To (phoneNumbers.Length - 1)
m = r.Match(phoneNumbers(i))
If m.Groups("Ext").Length > 0 Then
ext = " x " & CStr(m.Groups("Ext").Value)
Else
ext = ""
End If
Console.WriteLine("({0}) {1}-{2}{3}", _
CStr(m.Groups("AC").Value), _
CStr(m.Groups("First").Value), _
CStr(m.Groups("Last").Value), ext)
Next
Console.Read()
This would allow for phone numbers without extensions or with a variable length extension.
这将允许没有扩展名或具有可变长度扩展名的电话号码。
回答by klr650will
Here is a combination of Regex and string formatting that works fairly well for me. Just add salt to a recipe and it will taste GREAT!
这是正则表达式和字符串格式的组合,对我来说效果很好。只需在食谱中加盐,味道就会很棒!
''' <summary>
''' returned formats are:
''' example1 (620) 123-4567 Ext: 890
''' example2 123-4567 Ext: 890
''' example3 (620) 123-4567
''' example4 123-4567
''' The user can input a 7 or 10 digit number followed by a character(s) and digits for the extension
'''
''' </summary>
''' <param name="OriginalNumber"></param>
''' <returns></returns>
''' <remarks></remarks>
Public Function FormatPhone(ByVal OriginalNumber As String) As String
Dim sReturn As String
Dim tester() As String
Dim R As Regex
Dim M As Match
Dim sTemp As String
sReturn = ""
' removes anything that is not a digit or letter
sTemp = UnFormatPhone(OriginalNumber)
' splits sTemp based on user input of character(s) to signify an extension i.e. x or ext or anything else you can think of (abcdefg...)
tester() = Regex.Split(sTemp, "\D+")
' if the string was split then replace sTemp with the first part, i.e. the phone number less the extension
If tester.Count > 1 Then
sTemp = tester(0)
End If
' Based on the NANP (North American Numbering Plan), we better have a 7 or 10 digit number. anything else will not parse
If sTemp.Length = 7 Then
R = New Regex("^(?<First>\d{3})(?<Last>\d{4})")
ElseIf sTemp.Length = 10 Then
R = New Regex("^(?<AC>\d{3})(?<First>\d{3})(?<Last>\d{4})")
Else
Return OriginalNumber
End If
' now format the phone number nice and purtee...
M = R.Match(sTemp)
If m.Groups("AC").Length > 0 Then
sReturn &= String.Format("({0}) {1}-{2}", CStr(m.Groups("AC").Value), CStr(m.Groups("First").Value), CStr(m.Groups("Last").Value))
Else
sReturn &= String.Format("{0}-{1}", CStr(m.Groups("First").Value), CStr(m.Groups("Last").Value))
End If
If tester.Count > 1 Then
sReturn &= " Ext: " + tester(1)
End If
Return sReturn
End Function
''' <summary>
''' Strips NON ALPHANUMERICS from a string
'''
''' </summary>
''' <param name="sTemp"></param>
''' <returns></returns>
''' <remarks></remarks>
Public Function UnFormatPhone(ByVal sTemp As String) As String
sTemp = ClearNull(sTemp)
Dim sb As New System.Text.StringBuilder
For Each ch As Char In sTemp
If Char.IsLetterOrDigit(ch) OrElse ch = " "c Then
sb.Append(ch)
End If
Next
UnFormatPhone = sb.ToString
End Function
''' <summary>
''' Returns a trimmed string with vbNullChar replaced by a blank
''' </summary>
''' <param name="sTemp"></param>
''' <returns></returns>
''' <remarks></remarks>
Public Function ClearNull(ByRef sTemp As String) As String
sTemp = Replace(sTemp, vbNullChar, "")
ClearNull = Trim(sTemp)
End Function
回答by Kyra
Dim newNumber As New String
If number.Length = 10 Then
newNumber = "(" & number.Substring(0, 3) & ") " & number.Substring(2, 3) & "-" & number.Substring(5, 4)
ElseIf number.Length = 14 Then
newNumber = "(" & number.Substring(0, 3) & ") " & number.Substring(2, 3) & "-" & number.Substring(5, 4) & " x " & number.Substring(9)
End if