Array - Does it exist?

S

shapper

Hello,

Is there a way to find to check if a string exists in an array of
string and not mind the case?

I have trying and Googgling for a solution but I am not able to find
it.

If it is not possible, how can I transform the array into a list so I
can apply a lambda expression?

Thanks,
Miguel
 
A

anonymous

Am I missing something?

string[] ary = new string[] { "me", "you", "someone else" };
for (int i = 0; i <= ary.Length-1; i++)
if (ary.ToLower() == "bob")
return;

for (int i = 0; i <= ary.Length - 1; i++)
if (ary.ToLower().IndexOf("bob") > 0)
return;
 
A

Arne Vajhøj

shapper said:
Is there a way to find to check if a string exists in an array of
string and not mind the case?

I have trying and Googgling for a solution but I am not able to find
it.

If it is not possible, how can I transform the array into a list so I
can apply a lambda expression?

There are plenty of possibilities.

One is:

Array.Exists(yourstrarray, s => String.Compare(s, "yourval", true) == 0)

Arne
 
M

Michael C

shapper said:
Hello,

Is there a way to find to check if a string exists in an array of
string and not mind the case?

I have trying and Googgling for a solution but I am not able to find
it.

If it is not possible, how can I transform the array into a list so I
can apply a lambda expression?

MyArray.Any(x => string.Compare(x, "Whatever", true) == 0);

Note that using Compare is more efficient than using ToLower because it does
not create a copy of the string.

Michael
 
S

shapper

MyArray.Any(x => string.Compare(x, "Whatever", true) == 0);

Note that using Compare is more efficient than using ToLower because it does
not create a copy of the string.

Michael

Thank You. I am using Compare and it is working fine ...

Thank You All,
Miguel
 
I

Ignacio Machin ( .NET/ C# MVP )

Hello,

Is there a way to find to check if a string exists in an array of
string and not mind the case?

I have trying and Googgling for a solution but I am not able to find
it.

If it is not possible, how can I transform the array into a list so I
can apply a lambda expression?

Thanks,
Miguel

Depending of what you want returned you can use one of a couple of
extension methods:
If you just want to know if it exist:
Any( x=> String.Compare( x, myString, true)==0);
if you want to return the item
FirstOrDefault( x=> String.Compare( x, myString, true)==0);
 
M

Michael C

Ignacio Machin ( .NET/ C# MVP ) said:
if you want to return the item
FirstOrDefault( x=> String.Compare( x, myString, true)==0);

Why would you want to return the item when you already have it in myString
:)
 
A

Arne Vajhøj

Michael said:
MyArray.Any(x => string.Compare(x, "Whatever", true) == 0);

Note that using Compare is more efficient than using ToLower because it does
not create a copy of the string.

Does the doc for Compare guarantee that it will not implement
ignore case by doing a ToLower internally ?

Arne
 
M

Michael C

Arne Vajhøj said:
Does the doc for Compare guarantee that it will not implement
ignore case by doing a ToLower internally ?

Not that I know of but it would be a pretty big oversight if it did use
ToLower.

Michael
 
A

Arne Vajhøj

Michael said:
Not that I know of but it would be a pretty big oversight if it did use
ToLower.

It is not good to base performance advice on guesses about
implementation.

Arne
 
A

Arne Vajhøj

Arne said:
It is not good to base performance advice on guesses about
implementation.

And a bit of experimentation indicates that:

string.Compare(s, target, true) == 0

is actually slightly slower than:

s.ToLower() == target

[where target has already been lowercased before]

Arne
 
M

Michael C

Call it an educated guess. As I said, I'd be *very* suprised if they called
ToLower. If I was wrong 50 nerds would have jumped in to tell me so (this is
a newsgroup remember :)
And a bit of experimentation indicates that:

string.Compare(s, target, true) == 0

is actually slightly slower than:

s.ToLower() == target

[where target has already been lowercased before]

In that case it would not need to create a copy of the string. Then the
comparison would be quicker of course. However, you generally don't call
ToLower on a string if you know it's going to be lower anyway. In the
majority of cases you are going to be duplicating the string so taking more
time and double the memory. In the best case senario, as you said, ToLower
is slightly quicker, but in the majority of cases it will slower.

Also, I'm no expert on multi-language stuff but I'm guessing string.Compare
*might* actually return a different result. I am just guessing but this is
the sort of thing that raises my suspisions, by doing ToLower you are not
specifically asking for a case insensitive comparison so there is the
potential for problems.

Michael
 
M

Michael C

Peter Duniho said:
I don't think your guess was a bad one, but I think Arne's point is
sound. If you care about performance, the only correct approach is to
measure and analyze. Choosing implementation details based on assumptions
of performance characteristics doesn't make sense.

I agree with what you say, however I think all good programmers make many
such educated guesses every day. I don't have time to performance check
every line of code I write but I can get pretty good performance by going on
lots of good guesses. As it turns out my "guess" in this case appears to be
accurate. I didn't think I was going out on much of a limb and it turns out
that everything I said was accurate, string.Compare is faster, it is more
efficient, it doesn't create a copy and ToLower does.
Of course, in most cases, the performance details are unimportant. But in
that case, the implementation details should be chosen based on what makes
the simplest, most maintainable code. Any mention of performance is just
a red herring.

That is true but when faced with 2 similar pieces of code I will often go
with the one that I think will have the best performance. I think the
ToLower comparison is possibly slightly more readable but the string.Compare
is the better method. It definately possible to go overboard on the
readability, a balance is always good. :)
String doesn't keep track of whether it was created using ToLower(). So,
even if the string has already been lower-cased, it has no way to avoid
creating a copy.

You are correct, I was thinking it might not create the copy until it
encounters a change but I just ran a test and it creates a copy in all
cases. I'm not sure how Arne ran his test but I found a significant
difference between the 2 methods on a 200meg string.
However, there's an important omission in Arne's performance description:
he didn't actually post the code, so we have no idea what his test
actually did.

Yep, I would be interested to see it and how he managed to get ToLower to be
faster.

Michael
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top