Android Question Auto Line break after X Chars

hasexxl1988

Active Member
Licensed User
Longtime User
Hello,
is it possible to force a line break after a certain number of characters?

Say: After every 15th character a linebreak is generated in the text or string?

123456789012345 (here Autobreak) 456464654654654 (here Autobreak) 64668

I have not found anything on the subject in the community and Google unfortunately.

Thanks :)

EDIT:
i have found....sorry for the Question.

B4X:
Dim myList As List
myList.Initialize
Dim myString As String = "This is a long string which will be split into individual set size strings"

Wordwrap(myString, 13)

Sub Wordwrap (str As String, count As Int)

  myList.Clear
  str = str.Replace(" ", "")
  For i = 0 To str.Length - count Step count
  myList.Add(str.SubString2(i, i + count))
  Next

  'add remainder of string
  myList.Add(str.SubString(i))

End Sub
 

udg

Expert
Licensed User
Longtime User
You could use regular expressions too. A pattern like
.{13}
matches blocks of 13 chars (including spaces), so you could use it to return each block, append a newline code and so on until the last block which could be less than "13" but you know it in advance.
A quick example in B4J
B4X:
Dim myList As List
myList.Initialize
Dim myString As String = "This is a long string which will be split into individual set size strings"
Dim pattern As String = ".{13}"
myList = Regex.Split(pattern, myString)
Log(myList.Size)
Log(myList.Get(myList.Size-1))
 
Last edited:
Upvote 0
Top