File extension is in extensions list: Difference between revisions

Content added Content deleted
Line 761: Line 761:
}</lang>
}</lang>
It would be one line if not for the dot requirement. <code>FileNameExtensionFilter</code> requires that the extensions have no dots. It also requires that the extensions contain characters (i.e. not the empty string) so that test would need to be removed.
It would be one line if not for the dot requirement. <code>FileNameExtensionFilter</code> requires that the extensions have no dots. It also requires that the extensions contain characters (i.e. not the empty string) so that test would need to be removed.

===Using Lambda Expressions===
{{works with|Java|Current+}}
This version is the same as the main version only replace the definition for <code>extIsIn</code> with:
<lang java5>public static boolean extIsIn(String test, String... exts) {
int lastSlash = Math.max(test.lastIndexOf('/'), test.lastIndexOf('\\')); //whichever one they decide to use today
String filename = test.substring(lastSlash + 1);//+1 to get rid of the slash or move to index 0 if there's no slash

//end of the name if no dot, last dot index otherwise
int lastDot = filename.lastIndexOf('.') == -1 ? filename.length() : filename.lastIndexOf('.');
String ext = filename.substring(lastDot);//everything at the last dot and after is the extension

Arrays.sort(exts);//sort for the binary search

return Arrays.binarySearch(exts, ext, String::compareToIgnoreCase) >= 0;//binarySearch returns negative numbers when it's not found
}
}</lang>


=={{header|jq}}==
=={{header|jq}}==