473,800 Members | 2,599 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

getting first element from array

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","Csh arp rocks"}

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

Could someone help?
Thanks,
Jun 27 '08 #1
6 7682
CSharper <cs******@gmx.c omwrote:
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","Csh arp rocks"}

if (array.Contains <string>("Cshar p"))
{
//here I want to get the actual string like "Csharp rocks"
string str = array.First<str ing>(); //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.FirstOrDe fault(x =x.Contains("Cs harp"));

if (first != null)
{
...
}

--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
World class .NET training in the UK: http://iterativetraining.co.uk
Jun 27 '08 #2
On Apr 23, 3:00*pm, CSharper <cshar...@gmx.c omwrote:
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","Csh arp rocks"}

if (array.Contains <string>("Cshar p"))
{
* *//here I want to get the actual string like "Csharp rocks"
* *string str = array.First<str ing>(); //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.Fi nd(delegate(str ing item) { return
item.Contains(" MyStr"); });

Love generics :)

If you are in 3.5 you could replace it with a Lambda expression
Jun 27 '08 #3
On Apr 23, 2:18 pm, "Ignacio Machin ( .NET/ C# MVP )"
<ignacio.mac... @gmail.comwrote :
On Apr 23, 3:00 pm, CSharper <cshar...@gmx.c omwrote:
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","Csh arp rocks"}
if (array.Contains <string>("Cshar p"))
{
//here I want to get the actual string like "Csharp rocks"
string str = array.First<str ing>(); //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.Fi nd(delegate(str ing 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.
Jun 27 '08 #4
On Apr 23, 2:15 pm, Jon Skeet [C# MVP] <sk...@pobox.co mwrote:
CSharper <cshar...@gmx.c omwrote:
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","Csh arp rocks"}
if (array.Contains <string>("Cshar p"))
{
//here I want to get the actual string like "Csharp rocks"
string str = array.First<str ing>(); //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.FirstOrDe fault(x =x.Contains("Cs harp"));

if (first != null)
{
...

}

--
Jon Skeet - <sk...@pobox.co m>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?
Thanks you very much?
Jun 27 '08 #5
CSharper <cs******@gmx.c omwrote:
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/Ge...ersGuide3.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 - <sk***@pobox.co m>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
World class .NET training in the UK: http://iterativetraining.co.uk
Jun 27 '08 #6
CSharper <cs******@gmx.c omwrote:
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

--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
World class .NET training in the UK: http://iterativetraining.co.uk
Jun 27 '08 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

17
3362
by: Michael Hopkins | last post by:
Hi all I want to create a std::vector that goes from 1 to n instead of 0 to n-1. The only change this will have is in loops and when the vector returns positions of elements etc. I am calling this uovec at the moment (for Unit-Offset VECtor). I want the class to respond correctly to all usage of STL containers and algorithms so that it is a transparent replacement for std:vector. The options seems to be:
80
5293
by: Bibby | last post by:
Hi, I'm interested in getting started in the programming world. I've dabbled in C, C++ and VB6. Which would be the best language to focus my attention to regarding the following considerations: Hireability Portability Flexibility The likely candidates seem to be Java, VB.Net, C, C++, C#.
8
2375
by: ehames | last post by:
Hi guys, As far as I know, for any type T, and positive integer N, if I have an array declared as: T array; then, &array and array are the same element. Is there any reason why a
11
1961
by: The Crow | last post by:
i have a arraylist. say it contains integer values. i want to be able to inform user, which indexes in the array contain same values. but there can be N different values, and M different indexes having the same value. actually i will do this with javascript array to inform user which input's have same value. but i think its not a matter.
7
6102
by: chuck | last post by:
I load a bitmap using GDI+ now, I want to collect all the pixel values, (in RGB but without the A component) onto a color array, say... dim btmp1(,) as color is there a method that can help me collect the pixel values as quickly as possible (like one row at a time)? what about setting (or changing) the pixel values back onto the bitmap?
1
8260
by: yawnmoth | last post by:
Given an element ID, is there a way to figure out what index one would need to use in the parentNode's childNodes array to get at that element? For example... <body> <div id="parent"> <div id="a">a</div> <div id="b">b</div>
1
9485
by: kamleshsharmadts | last post by:
I am using Ajax with struts in web application. from jsp i am calling a function of ajax.js onclick of a button. code of that call function which calling from jsp given as below:- onclick="retrieveURL('/application/home_page.do?processAction=ItinerarySearch','AfoHomeForm')" after clicking on button i am getting following error:- object does not support this property or method at statement (Which i have made it bold in js file)
4
1648
by: nembo kid | last post by:
I have the following bidimensional array int a ; Why the first address of this array is only: & (mat) and not also:
4
4564
by: mab464 | last post by:
I have this code on my WAMP server running on my XP machine if ( isset( $_POST ) ) { for($i=0; $i<count($_POST);$i++) { if ($ans != NULL ) $ans .= ", " . $_POST ; // Not the first element so append a comma
0
9551
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10274
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
10033
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
6811
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5469
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5606
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4149
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3764
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2945
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.