c#:
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?
[quoted text, click to view] On Apr 23, 3:00=A0pm, CSharper <cshar...@gmx.com> wrote: > 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 =3D {"help","Csharp rocks"} > > if (array.Contains<string>("Csharp")) > { > =A0 =A0//here I want to get the actual string like "Csharp rocks" > =A0 =A0string str =3D 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 :)
On Apr 23, 2:18 pm, "Ignacio Machin ( .NET/ C# MVP )" [quoted text, click to view] <ignacio.mac...@gmail.com> wrote: > On Apr 23, 3:00 pm, CSharper <cshar...@gmx.com> wrote: > > > > > 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
[quoted text, click to view] On Apr 23, 2:15 pm, Jon Skeet [C# MVP] <sk...@pobox.com> wrote: > CSharper <cshar...@gmx.com> wrote: > > 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) > { > ... > > } > > -- > Jon Skeet - <sk...@pobox.com> http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet > World class .NET training in the UK: http://iterativetraining.co.uk 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?
[quoted text, click to view] CSharper <csharper@gmx.com> wrote: > 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) { ... } -- Jon Skeet - <skeet@pobox.com> http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
[quoted text, click to view] CSharper <csharper@gmx.com> wrote: > 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. [quoted text, click to view] > 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> -- Jon Skeet - <skeet@pobox.com> http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
[quoted text, click to view] On Apr 23, 4:32 pm, Jon Skeet [C# MVP] <sk...@pobox.com> wrote: > <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,
[quoted text, click to view] On Apr 24, 9:01 am, Chris Dunaway <dunaw...@gmail.com> wrote: > On Apr 23, 4:32 pm, Jon Skeet [C# MVP] <sk...@pobox.com> wrote: > > > <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
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?
[quoted text, click to view] Chris Dunaway <dunawayc@gmail.com> wrote: > On Apr 23, 4:32 pm, Jon Skeet [C# MVP] <sk...@pobox.com> wrote: > > > <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.
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. -- Jon Skeet - <skeet@pobox.com> http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
Don't see what you're looking for? Try a search.
|