-4

i am looking for a piece of code which can search for a string in a string.

String mainSTR = "It is Wednesday";

So i need a code that will return true if i search

"Day" or "day" or "DAY" in mainSTR.

I tried methods like

mainSTR.contains("")

and

mainSTR.IndexOf("") > -1

are not working for me

Thanks :)

  • 3
    You want to check for `Day`, then why are you passing empty string in `contains`? – Rohit Jain Jul 23 '13 at 18:50
  • Are you only looking for "Day", "day", and "DAY" or are things like "dAY" and "DaY" acceptable? – bengoesboom Jul 23 '13 at 18:51
  • Thanks but contains not working for me i tried those methods i mentioned – Lovish Sindhwani Jul 23 '13 at 19:12
  • 2
    @loveeSindh: Show your actual code with `contains()` which does not work. If you do it the way posted in all of the answers, it will work. You can use the [**edit**](http://stackoverflow.com/posts/17818593/edit) button below your question. – jlordo Jul 23 '13 at 19:13
  • @loveeSindh please stop saying "not working" and instead try to explain **exactly** how it fails. – Blorgbeard Jul 23 '13 at 19:34

7 Answers7

2

Well one can't expect such question from and Android Developer. Anyhow, for finding any word in String, you can use Java method

String mainSTR = "It is Wednesday";
String findThis = "DaY";

if(mainSTR.toLowerCase().contains(findThis.toLowerCase()))
{
   System.out.println("true: Day found");
}
else
   System.out.println("false: Day not found");
Umer Farooq
  • 7,356
  • 7
  • 42
  • 67
0

What you can use here is a one way - canonicalising function. toLowerCase (or toUpperCase) is one such helper.

mainSTR.toLowerCase().contains("day");
Joel
  • 4,732
  • 9
  • 39
  • 54
0
mainSTR.toUpperCase().contains("day".toUpperCase());
GriffeyDog
  • 8,186
  • 3
  • 22
  • 34
0
String days = "day";//or "Day" or "DAY"
mainSTR.toLowerCase().contains(days.toLowerCase());

Ps: for such comparison, you should prefer toLowerCase(Locale.ENGLISH) or toUpperCase(Locale.ENGLISH) which give you locale insensitive strings.

And here is what javaDoc has to say about it.

Note: This method is locale sensitive, and may produce unexpected results if used for strings that are intended to be interpreted locale independently. Examples are programming language identifiers, protocol keys, and HTML tags. For instance, "TITLE".toLowerCase() in a Turkish locale returns "t?tle", where '?' is the LATIN SMALL LETTER DOTLESS I character. To obtain correct results for locale insensitive strings, use toLowerCase(Locale.ENGLISH).

Ankit
  • 6,554
  • 6
  • 49
  • 71
  • JavaDoc says `toLowerCase()` uses the default locale, the same with `toUpperCase()`. Can you provide a source of what you are saying? – jlordo Jul 23 '13 at 19:05
  • @jlordo http://javapapers.com/core-java/javas-tolowercase-has-got-a-surprise-for-you/ – Ankit Jul 23 '13 at 19:14
  • @ay89: it says "_It performs the case conversion with respect to your Locale_", that is true. But `toUpperCase()` does the same thing, so there is no difference. (P.S. I am not the downvoter) – jlordo Jul 23 '13 at 19:18
  • @jlordo also read the docs again, there is something more in the description. – Ankit Jul 23 '13 at 19:19
  • @ay89: yes, it says the same thing in docs of `toUpperCase()`. So there is not a preferable one, both are the same. – jlordo Jul 23 '13 at 19:22
0

Please try following:

mainSTR.toUpperCase().contains(day.toUpperCase());

Cheers !!

Sachin Thapa
  • 3,559
  • 4
  • 24
  • 42
0

If mainSTR is a really long String, this approach may be more efficient:

String mainSTR = "It is Wednesday";
String toSearch = "Day";
boolean found = Pattern.compile(toSearch, Pattern.CASE_INSENSITIVE).matcher(mainSTR).find();

the last line is equivalent to

boolean found = Pattern.compile("(?i)" + toSearch).matcher(mainSTR).find();

But beware that toSearch may not contain regex meta characters if you do it this way.

jlordo
  • 37,490
  • 6
  • 58
  • 83
  • if mainSTR = "+91111111189"; toSearch = "89" will it still work in that case because i just got an error when i used this code. – Lovish Sindhwani Jul 23 '13 at 19:11
  • @loveeSindh: it would work without a problem (even tested it to be sure). What kind of error did you get?? – jlordo Jul 23 '13 at 19:20
  • but you warned me about toSearch. so even mainSTR cant contain regex meta character. so any way to ignore it? – Lovish Sindhwani Jul 23 '13 at 19:22
  • @loveeSindh: No, `toSearch` can't contain regex metacharacters, `mainSTR` can. To avoid it, you can use `Pattern.quote()` but it won't be case insensitive anymore. (I edited my previous comment). – jlordo Jul 23 '13 at 19:24
  • Ok now it is not getting any kind of error but it is now working in the case if i have number in mainSTR like i mentioned in first comment. the found remains false only. – Lovish Sindhwani Jul 23 '13 at 19:30
  • @loveeSindh: I can not help you further if you don't post your actual code (in your question above). – jlordo Jul 23 '13 at 19:42
0

Check this out

   String mainStr="It is Wednesday";
   System.out.println(mainStr.toLowerCase().contains("wed".toLowerCase()));
Salil Dua
  • 136
  • 1
  • 1
  • 8