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.
eg.
var collection = string[] { "ny", "er", "ty" };
"Johnny".EndsWith(collection); // returns true.
"Fred".EndsWith(collection); // returns false.
Select(x => new { x, count = x. tags. Count(tag => list. Contains(tag)) }) .
LINQ can be used to query and transform strings and collections of strings. It can be especially useful with semi-structured data in text files. LINQ queries can be combined with traditional string functions and regular expressions. For example, you can use the String.
In C#, EndsWith() is a string method. This method is used to check whether the ending of the current string instance matches with a specified string or not. If it matches, then it returns the string otherwise false. Using “foreach” loop, it is possible to check many strings.
The Linq Contains Method in C# is used to check whether a sequence or collection (i.e. data source) contains a specified element or not. If the data source contains the specified element, then it returns true else return false.
var collection = new string[] { "ny", "er", "ty" };
var doesEnd = collection.Any("Johnny".EndsWith);
var doesNotEnd = collection.Any("Fred".EndsWith);
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");
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With