文章出處

需求是這樣的:不允許在一個字符串中同時出現"博", "客", "園", "團", "隊"這5個文字。

如果不用LINQ,代碼寫起來會很啰嗦:

var teststr = "博2客0園1團4隊.";
if (teststr.IndexOf("") >= 0 &&
    teststr.IndexOf("") >= 0 &&
    teststr.IndexOf("") >= 0 &&
    teststr.IndexOf("") >= 0 &&
    teststr.IndexOf("") >= 0)
{
    //...
}

而用LINQ,代碼立馬變得簡潔:

var teststr = "博2客0園1團4隊.";           
var cmt = new string[] { "", "", "", "", "" };
if (cmt.All(teststr.Contains))
{
    //...
}

LINQ讓寫代碼變得更享受,也更具表達力。

【參考】

Using C# to check if string contains a string in string array


文章列表


不含病毒。www.avast.com
arrow
arrow
    全站熱搜
    創作者介紹
    創作者 大師兄 的頭像
    大師兄

    IT工程師數位筆記本

    大師兄 發表在 痞客邦 留言(0) 人氣()