Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Searching for a string in a String-Array item element

Tags:

android

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

<?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>

like image 688
user730009 Avatar asked Jun 28 '11 03:06

user730009


2 Answers

This method has better performances:

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

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

like image 163
Gregoire Barret Avatar answered Nov 03 '22 20:11

Gregoire Barret


I assume that you want to do this in code. There's nothing in the api to do text matching on an entire String array; you need to do it 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 a Matcher and Pattern, or you could iterate through the array with an index if you wanted to know the position in the array of a match. But this is the general approach.

like image 32
Ted Hopp Avatar answered Nov 03 '22 21:11

Ted Hopp