Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to make indexof case insensitive in java

Tags:

java

indexof

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.

like image 661
javalearner Avatar asked Sep 19 '14 23:09

javalearner


2 Answers

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.

like image 63
Crenguta S Avatar answered Oct 06 '22 11:10

Crenguta S


One common solution:

yourString.toLowerCase().indexOf("foo");
like image 26
Eng.Fouad Avatar answered Oct 06 '22 11:10

Eng.Fouad