Android Question Split text prevent words breaking across next line

scsjc

Well-Known Member
Licensed User
Longtime User
Hello
I need to be able to reduce a text without cutting the words, and move on to the next line if necessary.

example correct:
I need to be able to reduce
a text without cutting the
words, and move on to the
next line if necessary. Does
anyone have any method
for this?

example incorrect:

I need to be able to reduc
e a text without cutting th
e words, and move on to t
he next line if necessary. D
oes anyone have any meth
od for this?


Does anyone have any method for this?
thanks !!!
 

Jeffrey Cameron

Well-Known Member
Licensed User
Longtime User
This method is from memory but it should get you in the right direction. It could be optimized somewhat and converted entirely to use StringBuilders, etc. but it should give you a starting point.

** DISCLAIMER *** UNTESTED, CODED FROM MEMORY
B4X:
Public Sub LineWrap(Text As String, MaxCharacters As Int) As String
    Dim psWords() As String
    Dim poLines As List
    Dim psCurrentLine As String
    Dim piIndex As Int
    Dim psCheck As String
  
    psWords = Regex.Split(" ", Text)
    poLines.Initialize
  
  
    If Text.Length < MaxCharacters Or psWords.Length < 2 Then
        Return Text
    End If
  
    psCurrentLine = psWords(0) 
    For piIndex = 1 To psWords.Length - 1
        psCheck = psCurrentLine & " " & psWords(piIndex)
        If psCheck.Length > MaxCharacters Then
            poLines.Add(psCurrentLine)
            psCurrentLine = psWords(piIndex)
            If piIndex = psWords.Length -1 Then
                poLines.Add(psCurrentLine)
            End If
        Else
            If piIndex = psWords.Length -1 Then
                poLines.Add(psCheck)
            End If
            psCurrentLine = psCheck
        End If
      
    Next
  
    Dim poSb As StringBuilder
    poSb.Initialize
    For piIndex = 0 To poLines.Size - 1
        poSb.Append(poLines.Get(piIndex)).Append(CRLF)
    Next
    Return poSb.ToString 
End Sub
 
Upvote 0

scsjc

Well-Known Member
Licensed User
Longtime User
This method is from memory but it should get you in the right direction. It could be optimized somewhat and converted entirely to use StringBuilders, etc. but it should give you a starting point.

** DISCLAIMER *** UNTESTED, CODED FROM MEMORY
B4X:
Public Sub LineWrap(Text As String, MaxCharacters As Int) As String
    Dim psWords() As String
    Dim poLines As List
    Dim psCurrentLine As String
    Dim piIndex As Int
    Dim psCheck As String
 
    psWords = Regex.Split(" ", Text)
    poLines.Initialize
 
 
    If Text.Length < MaxCharacters Or psWords.Length < 2 Then
        Return Text
    End If
 
    psCurrentLine = psWords(0)
    For piIndex = 1 To psWords.Length - 1
        psCheck = psCurrentLine & " " & psWords(piIndex)
        If psCheck.Length > MaxCharacters Then
            poLines.Add(psCurrentLine)
            psCurrentLine = psWords(piIndex)
            If piIndex = psWords.Length -1 Then
                poLines.Add(psCurrentLine)
            End If
        Else
            If piIndex = psWords.Length -1 Then
                poLines.Add(psCheck)
            End If
            psCurrentLine = psCheck
        End If
     
    Next
 
    Dim poSb As StringBuilder
    poSb.Initialize
    For piIndex = 0 To poLines.Size - 1
        poSb.Append(poLines.Get(piIndex)).Append(CRLF)
    Next
    Return poSb.ToString
End Sub


thanks.... is perfect
 
Upvote 0

AnandGupta

Expert
Licensed User
Longtime User
Wow ! Jeffrey Cameron, your code from memory turned out perfect.
Which version of of memory you are using ;) ?

Regards,

Anand
 
Upvote 0

Jeffrey Cameron

Well-Known Member
Licensed User
Longtime User
I'm using my "hey, old guy!" memory... My first point-of-sale program was written in MicroFocus COBOL on a MS-DOS 3.0 machine. You had to do everything by hand in those days, there were no ".SingleLine = False" shortcuts. :D
 
Upvote 0
Top