Shared Function CheersomeColumnize(ByVal text$, ByVal padding_left As Boolean, Optional ByVal glue$ = " ") As String
'fizzle spaces - it means spacing is not really user scalable now
While InStrRev(text, " ") > 0
text = text.Replace(" ", " ")
End While
Dim lines() As String = Strings.Split(text, vbCrLf)
Dim words$() = Nothing, word$ = Nothing
Dim maximals As New Generic.Dictionary(Of Integer, Integer), wix, tln As Integer
Dim U% = lines.GetUpperBound(0)
: Dim parts As New Generic.List(Of String())
'create index (dict) of longest words, known for each column, keeping word arrays in 'parts'
For Each line In lines
If line <> Nothing AndAlso line.Length > 1 Then
words = line.Split(CChar(" "))
: parts.Add(words)
For wix = 0 To UBound(words, 1)
tln = Strings.Len(words(wix))
If maximals.ContainsKey(wix) Then
If tln > maximals(wix) Then maximals.Item(wix) = tln
: Else : maximals(wix) = tln : End If
Next
Else
: parts.Add(New String() {line})
End If
Next
'pad all words from 'parts' according to max index
Dim output(U) As String, noop As Boolean = CBool(maximals.Count = 0)
For i As Integer = 0 To U
words = parts(i)
If Not noop Then
For j As Integer = 0 To UBound(words)
word = words(j)
tln = Strings.Len(word)
If tln < maximals(j) Then
If padding_left Then : words(j) = Strings.LSet(word, maximals(j))
: Else : words(j) = Strings.RSet(word, maximals(j)) : End If
End If
Next
End If
output(i) = Strings.Join(words, glue)
Next
Return Strings.Join(output, vbCrLf)
End Function
Enter your message below
Sign in or Join us (it's free).