ArrayList contains case sensitivity

前端 未结 19 1770
故里飘歌
故里飘歌 2020-11-27 17:14

I am currently using the contains method belonging to the ArrayList class for making a search. Is there a way to make this search case insensitive in java? I found that in C

19条回答
  •  执笔经年
    2020-11-27 17:47

    Looking at the Java API, there is no such method for contains.

    But you could do at least two things:

    1. Override the equals method in your ArrayList object with your own, or equalsIgnoreCase(str)
    2. Write your own contains method, which should iterate through your ArrayList entities, and do a manual check.

      ArrayList list = new ArrayList();
      ...
      containsIgnoreCase("a", list);
      
      public boolean containsIgnoreCase(String str, ArrayList list){
          for(String i : list){
              if(i.equalsIgnoreCase(str))
                  return true;
          }
          return false;
      }
      

提交回复
热议问题