如何使用Linq确定此字符串是否以值EndsWith(来自集合)? [英] How can I use Linq to to determine if this string EndsWith a value (from a collection)?

查看:52
本文介绍了如何使用Linq确定此字符串是否以值EndsWith(来自集合)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试查找字符串值EndsWith是否是另一个字符串.此其他字符串"是集合中的值.我正在尝试将其作为字符串的扩展方法.

i'm trying to find out if a string value EndsWith another string. This 'other string' are the values from a collection. I'm trying to do this as an extension method, for strings.

例如

var collection = string[] { "ny", "er", "ty" };
"Johnny".EndsWith(collection); // returns true.
"Fred".EndsWith(collection); // returns false.

推荐答案

var collection = new string[] { "ny", "er", "ty" };

var doesEnd = collection.Any("Johnny".EndsWith);
var doesNotEnd = collection.Any("Fred".EndsWith);

您可以创建一个字符串扩展名以隐藏Any

You can create a String extension to hide the usage of Any

public static bool EndsWith(this string value, params string[] values)
{
    return values.Any(value.EndsWith);
}

var isValid = "Johnny".EndsWith("ny", "er", "ty");

这篇关于如何使用Linq确定此字符串是否以值EndsWith(来自集合)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