• Post Reply Bookmark Topic Watch Topic
  • New Topic

String.indexOf and String.equalsIgnoreCase  RSS feed

 
JayaSiji Gopal
Ranch Hand
Posts: 303
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a String "David Johnson"..i am expecting 10 results to come back.

My code -



when i give name = "David Johnson", i get 10 results.
if i give "david Johnson" or "David johnson", it does not return back 10 results.

Is there a way i could combine indexOf and equalsignoreCase in the above example?
 
JayaSiji Gopal
Ranch Hand
Posts: 303
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
what i meant to ask was can 'da' or 'Da' be made to behave the same way?
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You could use regular expressions; for example, the String.matches() method. Alternatively, you can convert the keyword and target both to the same case before comparing -- ie.,

if (flname[0]toLowerCase().indexOf(keyword.toLowerCase()) != -1) ...
 
Garrett OBrien
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I haven't done any speed tests, but when I need to do something like this, I assume it's much faster to do the conversion of case than regex's. I use regex's for pattern match, not case match.

Garrett
 
Pixy
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I guess the explanation till now is sufficient, still updating the above code as per Java5, following could also be used if you don't want to get the index of the text you are searching.

if (flname[0].toLowerCase().contains(keyword.toLowerCase())) {
 
Maneesh Godbole
Bartender
Posts: 11445
18
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"Pixy Pixy",
Welcome to the Ranch, we are happy to have you here
Looks like you missed out on our naming policy which states, the screen name should comprise a first name and last name, and should not be obviously fictitious.
You can adjust your screen name by clicking here
[ December 19, 2008: Message edited by: Maneesh Godbole ]
 
Campbell Ritchie
Marshal
Posts: 56578
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No, "Pixy" is no better. Change it again, please.
 
Campbell Ritchie
Marshal
Posts: 56578
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And why did you reopen a 5-month old thread? Please read this FAQ.
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!