Taking item from string array that *might* be there
Posted
by AndyC
on Stack Overflow
See other posts from Stack Overflow
or by AndyC
Published on 2010-04-30T11:04:29Z
Indexed on
2010/04/30
11:17 UTC
Read the original article
Hit count: 130
c#
Hi all,
I have a string array, and it may contain an element with the text "mytext" within the string. eg:
mystringarray
{
[0] => "hello world";
[1] => "some of mytext";
}
I also have an array that doesn't have the mytext text in it.
mystringarray
{
[0] => "hello world";
[1] => "some of notmy";
}
My problem is when I use:
string mytextdata = mystringarray.Single<string>(t => t.Contains("mytext")).ToString();
I get an exception for the second array as it can't find an element that matches the expression.
Is there a quick way I can edit this one line to not throw an exception if it finds nothing, and instead just ignore? I have a lot of these lines and I don't want to have to wrap each in an if statement.
Apologies if question isn't clear.
© Stack Overflow or respective owner