Android Question Get the first line of an EditText Multiline

Mattiaf

Active Member
Licensed User
Hi, I'm trying to get the first string of a multiline EditText.
Each line is splitted from the others by a CRLF and the first 2 lines of the edittext are empty too.
In few words, this is the input
B4X:
2 empty lines that the forum is removing if i don't insert text
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' same of above
    -CA13-ISTITUTO SUPERIORE BACCAREDDA VIA GRANDI 9/11 - N°2 CHIAVI MECCANICHE

    -CA14-ISTITUTO SUPERIORE MICHELANGELO VIA EFISIO MELIS/VIA SCHIAFFINO - N°3 CHIAVI MECCANICHE

    -CA15-ISTITUTO SUPERIORE DE SANCTIS DELEDDA VIA CORNALIAS 169 - N°2 CHIAVI MECCANICHE

    -CA16-ISTITUTO SUPERIORE TECNICO COMMERCIALE MARTINI VIA CIUSA 4 - N°4 CHIAVI MECCANICHE

    -CA17-ISTITUTO SUPERIORE SIOTTO VIALE TRENTO 103 - N°2 CHIAVI MECCANICHE

    -CA18-ISTITUTO SUPERIORE BUCCARI MARCONI VIA VALERIO PISANO - N°4 CHIAVI MECCANICHE

    -CA19-ISTITUTO SUPERIORE PACINOTTI VIA BRIANZA 1 - N°2 CHIAVI MECCANICHE

    -CA20-ISTITUTO SUPERIORE PACINOTTI VIA LIGURIA - N°1 CHIAVE MECCANICA

    -CA21-ISTITUTO SUPERIORE AZUNI VIA CODROIPO - N°4 CHIAVI MECCANICHE

    -CA22-ISTITUTO SUPERIORE MICHELANGELO VIA DEI DONORATICO/VIA CAPPELLINO - N°5 CHIAVI MECCANICHE

    -CA23-ISTITUTO SUPERIORE DE SANCTIS DELEDDA VIA SULCIS - N°5 CHIAVI MECCANICHE

    -CA24-ISTITUTO SUPERIORE AZUNI VIA IS MAGLIAS 132 - N°5 CHIAVI MECCANICHE

    -CA25-ISTITUTO SUPERIORE GIUA VIA MONTECASSINO - N°3 CHIAVI MECCANICHE

    -CA26-ISTITUTO SUPERIORE ALBERTI VIALE COLOMBO 37 - N°1 CHIAVE MECCANICA

    -CA27-ISTITUTO SUPERIORE ELEONORA D'ARBOREA VIA CARBONI-BOI/ANGOLO VIA AMAT - N°1 CHIAVE MECCANICA

    -CA28-ISTITUTO SUPERIORE EUCLIDE VIA LIGAS + SUCCURSALE - N°6 CHIAVI MECCANICHE

    -CA29-ISTITUTO SUPERIORE ELEONORA D'ARBOREA VIA S.CIVITA - N°3 CHIAVI MECCANICHE

    -CA30-ISTITUTO SUPERIORE ALBERTI VIA RAVENNA - N°4 CHIAVI MECCANICHE

    -CA31-ISTITUTO SUPERIORE DETTORI VIA CUGIA 2 - N°3 CHIAVI MECCANICHE

    -CA32-ISTITUTO SUPERIORE AZUNI VIA MONTE ACUTO - N°4 CHIAVI MECCANICHE

    -CA33-ISTITUTO SUPERIORE MEUCCI VIA VESALIO 16 - N°3 CHIAVI MECCANICHE

    -CA34-ISTITUTO SUPERIORE FOISO FOIS VIA S.EUSEBIO - N°3 CHIAVI MECCANICHE

    -CA35-ISTITUTO SUPERIORE DETTORI VIA ROLANDO

    -CA100 ISTITUTO PERTINI VIA CARPACCIO N° 18 A - N° 1 CHIAVE MECCANICA

I'm trying with the library StringFunctions
B4X:
Dim firstline As String
    Dim str As StringFunctions
    firstline=str.Trim(str.SplitGetWord(EditText2.text,Chr(13),1))
    Log (firstline)
I have also tried the Second example ( because I cannot be sure if there are always other lines ) of This Topic but it seems not working
but it only works if the lines are one below the others..
Is there a way to fix it?
 
Last edited:

Mattiaf

Active Member
Licensed User
Assuming I only have the first two lines, with the following code I'm trying to remove crfl

B4X:
EditText2.Text=EditText2.Text.Replace(CRLF,"")
    Sleep(500)
    Dim firstline As String
    Dim str As StringFunctions
    firstline=str.Trim(str.SplitGetWord(EditText2.text,Chr(13),1))
    Log (firstline)

but instead of
line1
line2
I'm getting
line1 line2

is there a reason?
 
Upvote 0

emexes

Expert
Licensed User
Try this:
Sub GetFirstNonBlankLine(S As String) As String
    
    Dim m As Matcher = Regex.Matcher("\n([^\n]+)\n", CRLF & S & CRLF)
    
    If m.Find Then
        Return m.Group(1)
    Else
        Return "There are no non-blank lines"
    End If

End Sub

