Skip to content

Instantly share code, notes, and snippets.

@CalvinRodo
Created July 19, 2012 16:36
Show Gist options
  • Save CalvinRodo/3145160 to your computer and use it in GitHub Desktop.
Save CalvinRodo/3145160 to your computer and use it in GitHub Desktop.
'Original
Public Shared Function FormatClientNumber(num As String) As String
Dim count As Integer = num.Length \ 3
If num.Length Mod 3 = 0 Then count -= 1 'no trailing dash
'If count > 3 Then count = 3 'format is ABC-999-999-123456789..
For i As Integer = count To 1 Step -1
num = num.Insert(i * 3, "-")
Next
Return num
End Function
'Change to this
Public Shared Function FormatClientNumber(num As String) As String
Dim count As Integer = num.Length \ 3
'Emphasize the order of the operations
If (num.Length Mod 3) = 0 Then
return num 'no trailing dash, no need to go through for loop
End If
'If count > 3 Then count = 3 'format is ABC-999-999-123456789..
For i As Integer = count To 1 Step -1
num = num.Insert(i * 3, "-")
Next
Return num
End Function
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment