HiveBrain v1.2.0
Get Started
← Back to all entries
patterncsharpMinor

Zero pad function trim extra leading zeros

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
leadingzerostrimfunctionpadzeroextra

Problem

Here is a zero pad function I have:

''' 
''' Take a number string and zero pad it to the specified length.
''' 
''' The string representation of the value that will be zero-padded.
''' The length of the string that will result after the zero-padding.
''' An optional boolean which, when true, indicates that any 
''' extra leading zeroes that make the string longer than the desired length should be removed.
''' A value which is the same as  but zero padded to the length specified by .
''' 
Friend Function ZeroPad(ByVal Number As String, ByVal ToLength As Integer, Optional ByVal TrimExtraZeroes As Boolean = False) As String
    Const ZeroChar = "0"c

    Select Case Number.Length
        Case Is  ToLength
            ZeroPad = Number

            If (TrimExtraZeroes) Then
                Do While ZeroPad.StartsWith(ZeroChar) AndAlso ZeroPad.Length > ToLength
                    ZeroPad = ZeroPad.Substring(1)
                Loop
            End If
        Case Else
            'This case can never occur, but it's here to suppress compiler warning.
            Throw New InvalidOperationException(String.Format("Unknown number length: {0}", Number.Length))
    End Select
End Function


Now, focusing on just one portion of it -- the part that trims leading zeros:

Do While ZeroPad.StartsWith(ZeroChar) AndAlso ZeroPad.Length > ToLength
    ZeroPad = ZeroPad.Substring(1)
Loop


is there a more efficient, cleaner, or "slick" way to do this other than looping?

Solution

Naming

Based on the naming guidelines parameters should be named using camelCase casing.

Select Case

If you omitt the Case Is > ToLength and the else part aka having only 2 cases you will have

Friend Function ZeroPad(ByVal Number As String, ByVal ToLength As Integer, Optional ByVal TrimExtraZeroes As Boolean = False) As String
    Const ZeroChar = "0"c

    Select Case Number.Length
        Case Is  ToLength
             Number = Number.Substring(1)
         Loop
     End If
    Return Number
End Function


Why don't you use the built in String.PadLeft() method which would obsolete the Select Case. In addition to first stripping any 0 of the left side of Number you won't need the loop at all.

This boils down to only this

Friend Function ZeroPad(ByVal Number As String, ByVal ToLength As Integer, Optional ByVal TrimExtraZeroes As Boolean = False) As String
    Const ZeroChar = "0"c
    If (TrimExtraZeroes) Then
        Number = Number.TrimStart({ZeroChar})
    End If
    Return Number.PadLeft(ToLength, ZeroChar)
End Function


Update

Some timetables for calling both methods 1.000.000 times

Yours(ms)  Mine(ms)
ZeroPad("0000000001", 2, False)       227       231
ZeroPad("0000000001", 2, True)       1696       297
ZeroPad("01", 2, False)               251       235
ZeroPad("01", 2, True)                248       263  
ZeroPad("01", 12, False)              256       247
ZeroPad("01", 12, True)               257       260

Code Snippets

Friend Function ZeroPad(ByVal Number As String, ByVal ToLength As Integer, Optional ByVal TrimExtraZeroes As Boolean = False) As String
    Const ZeroChar = "0"c

    Select Case Number.Length
        Case Is < ToLength
            Return New String(ZeroChar, ToLength - Number.Length) & Number
        Case Is = ToLength
            Return Number
     End Select

     If (TrimExtraZeroes) Then
         Do While Number.StartsWith(ZeroChar) AndAlso Number.Length > ToLength
             Number = Number.Substring(1)
         Loop
     End If
    Return Number
End Function
Friend Function ZeroPad(ByVal Number As String, ByVal ToLength As Integer, Optional ByVal TrimExtraZeroes As Boolean = False) As String
    Const ZeroChar = "0"c
    If (TrimExtraZeroes) Then
        Number = Number.TrimStart({ZeroChar})
    End If
    Return Number.PadLeft(ToLength, ZeroChar)
End Function
Yours(ms)  Mine(ms)
ZeroPad("0000000001", 2, False)       227       231
ZeroPad("0000000001", 2, True)       1696       297
ZeroPad("01", 2, False)               251       235
ZeroPad("01", 2, True)                248       263  
ZeroPad("01", 12, False)              256       247
ZeroPad("01", 12, True)               257       260

Context

StackExchange Code Review Q#76990, answer score: 7

Revisions (0)

No revisions yet.