如何查看java中我的文件名包含的字符串?

时间:2015-07-13 09:39:46

标签: java

  List <String> ntfs = Arrays.asList("AUX","NUL","SOH","STX","ETX","EOT","ENQ","ACK","BEL",
    "BS","HT","VT","LF","FF","NP","CR","SO","SI","DLE","DC1","DC2","DC3","DC4","NAK","SYN",
            "ETB","CAN","EM","SUB","ESC","escape","FS","GS","RS","US","AUX", "CLOCK$", 
            "COM1","COM2","COM3","COM4","COM5","COM6","COM7","COM8","COM9" , "CON", 
            "LPT1","LPT2","LPT3","LPT4","LPT5","LPT6","LPT7","LPT8","LPT9", "NUL","PRN");

例如:

 String pathname="/path/AUX.txt";

所以我想在路径名中包含或不包含列表项,我该怎么做?非常感谢你!

2 个答案:

答案 0 :(得分:1)

使用List#contains()方法检查元素是否存在。

    List<String> ntfs = Arrays.asList("AUX", "NUL", "SOH", "STX", "ETX", "EOT", "ENQ", "ACK", "BEL", "BS", "HT", "VT", "LF", "FF", "NP", "CR", "SO", "SI", "DLE", "DC1", "DC2", "DC3", "DC4", "NAK", "SYN", "ETB", "CAN", "EM", "SUB", "ESC", "escape", "FS", "GS", "RS", "US", "AUX", "CLOCK$", "COM1", "COM2", "COM3", "COM4", "COM5", "COM6", "COM7", "COM8", "COM9", "CON", "LPT1", "LPT2", "LPT3", "LPT4", "LPT5", "LPT6", "LPT7", "LPT8", "LPT9", "NUL", "PRN");
    String pathname = "/path/AUX.txt";
    // Take out the `AUX` part from `/path/AUX.txt` by string operations
    if (ntfs.contains(pathname.substring(pathname.lastIndexOf("/") + 1, pathname.indexOf(".")))) {
        System.out.println("PRESENT");
    } else {
        System.out.println("ABSENT");
    }

答案 1 :(得分:0)

没有评论:

public static boolean contains(List<String> ntfs,String pathname){ 
   for(String s:ntfs){
        if(pathname.contains(s)){
            return true;
        }
    }
    return false;
}