【发布时间】:2018-03-29 01:44:14
【问题描述】:
传入的pattern 和format,考虑到它们都是小写的,我如何更改下面的sn-p 代码以便startsWith 和endsWith 返回true 如果模式和格式匹配大小写不敏感?
try (Stream<Path> paths = Files.find(cobDir, 1,
(path, attrs) -> attrs.isRegularFile()
&& path.getFileName().startsWith(pattern)
&& path.toString().endsWith(format))) {
matchingFile = paths.findFirst();
} catch (IOException e) {
logger.error("Problem with getting files to process {}", e.getMessage());
}
有没有比以下更漂亮的方法:
try (Stream<Path> paths = Files.find(cobDir, 1,
(path, attrs) -> attrs.isRegularFile()
&& path.getFileName().toString().toLowerCase().startsWith(pattern)
&& path.toString().toLowerCase().endsWith(format))) {
matchingFile = paths.findFirst();
} catch (IOException e) {
logger.error("Problem with getting files to process {}", e.getMessage());
}
【问题讨论】:
-
toUpperCase()或toLowerCase()路径的字符串和format。 -
See this Q&A 剧透:不漂亮。