DaveAKing / guava-libraries

Automatically exported from code.google.com/p/guava-libraries
Apache License 2.0
0 stars 0 forks source link

getFileExtension return wrong result when path contains dot and filename without extension #1462

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
from getFileExtension docs:

"Returns the file extension for the given file name, or the empty string if the 
file has no extension"

this working fine when the path like:
D:\\Projects\\this file

the result is empty string.

but when the path is:
D:\\Files.Projects\\this file

the result is not empty string, result is "Projects\\this file"

suggested solution to fix this:

public static String getFileExtension(String fileName) {
 checkNotNull(fileName);

 String extension = "";     
 int index = fileName.lastIndexOf("\\");
 if ( index > 0 ) {         
   fileName= fileName.substring(index+1);
 }          

 index = fileName.lastIndexOf(".");
 if ( index > 0 ) {
   extension = fileName.substring(index+1);
 }

 return extension;
}

Regards,

Original issue reported on code.google.com by wajdyes...@gmail.com on 30 Jun 2013 at 8:58

GoogleCodeExporter commented 9 years ago
This is already fixed in the latest release (14.0.1 ). The fix is in this 
commit: 
https://code.google.com/p/guava-libraries/source/detail?r=5e983f3030df67659b3f5e
a48dd659173f6cd0a4&name=v14.0.1

We changed the method to create a File and use it to handle getting the file 
name, which should guarantee it works correctly on any platform. Your solution, 
for example, would only work correctly on Windows.

Original comment by cgdecker@google.com on 30 Jun 2013 at 9:15

GoogleCodeExporter commented 9 years ago
It looks like the status change didn't take last time somehow, so let me try 
again...

Original comment by cgdecker@google.com on 30 Jun 2013 at 9:26

GoogleCodeExporter commented 9 years ago
This issue has been migrated to GitHub.

It can be found at https://github.com/google/guava/issues/<issue id>

Original comment by cgdecker@google.com on 1 Nov 2014 at 4:12

GoogleCodeExporter commented 9 years ago

Original comment by cgdecker@google.com on 3 Nov 2014 at 9:08