Results 1 to 13 of 13
  1. #1
    Russell411 is offline Member
    Join Date
    Jun 2011
    Posts
    7
    Rep Power
    0

    Default 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:

    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");
    				}
    	}
    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?

    Thanks,

    Russell

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,651
    Blog Entries
    7
    Rep Power
    21

    Default

    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,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  3. #3
    Russell411 is offline Member
    Join Date
    Jun 2011
    Posts
    7
    Rep Power
    0

    Default

    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

  4. #4
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,651
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by Russell411 View Post
    Hi Jos,
    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.
    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,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  5. #5
    Russell411 is offline Member
    Join Date
    Jun 2011
    Posts
    7
    Rep Power
    0

    Default

    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

  6. #6
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,422
    Rep Power
    20

    Default

    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.
    A LinkedHashSet maintains insertion order.

    db

  7. #7
    Russell411 is offline Member
    Join Date
    Jun 2011
    Posts
    7
    Rep Power
    0

    Default

    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,

    Russell
    Last edited by Russell411; 06-27-2011 at 11:38 PM.

  8. #8
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,562
    Rep Power
    25

    Default

    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.

  9. #9
    Russell411 is offline Member
    Join Date
    Jun 2011
    Posts
    7
    Rep Power
    0

    Default

    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

  10. #10
    Russell411 is offline Member
    Join Date
    Jun 2011
    Posts
    7
    Rep Power
    0

    Default

    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

  11. #11
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,562
    Rep Power
    25

    Default

    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.

  12. #12
    Russell411 is offline Member
    Join Date
    Jun 2011
    Posts
    7
    Rep Power
    0

    Default

    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:
    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");
    					}
    				}
    			}
    Which also doesn't work. Jos hadn't said HashMap (maybe implied it)... So, I'm off to read about HashMaps.

    Thanks for another couple puzzle pieces.

    Russell

  13. #13
    kdmalviyan is offline Member
    Join Date
    Nov 2012
    Posts
    1
    Rep Power
    0

    Smile Re: How to use Contains - without case sensitivity?

    Quote Originally Posted by Russell411 View Post
    Hi Jos,


    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
    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

  1. Using a switch/case with my if statements
    By coding in forum New To Java
    Replies: 2
    Last Post: 03-07-2011, 08:01 AM
  2. Switch Case statement
    By seanfmglobal in forum New To Java
    Replies: 7
    Last Post: 02-15-2011, 01:18 PM
  3. if else changes to switch-case?
    By noobinoo in forum New To Java
    Replies: 1
    Last Post: 04-23-2010, 05:56 PM
  4. In Case You Want Colors!
    By angryboy in forum Forum Lobby
    Replies: 0
    Last Post: 08-29-2009, 10:06 PM
  5. case statement
    By skiing in forum New To Java
    Replies: 5
    Last Post: 05-07-2009, 12:13 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •