-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,427
  • 7
  • 31
  • 59
vico
  • 15,367
  • 39
  • 141
  • 262

2 Answers2

-1

Use toLowerCase()

System.out.println(p.toLowerCase().indexOf("Hello".toLowerCase()));
System.out.println(p.toLowerCase().indexOf("hello".toLowerCase()));
Jordi Castilla
  • 25,851
  • 7
  • 65
  • 105
-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
  • 25,114
  • 4
  • 37
  • 52