Here is a zero pad function I have:
''' <summary>
''' Take a number string and zero pad it to the specified length.
''' </summary>
''' <param name="Number">The string representation of the value that will be zero-padded.</param>
''' <param name="ToLength">The length of the string that will result after the zero-padding.</param>
''' <param name="TrimExtraZeroes">An optional boolean which, when true, indicates that any
''' extra leading zeroes that make the string longer than the desired length should be removed.</param>
''' <returns>A value which is the same as <paramref name="Number" /> but zero padded to the length specified by <paramref name="ToLength" />.</returns>
''' <remarks></remarks>
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
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?