【发布时间】:2014-01-21 04:45:10
【问题描述】:
我基本上是在尝试拆分最后一个时期的字符串以捕获文件扩展名。但是有时该文件没有任何扩展名,所以我很期待。
但问题是某些文件名在结尾之前有句点...
/mnt/sdcard/OG Ron C, Chopstars & Drake - Choppin Ain't The Same-2013-MIXFIEND/02 Drake - Connect (Feat. Fat Pat) (Chopped Not Slopped).mp3
所以当那个字符串出现时,它会在“02 Drake - Connect (Feat.”) 处截断它。
这是我一直在使用的...
String filePath = intent.getStringExtra(ARG_FILE_PATH);
String fileType = filePath.substring(filePath.length() - 4);
String FileExt = null;
try {
StringTokenizer tokens = new StringTokenizer(filePath, ".");
String first = tokens.nextToken();
FileExt = tokens.nextToken();
}
catch(NoSuchElementException e) {
customToast("the scene you chose, has no extension :(");
}
System.out.println("EXT " + FileExt);
File fileToUpload = new File(filePath);
如何在文件扩展名处拆分字符串,但在文件没有扩展名时也能够处理和警告。
【问题讨论】:
-
考虑使用
lastIndexOf(),使用一些启发式方法来检测它何时表示“扩展名”以及何时是文件名的一部分。 -
为什么不反转字符串,取'.'之前的第一个子字符串?
-
这个链接可能对你有帮助stackoverflow.com/questions/4894885/…