Currently I am using code like this
目前我正在使用这样的代码
while (fileName.endsWith(".csv")) {
fileName = fileName.substring(0, fileName.lastIndexOf(FILE_SUFFIX_CSV));
if (fileName.trim().isEmpty()) {
throw new IllegalArgumentException();
}
}
The above code works fine when user specifies extension in small letters(.csv),But windows accepts extensions case sensitive so he can give like .CsV ,.CSV etc. how can I alter above code ?
当用户用小写字母(.csv)指定扩展名时,上面的代码工作正常,但是windows接受区分大小写的扩展名,所以他可以给出类似.CsV,.CSV等。我怎样才能改变上面的代码?
Thanks in Advance
提前致谢
5 个解决方案
#1
12
why don't you turn it to lowercase?
你为什么不把它变成小写?
while (fileName.toLowerCase().endsWith(".csv")) {
fileName = fileName.substring(0, fileName.toLowerCase().lastIndexOf(FILE_SUFFIX_CSV));
if (fileName.trim().isEmpty()) {
throw new IllegalArgumentException();
}
}
#2
5
Late night regex solution:
深夜正则表达式解决方案:
Pattern pattern = Pattern.compile(".csv", Pattern.CASE_INSENSITIVE);
Matcher matcher = pattern.matcher(fileName);
while (matcher.find()) {
fileName = fileName.substring(0, matcher.start());
if (fileName.trim().isEmpty()) {
throw new IllegalArgumentException();
}
}
The Matcher
will only find()
once. It can then report its start
position which you can use to substring
the original file name.
匹配器只会找到()一次。然后,它可以报告其起始位置,您可以使用该位置对原始文件名进行子串。
#3
4
You can try this way
你可以试试这种方式
int lastIndexOfDot=fileName.lastIndexOf("\\.");
String fileExtension=fileName.substring(lastIndexOfDot+1,fileName.length());
while(fileExtension.equalsIgnoreCase(".csv")){
}
Or
while(fileName.toUpperCase().endsWith(".CSV"){}
#4
4
Please convert to lowercase and then compare.
请转换为小写然后进行比较。
while (fileName.toLowerCase().endsWith(".csv")) {
fileName = fileName.toLowerCase().substring(0, fileName.toLowerCase().lastIndexOf(FILE_SUFFIX_CSV));
if (fileName.toLowerCase().trim().isEmpty()) {
throw new IllegalArgumentException();
}
}
#5
3
You can convert both to uppercase.
您可以将两者都转换为大写。
So change this line
所以改变这一行
fileName = fileName.substring(0, fileName.lastIndexOf(FILE_SUFFIX_CSV));
to
fileName = fileName.toUpperCase().substring(0, fileName.lastIndexOf(FILE_SUFFIX_CSV.toUpperCase()));
#1
12
why don't you turn it to lowercase?
你为什么不把它变成小写?
while (fileName.toLowerCase().endsWith(".csv")) {
fileName = fileName.substring(0, fileName.toLowerCase().lastIndexOf(FILE_SUFFIX_CSV));
if (fileName.trim().isEmpty()) {
throw new IllegalArgumentException();
}
}
#2
5
Late night regex solution:
深夜正则表达式解决方案:
Pattern pattern = Pattern.compile(".csv", Pattern.CASE_INSENSITIVE);
Matcher matcher = pattern.matcher(fileName);
while (matcher.find()) {
fileName = fileName.substring(0, matcher.start());
if (fileName.trim().isEmpty()) {
throw new IllegalArgumentException();
}
}
The Matcher
will only find()
once. It can then report its start
position which you can use to substring
the original file name.
匹配器只会找到()一次。然后,它可以报告其起始位置,您可以使用该位置对原始文件名进行子串。
#3
4
You can try this way
你可以试试这种方式
int lastIndexOfDot=fileName.lastIndexOf("\\.");
String fileExtension=fileName.substring(lastIndexOfDot+1,fileName.length());
while(fileExtension.equalsIgnoreCase(".csv")){
}
Or
while(fileName.toUpperCase().endsWith(".CSV"){}
#4
4
Please convert to lowercase and then compare.
请转换为小写然后进行比较。
while (fileName.toLowerCase().endsWith(".csv")) {
fileName = fileName.toLowerCase().substring(0, fileName.toLowerCase().lastIndexOf(FILE_SUFFIX_CSV));
if (fileName.toLowerCase().trim().isEmpty()) {
throw new IllegalArgumentException();
}
}
#5
3
You can convert both to uppercase.
您可以将两者都转换为大写。
So change this line
所以改变这一行
fileName = fileName.substring(0, fileName.lastIndexOf(FILE_SUFFIX_CSV));
to
fileName = fileName.toUpperCase().substring(0, fileName.lastIndexOf(FILE_SUFFIX_CSV.toUpperCase()));