|
-
Aug 4th, 2004, 03:46 PM
#1
Thread Starter
Lively Member
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.
-
Aug 4th, 2004, 09:09 PM
#2
Fanatic Member
i don't know if this is the best way but you can try
PHP Code:
"Two".ToLower()
-
Aug 5th, 2004, 02:39 AM
#3
Thread Starter
Lively Member
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.
-
Aug 5th, 2004, 02:52 AM
#4
Fanatic Member
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());
-
Aug 5th, 2004, 03:12 AM
#5
Thread Starter
Lively Member
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.
-
Aug 5th, 2004, 08:00 AM
#6
Addicted Member
Should do a case-insensitive find for you
PHP Code:
int i = Array.BinarySearch(
new string[]{"one", "two", "three"},"tWo",System.Collections.CaseInsensitiveComparer.Default);
-
Aug 5th, 2004, 08:05 AM
#7
Thread Starter
Lively Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|