I have created a console application that asks the user to enter a string and then enter a value to pad the string to the left or to the right. It also asks the user to enter a character that will be used when the padding occurs. So far this is what my function looks like:
Public Function Pad(ByVal src As String, _
ByVal len As Integer, _
ByVal theChar As String, _
Optional ByVal bToTheLeft As Boolean = False) As String
Dim i As Integer
If len <= 0 Then Return ""
If src.Length >= len Then Return src.Substring(0 , len)
If bToTheLeft Then ' pad to the left
Return theChar & src
Else
Return src & theChar
this will only get the specified character to pad once on the left or right but not the number of of times as entered by the user.
Example: if the entered string to pad was "Trace" and the amount to pad is 10, and the entered character is #, i get a return value of #Trace, when it should read #####Trace (using len - src.Length)
I have tried to use a for loop to pad the string the given number of times but so far have not got the right results, can anyone help me please?
Public Function Pad(ByVal src As String, _
ByVal len As Integer, _
ByVal theChar As String, _
Optional ByVal bToTheLeft As Boolean = False) As String
Dim i As Integer
If len <= 0 Then Return ""
If src.Length >= len Then Return src.Substring(0 , len)
If bToTheLeft Then ' pad to the left
Return theChar & src
Else
Return src & theChar
this will only get the specified character to pad once on the left or right but not the number of of times as entered by the user.
Example: if the entered string to pad was "Trace" and the amount to pad is 10, and the entered character is #, i get a return value of #Trace, when it should read #####Trace (using len - src.Length)
I have tried to use a for loop to pad the string the given number of times but so far have not got the right results, can anyone help me please?
Comment