简体   繁体   中英

how to make indexof case insensitive in java

I have a simple question. How do I make indexof case insensitive in java. 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.

    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.

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.

一个常见的解决方

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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