Android Question Word Wrap cut Words

hasexxl1988

Active Member
Licensed User
Longtime User
Hello,
sorry that I'm annoying again ...

Is it feasible that words are not truncated in the Wordwrap function?

Current code:

B4X:
Wordwrap ("Hello my Name ist Test Text 123.",10)
Sub Wordwrap (str As String, count As Int)

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

    myList.Add(str.SubString(i))

End Sub

Result is:

Hello my N
ame is Tes
t Text 123
.

i Need:
Hello my
Name is
Test Text
123.
 

udg

Expert
Licensed User
Longtime User
If you really want to code it yourself, have a look at the regex pattern "\b" which returns all the full words in your sentence. You then pick from the list as many words they fit in the max allowed space you set (10 in your example), add a newline and start again until the list is fully traversed.
In your other thread I post a quick example of how you could use regex (regular expression).
 
Last edited:
Upvote 0

hasexxl1988

Active Member
Licensed User
Longtime User
If you really want to code it yourself, have a look at the regex pattern "\b" which returns all the full words in your sentence. You then pick from the list as many words they fit in the max allowed space you set (10 in your example), add a newline and start again until the list is fully traversed.
In your other thread I post a quick example of how you could use regex (regular expression).

How can this be realized that he always loads a maximum of 15 characters and then omits only whole words?

I can not find a solution to the problem ^^
 
Upvote 0

udg

Expert
Licensed User
Longtime User
Once you have the list of single words, you start adding word by word checking the maxlimit of 15 chars.
When it's exceeded you keep the last n-1 words, add the newline and start again beginning with the nth word.
That should work.
 
Upvote 0

hasexxl1988

Active Member
Licensed User
Longtime User
Once you have the list of single words, you start adding word by word checking the maxlimit of 15 chars.
When it's exceeded you keep the last n-1 words, add the newline and start again beginning with the nth word.
That should work.

I can divide the words into individual strings, but how can I reconcile that with Regex, which automatically generates a new line after 15 characters?
 
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
You can´t do it with regex. You need to write your own sub to split it like you wish.
 
Upvote 0

udg

Expert
Licensed User
Longtime User
My initial suggestion about regex was intented to substitute the for loop with a single regex instruction. Nothing more.
But keep in mind what Erel suggested about the usage of a Label. Do you really need to devise your own wordwrap code?
 
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
Do you really need to devise your own wordwrap code?
in #4 he told that he need to use it for drawing on a Canvas.

Regex can still a help here to split the words.
But it also need a bit of own logic to wrap the words to be a maximim of x chars in a line. It shouldn´t be complicated though.
 
Upvote 0

udg

Expert
Licensed User
Longtime User
Oh, I missed that. Yes it could be something like this:
B4X:
myList.Initialize
Dim myString As String = "This is a long string which will be split into individual set size strings"
Dim pattern As String = " \b"
myList = Regex.Split(pattern, myString)
MyWordwrap(15)
'....

Sub MyWordwrap(size As Int)
   Dim w As String
   Dim start, sz As Int
   sz = 0
   start = 0
   For i= 0 To myList.size-1
       w = myList.Get(i)
       sz = sz+ w.Length
       If sz > size Then
           Dim sb As StringBuilder
           sb.Initialize
           For k = start To i-1
               sb.Append(myList.get(k))
           Next
           sb.Append(CRLF)
           Log(sb.ToString)
           start = i
           w = myList.Get(i)
           sz =w.length
       End If
   Next
   sb.Initialize
   For k = start To myList.size-1
       sb.Append(myList.get(k))
   Next
   Log(sb.ToString)
End Sub

or a bit better with optional spaces
B4X:
Sub MyWordwrap(size As Int, spaces As Boolean)
   Dim sb As StringBuilder
   Dim w As String
   Dim start = 0, sz = 0 As Int
   For i= 0 To myList.size-1
       w = myList.Get(i)
       If spaces Then sz = sz+ w.Length+1 Else sz = sz+ w.Length
       If sz = size +1 Then
           sb.Initialize
           For k = start To i
               sb.Append(myList.get(k)).Append(" ")
           Next
           sb.Remove(sb.Length-1,sb.Length-1)
           sb.Append(CRLF)
           Log(sb.ToString)
           start = i+1
           sz = 0
       End If
       If sz > size Then
           sb.Initialize
           For k = start To i-1
               sb.Append(myList.get(k)).Append(" ")
           Next
           sb.Remove(sb.Length-1,sb.Length-1)
           sb.Append(CRLF)
           Log(sb.ToString)
           start = i
           w = myList.Get(i)
           If spaces Then sz = w.Length+1 Else sz = w.Length
       End If
   Next
   sb.Initialize
   For k = start To myList.size-1
       sb.Append(myList.get(k)).Append(" ")
   Next
   Log(sb.ToString)
End Sub
 
Last edited:
Upvote 0

hasexxl1988

Active Member
Licensed User
Longtime User
Oh, I missed that. Yes it could be something like this:
B4X:
myList.Initialize
Dim myString As String = "This is a long string which will be split into individual set size strings"
Dim pattern As String = " \b"
myList = Regex.Split(pattern, myString)
MyWordwrap(15)
'....

Sub MyWordwrap(size As Int)
   Dim w As String
   Dim start, sz As Int
   sz = 0
   start = 0
   For i= 0 To myList.size-1
       w = myList.Get(i)
       sz = sz+ w.Length
       If sz > size Then
           Dim sb As StringBuilder
           sb.Initialize
           For k = start To i-1
               sb.Append(myList.get(k))
           Next
           sb.Append(CRLF)
           Log(sb.ToString)
           start = i
           w = myList.Get(i)
           sz =w.length
       End If
   Next
   sb.Initialize
   For k = start To myList.size-1
       sb.Append(myList.get(k))
   Next
   Log(sb.ToString)
End Sub

or a bit better with optional spaces
B4X:
Sub MyWordwrap(size As Int, spaces As Boolean)
   Dim sb As StringBuilder
   Dim w As String
   Dim start = 0, sz = 0 As Int
   For i= 0 To myList.size-1
       w = myList.Get(i)
       If spaces Then sz = sz+ w.Length+1 Else sz = sz+ w.Length
       If sz = size +1 Then
           sb.Initialize
           For k = start To i
               sb.Append(myList.get(k)).Append(" ")
           Next
           sb.Remove(sb.Length-1,sb.Length-1)
           sb.Append(CRLF)
           Log(sb.ToString)
           start = i+1
           sz = 0
       End If
       If sz > size Then
           sb.Initialize
           For k = start To i-1
               sb.Append(myList.get(k)).Append(" ")
           Next
           sb.Remove(sb.Length-1,sb.Length-1)
           sb.Append(CRLF)
           Log(sb.ToString)
           start = i
           w = myList.Get(i)
           If spaces Then sz = w.Length+1 Else sz = w.Length
       End If
   Next
   sb.Initialize
   For k = start To myList.size-1
       sb.Append(myList.get(k)).Append(" ")
   Next
   Log(sb.ToString)
End Sub

Very thanks :D nice guy :D
 
Upvote 0
Top