In C# there is a way of reducing the length of an if-statement by using Enumerable.Any
to check if elements in a sequence satisfy a condition (https://msdn.microsoft.com/en-us/library/vstudio/bb534972%28v=vs.100%29.aspx).
For example Instead of:
If ( string.Contains(">") || string.Contains("<") || string.Contains("&") || string.Contains("l") || string.Contains("p") )
We can use
if (new [] { ">", "<", "&", "l", "p"}.Any(w => string.Contains(w)))
Is there an equivalent, if not better, way of doing this in Java?
With Java 8 you can write something like:
Out of curiosity I ran a benchmark to compare this method vs a regex. Code and results below (lower score = faster). Streams perform an order of magnitude better than regex.
Code: