Android Question stringfunctions2 pad

mterveen

Member
Licensed User
Longtime User
it appears that the pad function is incorrect. the "for" line adds one too many characters. it should be (length - text.length - 1). or am i missing something?

B4X:
Sub Pad (Text As String, PadWith As String, Length As Int, Post As Boolean) As String
    'Classic
    Private Str As String = ""
    Private FillStr As String
    If PadWith= "" Then PadWith = "*"
    For i=0 To Length - Text.Length
        FillStr = FillStr & PadWith.CharAt(0)
    Next
    If Post Then
        Str = Text & FillStr
    Else
        Str = FillStr & Text
    End If
    Return Str
End Sub
 

emexes

Expert
Licensed User
or am i missing something?
I think you are correct. But it is odd that it hasn't been noticed earlier, so I am a bit wary of missing something too :)
the "for" line adds one too many characters. it should be (length - text.length - 1)
I would be more inclined to change the loop start to count using natural numbers, eg:
B4X:
For I = 1 To Length - Text.Length
 
Upvote 0
Top