Searching for a string in a String-Array item element

后端 未结 2 881
南方客
南方客 2020-12-29 15:11

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

相关标签:
2条回答
  • 2020-12-29 15:41

    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.

    0 讨论(0)
  • 2020-12-29 15:56

    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.

    0 讨论(0)
提交回复
热议问题