我有一个这种形式的文件列表:
name_of_file_001.csv
name_of_file_002.csv
name_of_file_123.csv
or
name_of_file.csv
second_name_of_file.csv
我不知道这个文件有没有001。
如何在 java 中获取文件名(仅 name_of_file)?
最佳答案
尝试以下操作:
int i=0;
while(!fullName.charAt(i).equals('.')&&!fullName.charAt(i).equals('0')){
i++;
}
String name=fullName.substring(0, i);
取从fullName
开始到第一次出现的字符串。
或0
。
编辑:
引用评论和大于1..
的高数的情况,灵感来自于此answer :
int i=0;
String patternStr = "[0-9\.]";
Pattern pattern = Pattern.compile(patternStr);
Matcher matcher = pattern.matcher(fullName);
if(matcher.find()){
i=matcher.start(); //this will give you the first index of the regex
}
String name=fullName.substring(0, i);
编辑 2:
如果没有扩展名且全名与正则表达式不匹配(没有数字):
if(matcher.find()){
i=matcher.start(); //this will give you the first index of the regex
}else {
i=fullname.length();
}
String name=fullName.substring(0, i);
或者简单地说,我们将采用所有名称。
关于java - 如何拆分名称文件以计算 java 中的系列?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28669167/