View Full Version : formatting fax number on the fly
havey
05-24-2006, 10:47 PM
Hi, I've been using the code below to format fax number on the fly. All works great, but i was wondering if the code can be made more efficient? Thanks for your thoughts.
Function FormatFax(ByVal strFax)
Dim i
Dim strNewFax
If Not IsNull(strFax) Then
For i = 1 To Len(strFax)
If IsNumeric(Mid(strFax, i, 1)) Then
strNewFax = strNewFax & Mid(strFax, i, 1)
End If
Next
End If
' Add a prefix of 1
If strNewFax <> "" Then
strNewFax = "1" & strNewFax
End If
FormatFax = strNewFax
End Function
ghell
05-25-2006, 01:07 PM
is that just
for each character
..only add it to the string if its a number
stick a 1 on the front
?
if so you should be able to do it more efficiently with regular expressions (although i suck at those so it might not be a good idea asking me how, maybe finding all the blocks of numbers and concatinating them together or just replacing the non numbers [^0-9] with an empty string..
technically it would be more efficient if you stored the start position of a block of numbers, kept going till it was no longer a number and then concatenated from the start pos to the prev pos from the current character.. but this would make such a small difference its probably not worth doing.. i would go with regex. (and if you are looping through a lot of them do as much of the regex as possible outside the loop.. particularly as that "ByVal" suggests you are using vb.net in which case you can precompile the regex patterns etc if you dont use the static methods.)
If there is a possibility that the number will already be numeric it may be a good idea to check with IsNumeric() before doing anything, if it is then you can skip straight to the "1" & strNewFax
vBulletin® v3.8.2, Copyright ©2000-2012, Jelsoft Enterprises Ltd.