Sub Activity_Create(FirstTime As Boolean)
Dim t(100), Txt As String
Dim i, n As Int
Txt = "This is one very long line now is the time for all good men to come to the aid of their country. The quick brown fox jumped over the lazy dog."
n = 0
Do While Txt.Length > 29
i = 29
Do While Txt.CharAt(i) <> " " AND i > 0
i = i - 1
Loop
If i > 0 Then
t(n) = Txt.SubString2(0, i)
n = n + 1
Txt = Txt.SubString(i + 1)
End If
Loop
If Txt <> "" Then t(n) = Txt
End Sub
Thanks my friend, that's exactly what I was looking for.
I replace the hard coded width of 29 with whatever the report with needs and any long string can be spread over many lines - keeping sentences under the max width of the line. BEA utiful...
Sub Activity_Create(FirstTime As Boolean)
Dim t(100), Txt As String
Dim i, n As Int
Txt = "This is one very long line now is the time for all good men to come to the aid of their country. The quick brown fox jumped over the lazy dog."
n = 0
Do While Txt.Length > 29
i = 29
Do While Txt.CharAt(i) <> " " AND i > 0
i = i - 1
Loop
If i > 0 Then
t(n) = Txt.SubString2(0, i)
n = n + 1
Txt = Txt.SubString(i + 1)
End If
Loop
If Txt <> "" Then t(n) = Txt
End Sub
If Txt has no spaces in the first 29 characters (or only at character position 0), i will go to 0 and the code after "If i > 0 Then" will not execute and the loop will repeat endlessly. Although you may not ever expect this to happen, technically the code should account for the possibility in some way, such as:
B4X:
If i = 0 Then i = 29
t(n) = Txt.SubString2(0, i)
n = n + 1
Txt = Txt.SubString(i + 1)