gpt4 book ai didi

.net - 如何使用 Linq 来确定此字符串 EndsWith 是否为值(来自集合)?

转载 作者:行者123 更新时间:2023-12-04 10:45:51 26 4
gpt4 key购买 nike

我试图找出一个字符串值 EndsWith另一个字符串。这个“其他字符串”是集合中的值。我正在尝试将此作为字符串的扩展方法。

例如。

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 的用法。
public static bool EndsWith(this string value, params string[] values)
{
return values.Any(value.EndsWith);
}

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

关于.net - 如何使用 Linq 来确定此字符串 EndsWith 是否为值(来自集合)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1641499/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com