Finding a string in a String-Array element element

How to search for specific text inside an element of a string-array element? The following is an example xml file. The name of the string array is android. I have some elements inside an array of strings. Now I want to search for the word "software." Please tell me how to do this?

<?xml version="1.0" encoding="utf-8"?><resources> <string-array name="android"> <item>Android is a software stack for mobile devices that includes an operating system, middleware and key applications.</item> <item>Google Inc. purchased the initial developer of the software, Android Inc., in 2005..</item> </string-array> 

+6
source share
2 answers

I assume you want to do this in code. There is nothing in api to perform text matching over the entire String array; you need to do this one element at a time:

 String[] androidStrings = getResources().getStringArray(R.array.android); for (String s : androidStrings) { int i = s.indexOf("software"); if (i >= 0) { // found a match to "software" at offset i } } 

Of course, you could use Matcher and Pattern, or you could iterate over an array with an index if you wanted to know the position in the match array. But this is a general approach.

+18
source

This method has the best characteristics:

 String[] androidStrings = getResources().getStringArray(R.array.android); if (Arrays.asList(androidStrings).contains("software") { // found a match to "software" } 

Arrays.asList().contains() faster than using a for loop.

+19
source

Source: https://habr.com/ru/post/891510/


All Articles