I have an array I want to check the the last digits if it is in the array.
Example:
String[] types = {".png",".jpg",".gif"}
String image = "beauty.jpg";
// Note that this is wrong. The parameter required is a string not an array.
Boolean true = image.endswith(types);
Please note: I know I can check each individual item using a for loop.
I want to know if there is a more efficient way of doing this. Reason being is that image string is already on a loop on a constant change.
Arrays.asList(types).contains(image.substring(image.lastIndexOf('.') + 1))
You can substring the last 4 characters:
String ext = image.substring(image.length - 4, image.length);
and then use a HashMap
or some other search implementation to see if it is in your list of approved file extensions.
if(fileExtensionMap.containsKey(ext)) {
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