-1

I need to get substring position in string while ignoring substring case sensitive. That means I hould like to get the same output result in case of:

String p = "aaaHelloddd";

System.out.println(p.indexOf("Hello"));
System.out.println(p.indexOf("hello"));

How to achieve that?

Madhawa Priyashantha
  • 9,633
  • 7
  • 33
  • 60
vico
  • 17,051
  • 45
  • 159
  • 315

2 Answers2

-1

Use toLowerCase()

System.out.println(p.toLowerCase().indexOf("Hello".toLowerCase()));
System.out.println(p.toLowerCase().indexOf("hello".toLowerCase()));
Jordi Castilla
  • 26,609
  • 8
  • 70
  • 109
-3

You can simply change to uppercase and check for the uppercase substring.

String p = "aaaHelloddd";

System.out.println(p.toUpperCase().indexOf("HELLO"));

If Hello is a parameter simply make it uppercase.

String p = "aaaHelloddd";

System.out.println(p.toUpperCase().indexOf("hello".toUpperCase()));
System.out.println(p.toUpperCase().indexOf("Hello".toUpperCase()));

both prints the same results.

Davide Lorenzo MARINO
  • 26,420
  • 4
  • 39
  • 56