473,398 Members | 2,212 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,398 software developers and data experts.

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","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,
Jun 27 '08 #1
6 7637
CSharper <cs******@gmx.comwrote:
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
Jun 27 '08 #2
On Apr 23, 3:00*pm, CSharper <cshar...@gmx.comwrote:
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
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.comwrote:
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.
Jun 27 '08 #4
On Apr 23, 2:15 pm, Jon Skeet [C# MVP] <sk...@pobox.comwrote:
CSharper <cshar...@gmx.comwrote:
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?
Thanks you very much?
Jun 27 '08 #5
CSharper <cs******@gmx.comwrote:
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.com>
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.comwrote:
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.com>
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
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...
80
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: ...
8
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
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...
7
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...
1
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...
1
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:- ...
4
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
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...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
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...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
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...

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.