Friday, May 17, 2013

VB 2010 - PAD A STRING WITH CHARACTERS


Example : PADL("1",4,"0") = "0001"
                 PADR("1,4,"0") = "1000"



Public Class Form1
    Private clsStr As clsStringFunction
    Private Sub Button2_Click(sender As System.Object, e As System.EventArgs) Handles Button2.Click
        Me.Close()
        Application.Exit()
    End Sub

    Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
        clsStr = New clsStringFunction
        Me.TextBox1.Text = "CT." & clsStr.PADL("1", 7, "0")
    End Sub

    Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
        Me.TextBox1.Enabled = False
        Me.TextBox2.Focus()
    End Sub
End Class



Public Class clsStringFunction
    Public Function PADL(ByVal cWord As String, ByVal nLen As Integer, ByVal cInsert As String) As String

        Dim cResult As String
        Dim cTemp As String

        cTemp = ""
        cResult = ""

        If nLen > Len(cWord) Then
            For i = 0 To nLen - Len(cWord) - 1
                cTemp = cTemp & cInsert
            Next
            cResult = cTemp & cWord
        Else
            cResult = cWord
        End If
        Return cResult
    End Function

    Public Function PADR(ByVal cWord As String, ByVal nLen As Integer, ByVal cInsert As String) As String

        Dim cResult As String
        Dim cTemp As String

        cTemp = ""
        cResult = ""
        If nLen > Len(cWord) Then
            For i = 0 To nLen - Len(cWord) - 1
                cTemp = cTemp & cInsert
            Next
            cResult = cWord & cTemp
        Else
            cResult = cWord
        End If
        Return cResult
    End Function
End Class

 

Twitter Delicious Facebook Digg Stumbleupon Favorites More

 
Design by Free WordPress Themes | Bloggerized by Lasantha - Premium Blogger Themes | Affiliate Network Reviews