使用 .contains 方法忽略大小写的选项? [英] Option to ignore case with .contains method?
问题描述
是否可以使用 .contains()
方法忽略大小写?
Is there an option to ignore case with .contains()
method?
我有一个 DVD 对象的 ArrayList
.每个 DVD 对象都有几个元素,其中之一是标题.我有一种搜索特定标题的方法.它有效,但我希望它不区分大小写.
I have an ArrayList
of DVD object. Each DVD object has a few elements, one of them is a title. And I have a method that searches for a specific title. It works, but I'd like it to be case insensitive.
推荐答案
我猜你的意思是在搜索字符串时忽略大小写?
I'm guessing you mean ignoring case when searching in a string?
我不知道,但您可以尝试将要搜索的字符串转换为小写或大写,然后进行搜索.
I don't know any, but you could try to convert the string to search into either to lower or to upper case, then search.
// s is the String to search into, and seq the sequence you are searching for.
bool doesContain = s.toLowerCase().contains(seq);
正如 Ryan Schipper 所建议的,您也可以(并且可能会更好)执行 seq.toLowerCase(),具体取决于您的情况.
As Ryan Schipper suggested, you can also (and probably would be better off) do seq.toLowerCase(), depending on your situation.
这篇关于使用 .contains 方法忽略大小写的选项?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!