0

I am having 2 string and wanted to compare ignoring the case.

Ex: 
s1.contains(s2.toLowerCase()) in this case the problem is s1 might be in upper case. 
s1.toLowerCase().contains(s2) in this case the problem is s2  might be in upper case. 

The only option is i need to change both the string to lowercase to find the contains.

ex: s1.toLowerCase().contains(s2.toLowerCase())

Is there is any utility or string method instead of using this approach as String is immutable.

Thanks.

2 Answers2

1

You could use StringUtils from apache.

StringUtils.containsIgnoreCase(str, searchStr)

https://commons.apache.org/proper/commons-lang/apidocs/org/apache/commons/lang3/StringUtils.html

NeXT405
  • 55
  • 5
0

You should go with your s1.toLowerCase().contains(s2.toLowerCase()). Any utility method will only add unnecessary overhead and complexity

  /* commenting the answer, so that the comments don't become irrelevant.

Try using String class method equalsIgnoreCase(),
     and do whatever you want.

          // for example; suppose two strings, with same characters
          String str1 = "jangbahadUr";
          String str2 = "JanGbahadUr";

          if (str1.equalsIgnoreCase(str2)) {
              System.out.println("equal"); // here do your own logic.
          } else {
              System.out.println("not equal");
          }

    */
Jabongg
  • 2,099
  • 2
  • 15
  • 32