Is there any way to know if an arraylist contains a piece of text?

前端 未结 5 1472
执笔经年
执笔经年 2021-01-15 11:35

I have an arraylist with several items. Let\'s say they are: \"DARK BROWN\", \"BLUE\", \"GREEN\",....

Is there any way to look for if there\'s the string \"DARK\" in

5条回答
  •  情书的邮戳
    2021-01-15 12:24

    Here is an example of a function you could use with getting each item. The speed of this is not really an increase. Due to this being an arraylist there is not really a good way to do this. There are better data structures for searching for parts of a string.

        public class RegionMatchesDemo {
    public static void main(String[] args) {
        String searchMe = "Green Eggs and Ham";
        String findMe = "Eggs";
        int searchMeLength = searchMe.length();
        int findMeLength = findMe.length();
        boolean foundIt = false;
        for (int i = 0; 
             i <= (searchMeLength - findMeLength);
             i++) {
           if (searchMe.regionMatches(i, findMe, 0, findMeLength)) {
              foundIt = true;
              System.out.println(searchMe.substring(i, i + findMeLength));
              break;
           }
        }
        if (!foundIt)
            System.out.println("No match found.");
      }
    }
    

提交回复
热议问题