我有一个数组,我想检查数字中的最后一位数字.
例:
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);
Run Code Online (Sandbox Code Playgroud)
请注意:我知道我可以使用for循环检查每个项目.
我想知道是否有更有效的方法来做到这一点.原因是图像字符串已经在不断变化的循环上.
Nat*_*hes 13
Arrays.asList(types).contains(image.substring(image.lastIndexOf('.') + 1))
Run Code Online (Sandbox Code Playgroud)
您可以对最后4个字符进行子串:
String ext = image.substring(image.length - 4, image.length);
然后使用一个HashMap或其他搜索实现来查看它是否在您批准的文件扩展名列表中.
if(fileExtensionMap.containsKey(ext)) {
| 归档时间: |
|
| 查看次数: |
4007 次 |
| 最近记录: |