Android Question Formatting a string line with multiple string fields

Azhar

Active Member
Licensed User
Longtime User
Hello,

It's a simple question, but I can't find the answer in the forum.

I have a listView that I need to populate line by line with four columns of string data fields.
To have them all aligned up visually (due to various lengths of data in the individual fields) is proving difficult.
Is there a way in which I can concatenate the four fields into a format such that it will force the lengths of each data string field Eg.
Format.string("#######",strOne; "###",strTwo; "####",strThree; "###",strFour) so that they all line up vertically?
So here, strOne will be padded to 6 characters in length, strTwo to 3 characters in length etc

Thanks,

Azhar
 

emexes

Expert
Licensed User
Is there a way in which I can concatenate the four fields into a format such that it will force the lengths of each data string field Eg.
Format.string("#######",strOne; "###",strTwo; "####",strThree; "###",strFour) so that they all line up vertically?
Yes. There are a few String libraries that have functions like LSET or PadLeft or similar, and I have a recollection that the SmartString variable substitution has some optional formatting extensions, or... you can just use standard string concatenation ("&") and substring functions so that you know precisely what is going on, eg, what if the string is too large for the field? Probably easiest done via a function eg:
B4X:
Sub LSET(S As String, L as Int) As String

    Do While S.Length < L
        S = S & " "
    Loop

    Return S.SubString2(0, L)

End Sub

Log(LSET(strOne, 7) & LSET(strTwo, 3) & LSET(strThree, 4) & LSET(strFour, 3))
but you can do it inline too:
B4X:
Log((strOne & "       ").SubString2(0, 7) & (strTwo & "   ").SubString2(0, 3) & (strThree & "    ").SubString2(0, 4) & (strFour & "   ").SubString2(0, 3))
although I would probably simplify that to:
B4X:
Dim LogLine As String = ""
LogLine = LogLine & (strOne   & "       ").SubString2(0, 7)
LogLine = LogLine & (strTwo   & "   "    ).SubString2(0, 3)
LogLine = LogLine & (strThree & "    "   ).SubString2(0, 4)
LogLine = LogLine & (strFour  & "   "    ).SubString2(0, 3)
Log(LogLine)
 
Last edited:
Upvote 0

emexes

Expert
Licensed User
all aligned up visually ... force the lengths of each data string field
Bear in mind that you'll need to use a monospace font eg Courier (cf NOT a proportional font eg Arial etc).

Also, probably better to include an explicit space between columns, otherwise fields that are full (ie, length >= column width) will butt up against each other.
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
Don't use ListView at all. Use xCLV instead.

If you want to create a table then you can use B4XTable or FlexibleTable. If you want it to be all text then check BCTextEngine:

firefox_595n6GLQ5W.png
 
Upvote 0
Top