Results 1 to 6 of 6

Thread: compare a item in a collection

  1. #1

    Thread Starter
    Member
    Join Date
    Jul 2002
    Posts
    32

    compare a item in a collection

    I want to compare a string item in a collection with another string variable. I use collection.items.contains(string_var) to compare if the string_var is in the collection. But i need to compare case insensitive !! How can i do this ?

  2. #2
    Frenzied Member DevGrp's Avatar
    Join Date
    Nov 2001
    Location
    Charlotte, NC
    Posts
    1,256
    Can you post some more code?

    Anyway what kind of objects are in the collection? You can use a for each loop and compare.

    Did you try that?
    Dont gain the world and lose your soul

  3. #3

    Thread Starter
    Member
    Join Date
    Jul 2002
    Posts
    32
    The collection contains only string items. I know that i can use a for each loop, but is there no other way ?. As long as i have the items.contains function in a collection i really would like to use it.

    Example:

    VB Code:
    1. dim coll as new collection
    2.  
    3. coll.items.add("Part1")
    4. coll.items.add("PaRt2")
    5. coll.items.add("pArt3")
    6.  
    7. dim something as string = "part2"
    8.  
    9. if coll.items.contains(something) then messagebox.show("Found it")


    I hope this helps

  4. #4
    Frenzied Member DevGrp's Avatar
    Join Date
    Nov 2001
    Location
    Charlotte, NC
    Posts
    1,256
    I dont know what else to tell you, but if that is not working use the for each loop instead.

    In the for each loop you can assign a string variable each string in the collection, convert it to upper or lower case, convert the variable you are comparing then compare the results.

    Hope that helped.
    Dont gain the world and lose your soul

  5. #5

    Thread Starter
    Member
    Join Date
    Jul 2002
    Posts
    32
    Originally posted by DevGrp

    In the for each loop you can assign a string variable each string in the collection, convert it to upper or lower case, convert the variable you are comparing then compare the results.

    Hope that helped.
    I know that, but i am trying to find a more elegant way.

  6. #6
    Banished Cander's Avatar
    Join Date
    Dec 2000
    Location
    Why do you care?
    Posts
    6,913
    the compare method of the string object takes 3 parameters the first 2 are strings to compare, and the third is a boolean to see if you want to ignore case. It returns an integer that I guess represents if they match or not. Try using that.
    Stack Overflow
    See the features of Visual Studio 2010 and C# 4.0: The 10-4 show on Channel9

Posting Permissions

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



Click Here to Expand Forum to Full Width