我有一個字符串數組,並且它可能包含一個在字符串中帶有文本「mytext」的元素。例如:從可能存在的字符串數組中取出項目
mystringarray
{
[0] => "hello world";
[1] => "some of mytext";
}
我也有一個沒有mytext文本的數組。
mystringarray
{
[0] => "hello world";
[1] => "some of notmy";
}
我的問題是,當我使用:
string mytextdata = mystringarray.Single<string>(t => t.Contains("mytext")).ToString();
我得到一個異常的第二陣列,因爲它無法找到該表達式匹配的元素。
有沒有一種快速的方法,我可以編輯這一行,如果它什麼都沒發現,不會拋出異常,而是忽略?我有很多這些行,我不想在if語句中包裝每一行。
道歉,如果問題不明確。
非常好!乾杯! – AndrewC 2010-04-30 11:28:54