码迷,mamicode.com
首页 > 其他好文 > 详细

享受LINQ:判断一组文字是否在字符串中同时出现的最简单方法

时间:2014-07-16 19:30:52      阅读:227      评论:0      收藏:0      [点我收藏+]

标签:style   blog   http   color   io   代码   

需求是这样的:不允许在一个字符串中同时出现"博", "客", "园", "团", "队"这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

享受LINQ:判断一组文字是否在字符串中同时出现的最简单方法,布布扣,bubuko.com

享受LINQ:判断一组文字是否在字符串中同时出现的最简单方法

标签:style   blog   http   color   io   代码   

原文地址:http://www.cnblogs.com/dudu/p/3841256.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!