I have a string and I want to check if it contains any invalid filename characters. I know I could use
Path.GetInvalidFileNameChars
to get a array of invalid filename characters and loop through to see if the string contains any invalid char. But is there a easier and shorter expression? It is c#. So can anyone help?
bool containsInValidFilenameCharacters(string str) {
return str.Any(Path.GetInvalidFileNameChars().Contains)
}
Note that this is the same as doing
var invalidChars = Path.GetInvalidFileNameChars();
return str.Any(c => invalidChars.Contains(c));
But since the type signature of Contains matches up exactly with the parameter delegate type of Any we can just pass it directly and it will do an implicit conversion.
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