B4J Question [Resolved] I need to select a text or a part of it by passing the search string

ivanomonti

Expert
Licensed User
Longtime User
I need to select a text or a part of it by passing the search string

example: I'm going to my mother's (textarea)
I'm looking for: mom (textfield)
I select: mom (textarea)

example 2: I'm going to mom's(textarea)
I'm looking for: I'm going mom (textfield)
I select: I'm going and mom (textarea)

my code:
    Dim value() As String = Regex.Split(" ",lb.Text)
    Dim s,e As Int
    Log(value(0))
    Log(value(value.Length-1))
    s=ta.Text.LastIndexOf(value(0))
    e=ta.Text.LastIndexOf(value(value.Length-1))
    ta.SetSelection(s,e)

Result

2023-03-16_100331.jpg


Thank 1000
 
Last edited:

Andrew (Digitwell)

Well-Known Member
Licensed User
Longtime User
Example 1.
You can use <string>.indexof to find the string that you are searching for.
You can then use <edittext>.setselection(start,length) to highlight the text.

B4X:
private et as edittext

et.text = "I'm going my mothers"
private s as string = "my"
private st as int = et.text.indexof(s)
if (st >= 0) then
    et.setselection(st,s.length)
end if

Example 2 is trickier as you are finding parts of the search string.
Are you searching for whole words or any part of the search string?

Edited: to add comment on example 2
 
Upvote 0

ivanomonti

Expert
Licensed User
Longtime User
I need to select a text or a part of it by passing the search string

example: I'm going to my mother's (textarea)
I'm looking for: mom (textfield)
I select: mom (textarea)

example 2: I'm going to mom's(textarea)
I'm looking for: I'm going mom (textfield)
I select: I'm going and mom (textarea)

my code:
    Dim value() As String = Regex.Split(" ",lb.Text)
    Dim s,e As Int
    Log(value(0))
    Log(value(value.Length-1))
    s=ta.Text.LastIndexOf(value(0))
    e=ta.Text.LastIndexOf(value(value.Length-1))
    ta.SetSelection(s,e)

Result

View attachment 140312

Thank 1000

Resolved:
    s=ta.Text.LastIndexOf(lb.Text)
    e=s+lb.Text.Length
    ta.SetSelection(s,e)

now i miss looking for repetitions example, find all the words "mom" :)
 
Upvote 0
Top