Test with this:
Log("First non-blank line is """ & GetFirstNonBlankLine(TextArea1.Text) & """")

and some input text like this:

1668483129844.png


and hopefully you'll get something like this:

1668483330005.png
 
Upvote 0

emexes

Expert
Licensed User
Your next hurdle is probably non-blank lines that look blank, eg contain only spaces etc 🙃

which would appear fixable by adding \s* to the regex pattern:

B4X:
Sub GetFirstNonBlankLine(S As String) As String
   
    Dim m As Matcher = Regex.Matcher("\n\s*([^\n]+)\n", CRLF & S & CRLF)
   
    If m.Find Then
        Return m.Group(1)
    Else
        Return "There are no non-blank lines"
    End If

End Sub

although now the pattern will strip leading spaces from the non-blank line, but not strip trailing spaces...

but you could argue this is a balanced compromise solution to the question of whether excess spaces should be trimmed or not ⚖️
 
Upvote 0

emexes

Expert
Licensed User
Lol now you've got me on a roll...

B4X:
Sub GetFirstNonBlankLine(S As String) As String
 
    S = CRLF & S & CRLF
 
    Do While S.Contains(CRLF & " " ) : S = S.Replace(CRLF & " " , CRLF) : Loop    'get rid of leading spaces
    Do While S.Contains(" "  & CRLF) : S = S.Replace(" "  & CRLF, CRLF) : Loop    'get rid of trailing spaces
    Do While S.Contains(CRLF & CRLF) : S = S.Replace(CRLF & CRLF, CRLF) : Loop    'get rid of empty lines
 
    If S.Length > 2 Then    'if there were any non-blank lines, then will be at least 3 chars ie CRLF & something & CRLF
        Return(S.SubString2(1, S.IndexOf2(CRLF, 1)))    'return text between first CRLF (at start of string) and second CRLF
    Else
        Return "There are no non-blank lines"
    End If

End Sub

or this, if you're ok with getting a blank line back if there are no non-blank lines:

B4X:
Sub GetFirstNonBlankLine(S As String) As String
 
    S = CRLF & S & CRLF
 
    Do While S.Contains(CRLF & " " ) : S = S.Replace(CRLF & " " , CRLF) : Loop    'get rid of leading spaces
    Do While S.Contains(" "  & CRLF) : S = S.Replace(" "  & CRLF, CRLF) : Loop    'get rid of trailing spaces
    Do While S.Contains(CRLF & CRLF) : S = S.Replace(CRLF & CRLF, CRLF) : Loop    'get rid of empty lines
 
    If S.Length < 2 Then S = S & CRLF    'S might have been reduced to a single CRLF, but we need at least two for the line extraction
 
    Return(S.SubString2(1, S.IndexOf2(CRLF, 1)))    'return line extracted from between first CRLF (at start of string) and second CRLF
 
End Sub

I didn't test that last one, but hey, what could possibly go wrong?!
 
Upvote 0

emexes

Expert
Licensed User
I'm trying with the library StringFunctions
B4X:
Dim firstline As String
    Dim str As StringFunctions
    firstline=str.Trim(str.SplitGetWord(EditText2.text,Chr(13),1))
    Log (firstline)

Actually, you were pretty close to that working too, I think. 🍻

B4X:
 
 
Dim firstline As String = Trim(EditText2.text)
 
Do While firstline.BeginsWith(CRLF)
    firstline = firstline.SubString(1).Trim    'remove first character (CRLF) and any (new) leading spaces
Loop
 
firstline = firstline & CRLF    'add a sacrificial CRLF to search for, in case there isn't already one present
firstline = firstline.SubString2(0, firstline.IndexOf(CRLF)).Trim    'return up to CRLF, and remove trailing spaces
 
'''firstline=str.Trim(str.SplitGetWord(EditText2.text,Chr(13),1))

Log (firstline)

Again, not tested, but...
 
Upvote 0

emexes

Expert
Licensed User
Last variation, I promise. 🙃

B4X:
Sub GetFirstNonBlankLine(S As String) As String

    Do While S.Length <> 0 And Asc(S.CharAt(0)) <= 32    'trims leading NULs (ASCII 0) and all control codes ASCII 1..31 and spaces (ASCII 32)
        S = S.SubString(1)
    Loop
    
    Return S.SubString2(0, (S & CRLF).IndexOf(CRLF)).Trim
    
End Sub
 
Upvote 0

PaulMeuris

Active Member
Licensed User
Food for thought...
B4X:
    Private edt1 As EditText
    edt1.Initialize("")
    edt1.Text = " " & CRLF & " " & CRLF & "first line of text" & CRLF & "second line of text" & CRLF & "third line of text"
    Log(edt1.Text)
    ' make a list of lines
    Private lst As List = Regex.Split(CRLF,edt1.text)
    For i = 0 To lst.Size -1
        Private line As String = lst.Get(i)
        If line.Trim = "" Then
            Log("empty line")
        Else
            Log(line.Trim)
        End If
    Next
    ' replace the linefeed with a space (not the carriage return)
    Private no_lf As String = edt1.Text.Replace(Chr(10)," ").Trim
    Log(no_lf)
Log panel results:

first line of text
second line of text
third line of text
empty line
empty line
first line of text
second line of text
third line of text
first line of text second line of text third line of text
 
Upvote 0
Top