Android Question Select a string of text from the EditText

Matteo Granatiero

Active Member
Licensed User
Longtime User
Example: Jeans - Season Event Color.
I want to copy from - on (so only color, season and event)
I checked another forum but I did not understand much, so I was wondering if you could give me the code that's already right for me, thanks in advance
 

panagiotisden2

Active Member
Licensed User
Longtime User
B4X:
Dim trimmed_text As String

    Dim indexvalue As Int
    indexvalue=EditText1.text.IndexOf("-")'replace EditText1 with your edittext/string
    If indexvalue=-1 Then
        Log("text does not contain a -")
        Return
    End If
    trimmed_text=EditText1.text.SubString(indexvalue+1)
    If trimmed_text.StartsWith(" ") Then
        trimmed_text=trimmed_text.SubString(1)
    End If
    Log(trimmed_text)
 
Last edited:
Upvote 0

mangojack

Expert
Licensed User
Longtime User
Using a code snippet from @klaus 's Edit Text Utils ...

B4X:
EditText1.Text ="Jeans - Season Event Color"
Dim sel1 As Int = EditText1.Text.IndexOf("-")
Dim sel2 As Int = EditText1.Text.Trim.Length
setSelection(EditText1, sel1 +2, sel2)
EditText1.RequestFocus
  
'Selects the text between the two indexes.
Sub setSelection(edt As EditText, StartIndex As Int, EndIndex As Int)
    Dim jo = edt As JavaObject
    jo.RunMethod("setSelection", Array As Object(StartIndex, EndIndex))
End Sub
 
Last edited:
Upvote 0
Top