getting first element from array

  • Thread starter Thread starter CSharper
  • Start date Start date
C

CSharper

I am trying to use the following; I have an array with bunch of values
in it. I am trying to find a value that contains part of the string I
am passing
eg
string[] array = {"help","Csharp rocks"}

if (array.Contains<string>("Csharp"))
{
//here I want to get the actual string like "Csharp rocks"
string str = array.First<string>(); //Here there is a way, I can
check if the string has the value using lambda expression, not sure
how.
}

Could someone help?
Thanks,
 
CSharper said:
I am trying to use the following; I have an array with bunch of values
in it. I am trying to find a value that contains part of the string I
am passing
eg
string[] array = {"help","Csharp rocks"}

if (array.Contains<string>("Csharp"))
{
//here I want to get the actual string like "Csharp rocks"
string str = array.First<string>(); //Here there is a way, I can
check if the string has the value using lambda expression, not sure
how.
}

You can avoid the "contains" bit to start with by using FirstOrDefault:

string first = array.FirstOrDefault(x => x.Contains("Csharp"));

if (first != null)
{
...
}
 
I am trying to use the following; I have an array with bunch of values
in it. I am trying to find a value that contains part of the string I
am passing
eg
string[] array = {"help","Csharp rocks"}

if (array.Contains<string>("Csharp"))
{
   //here I want to get the actual string like "Csharp rocks"
   string str = array.First<string>(); //Here there is a way, I can
check if the string has the value using lambda expression, not sure
how.

}

Could someone help?
Thanks,

Hi,

myStringList.Find(delegate(string item) { return
item.Contains("MyStr"); });

Love generics :)

If you are in 3.5 you could replace it with a Lambda expression
 
I am trying to use the following; I have an array with bunch of values
in it. I am trying to find a value that contains part of the string I
am passing
eg
string[] array = {"help","Csharp rocks"}
if (array.Contains<string>("Csharp"))
{
//here I want to get the actual string like "Csharp rocks"
string str = array.First<string>(); //Here there is a way, I can
check if the string has the value using lambda expression, not sure
how.

Could someone help?
Thanks,

Hi,

myStringList.Find(delegate(string item) { return
item.Contains("MyStr"); });

Love generics :)

If you are in 3.5 you could replace it with a Lambda expression

Thank you both for the help.
 
CSharper said:
I am trying to use the following; I have an array with bunch of values
in it. I am trying to find a value that contains part of the string I
am passing
eg
string[] array = {"help","Csharp rocks"}
if (array.Contains<string>("Csharp"))
{
//here I want to get the actual string like "Csharp rocks"
string str = array.First<string>(); //Here there is a way, I can
check if the string has the value using lambda expression, not sure
how.
}

You can avoid the "contains" bit to start with by using FirstOrDefault:

string first = array.FirstOrDefault(x => x.Contains("Csharp"));

if (first != null)
{
...

}
quick question, what does (x=>x.Contains("Csharp")) means?
I know that, x is each element from the collection and it returns the
string of x. So does it mean, foreach element in the array first the
first element which contains the requested match?
x=>x.Contains() what does it do? It is evaluating to a bool and where
is the return here?
Thanks you very much?
 
CSharper said:
quick question, what does (x=>x.Contains("Csharp")) means?
I know that, x is each element from the collection and it returns the
string of x. So does it mean, foreach element in the array first the
first element which contains the requested match?

Sort of, yes.
x=>x.Contains() what does it do? It is evaluating to a bool and where
is the return here?
Thanks you very much?

It's a lambda expression. I don't have time to go into lambda
expressions in detail right now, but hopefully you can find out a fair
amount about them just by searching with the right name.

My bluffer's guide gives *slightly* more detail:
http://csharpindepth.com/Articles/General/BluffersGuide3.aspx

<shameless plug>
If you want *lots* of detail, look at chapter 9 in my book - click on
the image on the left hand side of the page linked above.
</shameless plug>
 
<shameless plug>
If you want *lots* of detail, look at chapter 9 in my book - click on
the image on the left hand side of the page linked above.
</shameless plug>

Has the book been released yet? Amazon still shows April 15, 2008 and
haven't updated their site in awhile with any new information.
Manning's site seems to indicate that it is available (not pre-order)
but Amazon doesn't seem to know about it and hasn't updated the
shipping information on my order.

Regards,

Chris
 
Has the book been released yet? Amazon still shows April 15, 2008 and
haven't updated their site in awhile with any new information.
Manning's site seems to indicate that it is available (not pre-order)
but Amazon doesn't seem to know about it and hasn't updated the
shipping information on my order.

Regards,

Chris

Thanks Jon and I appriciate your answer and the link and yes, I will
buy the book (based on the content on your page, it is worth
pursuing...)
I am starting to write a blog for myself and bunch of my friends at
work to keep up with C# and here it the link, if you have time have a
look at it and let me know if the content is right?
http://www.myfavoritemovies.us/csharp
 
Chris Dunaway said:
Has the book been released yet? Amazon still shows April 15, 2008 and
haven't updated their site in awhile with any new information.
Manning's site seems to indicate that it is available (not pre-order)
but Amazon doesn't seem to know about it and hasn't updated the
shipping information on my order.

It's been shipped from the printers - I've got my copies. I'd expect it
to become in stock at Amazon Real Soon Now (tm). I keep checking as
well - as soon as it's available from Amazon I'll blog about it one
last time :)

I suspect you'll get it faster if you order it from Manning though - I
assume they've got copies ready to ship. I haven't checked pricing,
mind.
 
CSharper said:
Thanks Jon and I appriciate your answer and the link and yes, I will
buy the book (based on the content on your page, it is worth
pursuing...)
I am starting to write a blog for myself and bunch of my friends at
work to keep up with C# and here it the link, if you have time have a
look at it and let me know if the content is right?
http://www.myfavoritemovies.us/csharp

Looks good to me, but I'd try to format the code a bit more clearly.
You're welcome to use the formatter I use for my blog:

http://csharpindepth.com/CodeFormatterTool.aspx
 
Looks good to me, but I'd try to format the code a bit more clearly.
You're welcome to use the formatter I use for my blog:

http://csharpindepth.com/CodeFormatterTool.aspx

Hi Jon,

Thank you very much and yes, I would love to use the formatter. I use
BN and I have made the pre-order already. Looking forward to reading
the good book. I will blog about it as well in my blog. Bunch of my
friends (CSharpers) follow my Ironruby blog (http://
www.myfavoritemovies.us/ironruby) and I will put it there along with
my c# blog as well.

Thanks for the help.
 
CSharper said:
Thank you very much and yes, I would love to use the formatter. I use
BN and I have made the pre-order already. Looking forward to reading
the good book. I will blog about it as well in my blog. Bunch of my
friends (CSharpers) follow my Ironruby blog (http://
www.myfavoritemovies.us/ironruby) and I will put it there along with
my c# blog as well.

Cool - the more the merrier :)
 
Back
Top