Results 1 to 13 of 13
- 06-27-2011, 08:27 PM #1
Member
- Join Date
- Jun 2011
- Posts
- 7
- Rep Power
- 0
How to use Contains - without case sensitivity?
Hello everyone,
I just registered here now that I'm working on my first "real world" (not book driven) application and I'm stuck with case sensitivity:
It is clear that Names.contains() is case sensitive, but I can't find an "ignore case" or "case insensitive" version - though it seems this should be common enough to be included somewhere. Can you nudge me in the right direction?Java Code:// Names holds the names I want to search for. Set Names = new HashSet<String>(); // (Names is populated by a SQL result set) // wordlist contains the words from a page of text that I have pulled from a file: List<String> wordlist = stringToArrayList(pagetext, " "); for (String word: wordlist) { System.out.print(word + "\t"); if (Names.contains(word)) { System.out.print("Name Found.\n"); } }
Thanks,
Russell
- 06-27-2011, 08:44 PM #2
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,375
- Blog Entries
- 7
- Rep Power
- 17
There are a couple of possibilities here:
1) if you're willing to sacrifice a bit of performance you can use a TreeSet; this object takes a Comparator<String> where you can use the String.compareToIgnoreCase( ... ) method.
2) wrap all your Strings in small objects that do the same as outlined in 1)
3) store only upper/lowercase Strings in your Map and only check if it contains the upper/lowercase version of your String.
kind regards,
JosWhen people rob a bank they get a penalty; when banks rob people they get a bonus.
- 06-27-2011, 08:53 PM #3
Member
- Join Date
- Jun 2011
- Posts
- 7
- Rep Power
- 0
Hi Jos,
Thanks for the quick reply... But I've gotta say, "Yuck!"
1. I'm scanning tens of thousands of files, so speed is a concern - more concerning is that I'm not clear on "Comparator<String>" - going to have to read. This seems like the best solution, so I've gotta go figure out how to do it.
2. Another technique I'm not familiar with - I have no idea how I'd do this.
3. Hmmm... I can probably do this - but I don't like the result: having no case in my dataset any longer - Maybe I can make a copy of each dataset to run together and use an iterator to retrieve the "non-case-corrected" version.
Seems like such a trivial task has become a lot of study.
Thanks,
Russell
- 06-27-2011, 09:04 PM #4
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,375
- Blog Entries
- 7
- Rep Power
- 17
w.r.t.
1) Writing an implementation of the Comparator<String> interface is easy: you have to implement only two methods: compare(String o1, String o2) and an equals(Object o) method,
2) Forget about this: it is home brewn and can take a lot of (small) object, one for each String
3) You can have case in your dataset, just store the upper/lowercase version of your Strings (fetched from your database) in your Map.
Personally I'd go for option 3)
kind regards,
JosWhen people rob a bank they get a penalty; when banks rob people they get a bonus.
- 06-27-2011, 09:08 PM #5
Member
- Join Date
- Jun 2011
- Posts
- 7
- Rep Power
- 0
Okay,
A few minutes of homework later and it seems that I could simply change my declaration of the set to:
Set LastNames = new TreeSet(String.CASE_INSENSITIVE_ORDER);
But that gives a warning that I don't understand (in Eclipse): "The method equalsIgnoreCase(String) is undefined for the type Set"
And it also looks like adding things to a TreeSet takes them out of their original order and puts them in alphabetic order - which won't work for me either - since I care about the order in which the data appears in the file.
Still working on it,
Russell
- 06-27-2011, 10:02 PM #6
A LinkedHashSet maintains insertion order.And it also looks like adding things to a TreeSet takes them out of their original order and puts them in alphabetic order - which won't work for me either - since I care about the order in which the data appears in the file.
db
- 06-27-2011, 11:36 PM #7
Member
- Join Date
- Jun 2011
- Posts
- 7
- Rep Power
- 0
Hi Jos,
If you'd personally go with option 3, that's what I'm going to do. :-) What Map are you talking about?
After I've identified the names in the dataset, of course, I'll be using them - and I'd like their original case to still be there. (I'm really struggling with collections, it seems - Wondering: maybe I should just go back to arrays and do a ton of iteration).
Am I to create a "Set" or Objects that contain both cases? I think that's what you mean, so I'm trying to figure out how to do that.
Can you point me in the direction I need to be going?
Thanks,
RussellLast edited by Russell411; 06-27-2011 at 11:38 PM.
- 06-27-2011, 11:42 PM #8
Is it possible to have names in the Names collection that would be the same if they were the same case?
For example: sam and Sam
If that can never happen, you could wrap your String in your own class and add an equals method that uses equalsIgnoreCase.
- 06-28-2011, 12:52 AM #9
Member
- Join Date
- Jun 2011
- Posts
- 7
- Rep Power
- 0
Jos: If you can point me a place to learn about how to do this, that'd be great.
Norm: I could get sam and Sam from my textfiles - they're human written, so anything's possible.
I'm so frustrated - I've been monkeying with this for hours now.
I'm so noob, I don't even know how to do the things you guys are suggesting - or even where to look. I'm feeling rather defeated, before I've even gotten anywhere. How can it be so difficult?
Sadly,
Russell
- 06-28-2011, 01:07 AM #10
Member
- Join Date
- Jun 2011
- Posts
- 7
- Rep Power
- 0
A light bulb just went off in my head. Basically, I'm trying to shortcut a loop within a loop, running through two different sets of data - maybe I should just loop through both sets of data?
Working on that concept now.
Russell
- 06-28-2011, 01:09 AM #11
If its possible to have sam and Sam. Then case is important for you.
Would saM be a match for either of these or both???
Seems you need some constraints on the possibilities. JosAH #3 is the only way to go.
Then you need an associated HashMap with the Uppercase name as key and the value being an ArrayList of all the different values that have different cases. Match first on the Uppercase and then search the arraylist for the case sensitive match.
- 06-28-2011, 01:22 AM #12
Member
- Join Date
- Jun 2011
- Posts
- 7
- Rep Power
- 0
Hi Norm,
My constraint concept was "Ignore Case - they're all the same"... but retain the case so I can output it the way I received it.
I moved onto this ugly attempt:
Which also doesn't work. Jos hadn't said HashMap (maybe implied it)... So, I'm off to read about HashMaps.Java Code:Set FirstNames = new TreeSet<String>(String.CASE_INSENSITIVE_ORDER); // Populate LastNames from SQL Database. Iterator itr = FirstNames.iterator(); while(itr.hasNext()) { for (String word: wordlist) { System.out.print(word + "\t"); if (word.equalsIgnoreCase((String) itr.next())) { System.out.print("Name\n"); } else { System.out.print("\n"); } } }
Thanks for another couple puzzle pieces.
Russell
- 11-07-2012, 02:53 PM #13
Member
- Join Date
- Nov 2012
- Posts
- 1
- Rep Power
- 0
Re: How to use Contains - without case sensitivity?
org.apache.commons.lang.StringUtils.containsIgnore Case("AbBaCca", "bac");
use commons api provided by apache.
I also faced this problem and i solved this using the above api.
Now i think it may be proved useful information for you.
Thanks for the quick reply... But I've gotta say, "Yuck!"
Similar Threads
-
Using a switch/case with my if statements
By coding in forum New To JavaReplies: 2Last Post: 03-07-2011, 08:01 AM -
Switch Case statement
By seanfmglobal in forum New To JavaReplies: 7Last Post: 02-15-2011, 01:18 PM -
if else changes to switch-case?
By noobinoo in forum New To JavaReplies: 1Last Post: 04-23-2010, 05:56 PM -
In Case You Want Colors!
By angryboy in forum Forum LobbyReplies: 0Last Post: 08-29-2009, 10:06 PM -
case statement
By skiing in forum New To JavaReplies: 5Last Post: 05-07-2009, 12:13 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks