4

While reading the line as a string from a file and string.contains("someexamplestring") will return the output of the case sensitive string.

If there is "someExampleString" in line, it's not returning.

How to identify a string in a case-insensitive manner?

madhan kumar
  • 1,560
  • 2
  • 26
  • 36
Jagadeeswar
  • 311
  • 2
  • 5
  • 14
  • 1
    Use a case insensitive regex, e.g. `string.matches("(?i).*someexamplestring.*")` – Thomas Sep 23 '16 at 11:51
  • Regarding all the answers suggesting toLowerCase/toUpperCase - be careful if you go outside of ASCII space. There are some languages where going lower to upper and back (or other way around) is not consistent. Turkish with its dotless 'i' comes to mind https://en.wikipedia.org/wiki/Dotted_and_dotless_I – Artur Biesiadowski Sep 23 '16 at 11:57

6 Answers6

5

Actually, this is a duplicate of How to check if a String contains another String in a case insensitive manner in Java?


If you've simpler requirements and are dealing with English letters only, you can follow the below answer.

You should do string.toLowerCase().contains("someExampleString".toLowerCase());.

Read more about public String toLowerCase() from Java SE Documentation.

Also, as hinted by Artur Biesiadowski in the comment section of the question, re-iterating it here :

Regarding all the answers suggesting toLowerCase/toUpperCase - be careful if you go outside of ASCII space. There are some languages where going lower to upper and back (or other way around) is not consistent. Turkish with its dotless 'i' comes to mind : Dotted and dotless I


Also, to make it safer, you may use another method toLowerCase(Locale.English) and override the locale to English always. But, the limitation being you are not internationalized any longer.

string.toLowerCase(Locale.English).contains("someExampleString".toLowerCase(Locale.English));
Community
  • 1
  • 1
Am_I_Helpful
  • 18,735
  • 7
  • 49
  • 73
2

You can use regex:

str.matches("(?i).*someexamplestring.*")
Evgeniy Dorofeev
  • 133,369
  • 30
  • 199
  • 275
1

Use String.toLowerCase(str) method on your input String first and then apply String.contains(str) method.

Blue
  • 22,608
  • 7
  • 62
  • 92
cagrias
  • 1,847
  • 3
  • 13
  • 24
1

To achieve case insensitive operations we need to make both strings in one format either upper case or lower case .toLowerCase or 'toUpperCase'methods will be helpful when you want to do contains operation

There are two variants in toLowerCase.

somestring.toLowerCase();
somestring.toLowerCase(Locale somelocale)

Example

String case1 = "StackOverFlow";
String case2 = "STACKOverfloW";
bool isSame ;
isSame = case1.toLowerCase().Contains(case2.toLowerCase());

Note that equalsIgnoreCase() will be helpful when you want to compare two strings

Venkat
  • 2,549
  • 2
  • 28
  • 61
  • Please add your useful commands before downvote , so that I can know the correct answer. It will be helpful for learning – Venkat Sep 23 '16 at 12:12
0

You can toLowerCase() your String and then do contains with the lower cased version of what you want to match.

NilsH
  • 13,705
  • 4
  • 41
  • 59
0

You should convert both strings to lower case or upper case. Then do operation.

    Str.toUpperCase().contains("someexamplestring".toUpperCase());

                              or

    Str.toLowerCase().contains("someexamplestring".toLowerCase());
mahesh chouhan
  • 63
  • 1
  • 10