简体   繁体   English

如何在java中使indexof case不敏感

[英]how to make indexof case insensitive in java

I have a simple question. 我有一个简单的问题。 How do I make indexof case insensitive in java. 如何在java中使indexof case不敏感。 This question has been already answered in some forum but I didn't understand the answer. 这个问题已经在一些论坛得到了回答,但我不明白答案。

Say for eg.I have a string s = Phone(Conf) I want to pull the record that has (Conf) like this but the users are entering CONF or conf or Conf etc. So my program should be able to pull the record if it finds the word conf in any case. 比如说。我有一个字符串s = Phone(Conf)我想拉这样的记录(Conf),但是用户正在输入CONF或conf或Conf等。所以我的程序应该能够拉出记录在任何情况下它都会找到conf这个词。

    if(s.indexOf("(")>-1&& s.indexOf("Conf")>-4 && s.lastIndexOf(")")>-1)
 {
    String s1=s.substring(s.indexOf("(Conf"),s.lastIndexOf(")")+1);
   }

can someone explain me pls? 有人可以解释一下吗? The above code pulls it if the string is (Conf) only. 如果字符串仅为(Conf),则上面的代码将其拉出。

The safest way to do it would be: 最安全的方法是:

content.toLowerCase().indexOf(searchTerm.toLowerCase()) , this way you cover 2 possible edge cases when the content may be in lower case and the search term would be in upper case or both would be upper case. content.toLowerCase().indexOf(searchTerm.toLowerCase()) ,这样你可以覆盖2个可能的边缘情况,当内容可能是小写的,搜索项是大写的,或者两者都是大写。

一个常见的解决方

yourString.toLowerCase().indexOf("foo");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM