yf0994 / guava-libraries

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

Files.getNameWithoutPath(String fullName) #1135

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
Shouldn't there also be a method:

Files.getNameWithoutPath(String fullName)

I recognize that there would be the forward vs. backward slash issue--perhaps 
you could add another parameter that indicates the platform's slash direction.

Here's my version (you're free to use it/modify it/etc.)

   public static String getFile(String fullName, bool forwardSlash) {
      return fullName.substring(fullName.lastIndexOf(
         forwardSlash ? '/' : '\') + 1, fullName.length());
   }

Original issue reported on code.google.com by glenview...@gmail.com on 6 Sep 2012 at 2:25

GoogleCodeExporter commented 9 years ago
This should already work like you want: you can pass a full path to the method, 
not just the filename by itself. It already handles the separator issue by 
creating a File internally and getting the filename from it. Perhaps changing 
the parameter name to "fullName" to match getFileExtension would make this more 
clear?

Original comment by cgdec...@gmail.com on 6 Sep 2012 at 4:05

GoogleCodeExporter commented 9 years ago
What method are you talking about? The javadocs only mention two methods that I 
can see, neither of which would remove the path and return the file name 
including extension: getNameWithoutExtension, and getFileExtension.  From what 
I can tell neither of these could be used.

It's a side issue, but yes, I was actually going to suggest you use consistent 
parameter names.  I didn't only because I figured it was too pedantic.  :)

Original comment by glenview...@gmail.com on 6 Sep 2012 at 4:19

GoogleCodeExporter commented 9 years ago
Hm... I clearly just saw "getNameWithoutExtension" there for some reason. Need 
more coffee. =)

Anyway, we had a similar proposal come up internally recently. My opinion is 
that it doesn't really provide any value over "new File(fullName).getName()".

Original comment by cgdec...@gmail.com on 6 Sep 2012 at 4:30

GoogleCodeExporter commented 9 years ago
There is a getNameWithoutExtension().  Just not a getNameWithoutPath().  
Perhaps sleep would be a better substitute for coffee :)

I forgot that File() already had such a facility and agree that it's 
preferable--consider the request withdrawn.

Original comment by glenview...@gmail.com on 6 Sep 2012 at 4:38

GoogleCodeExporter commented 9 years ago
(Yes, I know that getNameWithoutExtension exists. =) I just misread and took 
this as a request for it to support full paths in the argument, which it of 
course already does.)

Original comment by cgdec...@gmail.com on 6 Sep 2012 at 4:46

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

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

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

GoogleCodeExporter commented 9 years ago

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