comme la question le dit. J'obtiens des nombres comme 2125550938 ou 20298277625552. Ceux-ci devraient changer en (212) 555-0938 et (202) 982-7762 x 5552 respectivement. c'est vb.netformater une chaîne au numéro de téléphone usa dans vb.net
Répondre
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
Je voudrais changer la longueur = 14, à la longueur> 10 – maxwellb
Je serais probablement aller avec une mise en œuvre d'expressions régulières, quelque chose comme ceci:
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()
Cela permettrait à des numéros de téléphone sans extension ou avec une extension de longueur variable.
Fonction publique partagée 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
Effectuez les opérations suivantes lors de l'utilisation String.Format en VB.NET:
String.Format("{0:(###) ###-####}", Long.Parse(PhoneString))
ici est une combinaison de formatage Regex et chaîne qui fonctionne assez bien pour moi. Il suffit d'ajouter du sel à une recette et il sera délicieux!
''' <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
- 1. Comment formater une chaîne de 10 chiffres dans un numéro de téléphone?
- 2. format int au numéro de téléphone
- 3. Formatage du numéro de téléphone
- 4. Expression régulière correspondant au numéro de téléphone?
- 5. Puis-je formater une chaîne comme un nombre dans .NET?
- 6. Le moyen le plus rapide de formater un numéro de téléphone en C#?
- 7. Comment formater une date dans vb.net
- 8. Parse Numéro de téléphone en composants
- 9. Convertir NSString avec un nombre au format de localisation de numéro de téléphone approprié
- 10. Comment capturer un numéro de téléphone
- 11. téléphone java numéro validation
- 12. Android envoi de données téléphone au téléphone
- 13. numéro de téléphone android Convertir au format international
- 14. Format numéro de téléphone dans datagridview
- 15. Valider un numéro de téléphone au Royaume-Uni
- 16. Comment formater conditionnellement une chaîne dans .Net?
- 17. Numéro Formatage dans Excel Vb.Net
- 18. supprimer le numéro de téléphone d'un document
- 19. Regex - numéro de téléphone simple
- 20. reg ex numéro de téléphone
- 21. preg_replace aide: numéro de téléphone
- 22. Validation du numéro de téléphone
- 23. ASP.NET GridView colonne - formatage numéro de téléphone
- 24. NSNumberFormatter pour formater les numéros de téléphone américains
- 25. Numéro de démineur VB.NET
- 26. Comment formater une chaîne pour faire partie de l'URL?
- 27. Fractionnement un numéro de téléphone en php
- 28. importer des numéros de téléphone à partir d'une chaîne dans vb.net
- 29. Valide comme un numéro de téléphone
- 30. un téléphone spécifique Numéro regex
double possible [Comment formater une chaîne comme un numéro de téléphone en C#] (http://stackoverflow.com/questions/188510/how-to-format-a-string-as-a-telephone -number-in-c) mais dans VB.NET au lieu de C#. –
vb.net =/= C#, imho – maxwellb