Phone number string manipulation

chicago75

Newcomer
Joined
Nov 27, 2002
Messages
8
Help..
I am using the String.format() method and getting the following error...

An unhandled exception of type 'System.FormatException' occurred in mscorlib.dll
Additional information: Input string was not in a correct format.

Here is the line of the code causing it...

strResult = String.Format("{^\d-\d{3}-\d{3}-\d{4}}", hpformato)

The "hpformato" is Dimmed as String. I don't know why this is not working..Help
Chicago75@evildata.com
 
String.Format doesn't work with regular expressions. It has it's own set of rules (ie; # for an integer). Also you need to specify the location of the value you are formatting... example;

Visual Basic:
Dim phoneNum As Integer = 2092524321
'Note that Console.WriteLine follows the same format rules as String.Format
Console.WriteLine("Phone number is {0:(###) ###-####}", phoneNum)

To plop an object inside a sentence you use {n} (n stands for the nth place). If you want to format it you place the format rules after a colon :.

Does that makes sense? In respect it's similar to using..

Console.WriteLine("Some {0} to {1}", wordZero, wordOne)

except you're adding formatting. Does this make sense?
 
Thanks.... However I am still getting the same error.....


I have attached the updated code to help make sense of this.... Please let me know what I am doing wrong...


Public Function PhoneFormat(ByVal hpformat As String) As String
Dim strResult As String
Dim iLength As Integer
Dim strOriginal As String
Dim i As Integer

strOriginal = hpformat
hpformat = Replace(hpformat, ")", "")
hpformat = Replace(hpformat, "(", "")
hpformat = Replace(hpformat, "-", "")
hpformat = Replace(hpformat, ".", "")
hpformat = Replace(hpformat, Space(1), "")
iLength = Len(hpformat)
For i = 1 To iLength
Next i
Select Case iLength
Case Is = 10
hpformat = strResult
Case Is = 7
strResult = String.Format("{###-####}", Decimal.Parse(hpformat))
GoTo Exit_Proc
Case Else
strResult = "1"
GoTo Exit_Proc
End Select
strResult = String.Format("{###-###-####}", Decimal.Parse(hpformat))
Exit_Proc:
PhoneFormat = strResult
hpformat = PhoneFormat
End Function
 
Derek:
Any specific reason why you're using Decimal.Parse? String.Format parameters in n position accept objects, it will accept anything he passes to it.
 
Back
Top