How could I construct a LINQ expression to remove values from one list that meet the criteria of a function that returns a boolean?
string[] message = "days of the week"
message.ToList().RemoveAll(c=>checkShortWord(c));
public static bool checkShortWord(string word) {
if ((word.Length > 3) &&
(!Regex.IsMatch(word, "^[0-9]+$")))
return true;
return false;
}
My ending string array should now be:
message = {"days","week"}
What should I change? My message array never changes.
You are constructing a new List and removing the items from that list, and then throwing it away. If you want an array that is missing the removed items, you will need to create a new one:
Alternately, you could use a
List<String>instead of astring[], and then use the RemoveAll method to modify it in place:As others have mentioned, you have also named your predicate method badly. “IsLongWord” might be more appropriate. You could write it a little more simply like this: