ArrayList contains case sensitivity

前端 未结 19 1765
故里飘歌
故里飘歌 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 18:08

    If you don't want to create a new function, you can try this method:

    List myList = new ArrayList(); //The list you're checking
    String wordToFind = "Test"; //or scan.nextLine() or whatever you're checking
    
    //If your list has mix of uppercase and lowercase letters letters create a copy...
    List copyList = new ArrayList();
    for(String copyWord : myList){
       copyList.add(copyWord.toLowerCase());
    }
    
    for(String myWord : copyList){
       if(copyList.contains(wordToFind.toLowerCase()){
          //do something
       }
    }
    

提交回复
热议问题