We Recommend

Beginning VB.NET Beginning VB.NET
Visual Basic .NET is the latest version of the most widely used programming language in the world, popular with professional developers and complete beginners alike. This book will teach you Visual Basic .NET from first principles. You'll quickly and easily learn how to write Visual Basic .NET code and create attractive windows and forms for the users of your applications.


Posted By

mafro on 10/10/07


Tagged

Pad


Versions (?)


Who likes this?

1 person has marked this snippet as a favorite

mafro


Ultimate Pad String


Published in: VB.NET 


Pad a string with any character up to a fixed length.


  1. ''' <summary>Pad an int with an arbitary number of chars</summary>
  2. ''' <param name="n">Integer to pad</param>
  3. ''' <param name="len">Length of the resulting padded string</param>
  4. ''' <param name="c">Char to pad</param>
  5. ''' <returns>Padded string</returns>
  6. Private Function Pad(ByVal n As Int32, ByVal len As Int32, ByVal c As Char) As String
  7. If n > (10^len) Then
  8. Return Convert.ToString(n)
  9. ElseIf n < (10^len) And n > (10^(len-1)) Then
  10. Return Pad(n, len-1, c)
  11. Else
  12. Dim t As String = Convert.ToString(n)
  13. Dim s As String = ""
  14. For j As Int32 = 1 To len - t.Length
  15. s &= c
  16. Next
  17. Return s & n
  18. End If
  19. End Function
  20.  
  21. ''' <summary>Pad an int with an arbitary number of zeros</summary>
  22. ''' <param name="n">Integer to pad</param>
  23. ''' <param name="len">Length of the resulting padded string</param>
  24. ''' <returns>Padded string</returns>
  25. Private Function Pad(ByVal n As Int32, ByVal len As Int32) As String
  26. Return Pad(n, len, "0"c)
  27. End Function

Report this snippet 

You need to login to post a comment.