Phone number string manipulation

chicago75

Member
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 dont know why this is not working..Help
Chicago75@evildata.com
 
Its expecting a numeric data type.
Code:
strResult = String.Format("{###-###-####}", Decimal.Parse(hpformato))
 
Last edited by a moderator:
String.Format doesnt work with regular expressions. It has its own set of rules (ie; # for an integer). Also you need to specify the location of the value you are formatting... example;

Code:
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 its similar to using..

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

except youre 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
 
Thats an error on my part, which wyrd had correct.
Code:
strResult = String.Format("{[b]0:[/b]###-###-####}", Decimal.Parse(hpformat))
 
Derek:
Any specific reason why youre using Decimal.Parse? String.Format parameters in n position accept objects, it will accept anything he passes to it.
 
Back
Top