• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Knute Snortum
  • Bear Bibeault
Saloon Keepers:
  • Ron McLeod
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Ganesh Patekar
Bartenders:
  • Frits Walraven
  • Carey Brown
  • Tim Holloway

String.indexOf and String.equalsIgnoreCase

 
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?
 
author and iconoclast
Posts: 24203
43
Mac OS X Eclipse IDE Chrome
  • 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) ...
 
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
 
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())) {
 
Bartender
Posts: 11445
18
Android Google Web Toolkit Mac Eclipse IDE Ubuntu Java
  • 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 ]
 
Marshal
Posts: 64616
225
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No, "Pixy" is no better. Change it again, please.
 
Campbell Ritchie
Marshal
Posts: 64616
225
  • 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.
 
This. Exactly this. This is what my therapist has been talking about. And now with a tiny ad:
how do I do my own kindle-like thing - without amazon
https://coderanch.com/t/711421/engineering/kindle-amazon
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!