B4J Question Select a line in string

Lego Jérôme

Member
Licensed User
I wish I could select a line from a string.

The user is invited to fill out a textarea.
After I would like to split this textarea by line.

B4X:
Sub ExtraireLigne(nligne As Int,text As String) As String
    Dim aPos,bPos As Int=0
    Dim t As String
    Dim c As Byte = 26
    text=text.Trim
    For n=0 To nligne-1
        Try
            bPos=text.IndexOf2(Chr(10),aPos)
        Catch
        End Try'ignore
                
            
        Try       
            If bPos = -1 Then           
                bPos=text.Length       
            End If
            
            If bPos-aPos>26 Then
                Do While text.CharAt(aPos+c)<>" "
                c=c-1                   
                Loop
                bPos=aPos+c
            End If
                
            If aPos>text.Length Then
                    t=""
                Return t
            End If
                
                t=text.SubString2(aPos,bPos)           
        
        Catch
            t=""
        End Try
        aPos=bPos+1
    Next
    Return t
End Sub

Actually, this is my sub.
It works, but if the user does not put text in the first lines, my sub will return the first line with text as the first line.
 

Daestrum

Expert
Licensed User
Longtime User
You could use 'getParagraphs' using JavaObject library to get the logical lines (blank lines are ignored).
B4X:
...
 ' ta is the TextArea
 Dim lines As List = asJO(ta).RunMethod("getParagraphs",Null)
 Log(lines)
End Sub
Sub asJO(o As JavaObject) As JavaObject
 Return o
End Sub

Then you can use For Each on the list to traverse the lines
 
Upvote 0
Top