retrievind index number of a certain element into a list

Post your questions and help other users.

Moderator: Martin

Post Reply
User avatar
tsolignani
Posts: 187
Joined: 12 Jan 2019 11:53
Location: Vignola, Mo, Italy
Contact:

retrievind index number of a certain element into a list

Post by tsolignani » 03 Mar 2020 10:39

Good morning everyone.

I have a list. Say [ABC, CAA, CXZ]

With the contains function I can see that the list, in one of its item, contains a certain string, say "X"

Now, I would like to extract the whole item or element which matched the search, say "CXZ.

So I guess I have to obtain the index (which, in my example, would be 2, being the third item) of the element and then use the function getElement.

How do I retrieve that?

Thank you.

User avatar
Desmanto
Posts: 2709
Joined: 21 Jul 2017 17:50

Re: retrievind index number of a certain element into a list

Post by Desmanto » 03 Mar 2020 11:00

Usual loop should be enough.

Code: Select all

match = "";
s = "X"; //search string
list = newList("ABC", "CAA", "CXZ");
for(i in list)
{
  if(contains(i, s))
  {
    match = i;
    break;
  }
}
Index of Automagic useful thread List of my other useful posts (and others')
Xiaomi Redmi Note 5 (whyred), AOSP Extended v6.7 build 20200310 Official, Android Pie 9.0, Rooted.

User avatar
tsolignani
Posts: 187
Joined: 12 Jan 2019 11:53
Location: Vignola, Mo, Italy
Contact:

Re: retrievind index number of a certain element into a list

Post by tsolignani » 05 Mar 2020 15:34

It works like a charm!

I kept it as an example for future analogue needs.

Thank you so much.

Post Reply