Android Question Convert word wrap code

hub73

Active Member
Licensed User
Longtime User
Hi !
Could you help me to convert this code to b4a native commands !
Thanks !

B4X:
'Function Wrapper$(Var$,width)
'    While Var <> ""
'        result$ = ""
'    ;if length of string Is greater than our width
'        If Len(Var) > width
'            spotcheck = width
'            char$ = ""
'        ;Look For the last space in our Next 'width chunk' of the MasterText
'            Repeat
'                spotcheck = spotcheck - 1
'                char = Mid(Var,spotcheck,1)
'            Until char = " " Or spotcheck = 0
'        ;didn't find a space
'            If spotcheck = 0
'                result = Left(Var,width)
'                Var = Right(Var,Len(Var) - width)
'        ;found a space
'            Else
'                result = Left(Var,spotcheck - 1)
'                Var = Right(Var,Len(Var) - spotcheck)
'            EndIf
'    ;length of string Is less than Return width so send it all back
'        Else
'            result=Var
'            Var=""
'        EndIf
'        Print result
'    Wend
'End Function

i've this

B4X:
Sub WordWrap(Texte As String, Largeur As Int) As String

    Dim Resultat As String
    Dim spotcheck As Int
    Dim Caractere As String
  
    Do While Texte <> ""
        Resultat = ""
    'if length of string Is greater than our Largeur
        If Texte.Length > Largeur Then
            spotcheck = Largeur
            Caractere = ""
        'Look For the last space in our Next 'Largeur chunk' of the MasterText
            Do While Caractere = " " Or spotcheck = 0
                spotcheck = spotcheck - 1
                Caractere = Texte.SubString2(spotcheck,1)
            Loop
        'didn't find a space
            If spotcheck = 0 Then
                Resultat = Texte.substring2 (0, Largeur) 
                Texte =  Texte.substring2 (Texte.Length - Largeur, Texte.Length) 
        'found a space
            Else
                Resultat = Texte.SubString2 (0,spotcheck - 1)
                Texte = Texte.SubString2 (Texte.Length - spotcheck, Texte.Length)
            End If
    'length of string Is less than Return Largeur so send it all back
        Else
            Resultat=Texte
            Texte=""
        End If
        Return Resultat
    Loop
End Sub
But not works...
Thanks for the help !
 

eurojam

Well-Known Member
Licensed User
Longtime User
I think the problem is - withou testing, just having a short look to your code - that you make from the

B4X:
'            Repeat
'                spotcheck = spotcheck - 1
'                char = Mid(Var,spotcheck,1)
'            Until char = " " Or spotcheck = 0

a loop like this

B4X:
  Do While Caractere = " " Or spotcheck = 0
                spotcheck = spotcheck - 1
                Caractere = Texte.SubString2(spotcheck,1)
Loop

it should be
B4X:
  Do While Caractere <> " " AND spotcheck <> 0
                spotcheck = spotcheck - 1
                Caractere = Texte.SubString2(spotcheck,1)
Loop

because the repeat until stops when char = " " Or spotcheck = 0
and you do it while this statement is true
 
Upvote 0

hub73

Active Member
Licensed User
Longtime User
Many thanks, but the code crash, so...

algorithm from wiki :
https://en.wikipedia.org/wiki/Line_wrap_and_word_wrap


B4X:
Sub WordWrap (Texte As String, Longueur As Int) As String
   
    Dim SpaceLeft As Int
    Dim LineWidth As Int
    Dim Mot As String
    Dim Resultat As String
   
    Dim Liste_mots As List

    Resultat = ""
       
    Liste_mots = StringSplit (Texte)
   
    SpaceLeft = Longueur
   
    For i = 0 To Liste_mots.Size - 1
   
        Mot = Liste_mots.Get(i)
         
        If Mot.Length + 1 > SpaceLeft Then
            Mot = CRLF & Mot
            SpaceLeft = Longueur - Mot.Length
            Resultat = Resultat & Mot     

        Else
            SpaceLeft = SpaceLeft - (Mot.Length + 1)
            Resultat = Resultat & " " & Mot     
        End If
         
    Next
   
    Return Resultat

End Sub

' from forum search
Sub StringSplit(InputString As String) As List
   
   Dim OutList As List
   Dim CommaLoc As Int
   
   OutList.Initialize
   CommaLoc=InputString.IndexOf(" ")
   
   Do While CommaLoc >-1      
      Dim LeftSide As String : LeftSide= InputString.SubString2(0,CommaLoc)
      Dim RightSide As String :RightSide= InputString.SubString(CommaLoc+1)
      OutList.Add(LeftSide)
      InputString=RightSide
      CommaLoc=InputString.IndexOf(" ")
   Loop
   
   OutList.Add(InputString)
   
   Return OutList

End Sub
 
Upvote 0

hub73

Active Member
Licensed User
Longtime User
works fine,
(i don't understand how to adapt the 'Minimum raggedness' wiki explanation used by 'TeX' in this code...)
 
Upvote 0
Top