Results 1 to 7 of 7

Thread: Case Insenstive search an array

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Jun 2003
    Posts
    89

    Case Insenstive search an array

    Hi,
    I got this string array and I have to find out whether a value is inside that array or not. The simplest solution is to use the static method .indexOf()

    Array.indexOf(new string[] {"one","two","three"},"two")

    Unfortunately, the search seems to be case sensitive, which I don't want. For instance, the value "Two" wont' be found:
    Array.indexOf(new string[] {"one","two","three"},"Two")

    I need either to make all the elements lowercase, or to perform a search that is case insenstive. How to do this?

    Thanks in advance, bye
    - mo! I said MOOOOOOO!!
    - ...yep, that's a cow, alright.

  2. #2
    Fanatic Member brown monkey's Avatar
    Join Date
    Jun 2004
    Location
    Cebu
    Posts
    552
    i don't know if this is the best way but you can try
    PHP Code:
    "Two".ToLower() 

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Jun 2003
    Posts
    89
    Thanks for replying, but I probably didnt explained it well:

    I dont know if the array elements are lower or upper case. In this case:

    Array.indexOf(new string[] {"OnE","tWo","ThRee"},"Two".toLower())

    my "two" element won't be still found
    - mo! I said MOOOOOOO!!
    - ...yep, that's a cow, alright.

  4. #4
    Fanatic Member brown monkey's Avatar
    Join Date
    Jun 2004
    Location
    Cebu
    Posts
    552
    i'm no expert but what about looping to the array?
    PHP Code:
             string[] s={"OnE","tWo","ThRee"};
             for(
    int i=0;i<s.Length;i++) s[i]=s[i].ToLower();
             
    MessageBox.Show(Array.IndexOf(s,"two".ToLower()).ToString()); 

  5. #5

    Thread Starter
    Lively Member
    Join Date
    Jun 2003
    Posts
    89
    thanks brown monkey
    I think there's no way other than iterate thru the elements then.
    byee
    - mo! I said MOOOOOOO!!
    - ...yep, that's a cow, alright.

  6. #6
    Addicted Member
    Join Date
    Feb 2002
    Location
    closed
    Posts
    196
    Should do a case-insensitive find for you

    PHP Code:
    int i = Array.BinarySearch(
    new  
    string[]{"one""two""three"},"tWo",System.Collections.CaseInsensitiveComparer.Default); 

  7. #7

    Thread Starter
    Lively Member
    Join Date
    Jun 2003
    Posts
    89
    nice! it's working good
    thanks!
    - mo! I said MOOOOOOO!!
    - ...yep, that's a cow, alright.

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