ArrayList contains case sensitivity

前端 未结 19 1792
故里飘歌
故里飘歌 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:53

    Don't reinvent the wheel. Use well tested APIs. For your purpose use Apache Commons StringUtils.

    From the Javadoc: Compares given string to a CharSequences vararg of searchStrings returning true if the string is equal to any of the searchStrings, ignoring case.

    import org.apache.commons.lang3.StringUtils;
    ...
    StringUtils.equalsAnyIgnoreCase(null, (CharSequence[]) null) = false
    StringUtils.equalsAnyIgnoreCase(null, null, null)    = true
    StringUtils.equalsAnyIgnoreCase(null, "abc", "def")  = false
    StringUtils.equalsAnyIgnoreCase("abc", null, "def")  = false
    StringUtils.equalsAnyIgnoreCase("abc", "abc", "def") = true
    StringUtils.equalsAnyIgnoreCase("abc", "ABC", "DEF") = true
    

提交回复
热议问题