• Post Reply Bookmark Topic Watch Topic
  • New Topic

remove extension from file name  RSS feed

 
Cathy Song
Ranch Hand
Posts: 92
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All,

I can get the name of a file using getName(), but I want to remove the extension

Should I just write a substring to extract a filename, or is there a method in the API to do so.

Name: file.txt
I want to remove the .txt to extract just "file"

This code does the job do for:

[ August 11, 2005: Message edited by: Cathy Song ]
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There are other ways to do it, but there's no "removeExtension" method. Your way is as good as any other.
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You might want lastIndexOf. I have some files that include versions like

BogWater1.3.1.exe
 
Sathya Srinivasan
Ranch Hand
Posts: 379
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The simplest version would be



The assumption is that regardless of how mant dots you have in the file name, the extension is always after the last dot.
 
Stuart Gray
Ranch Hand
Posts: 410
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So to extract the filename using the same approach as Sathya:
 
Cathy Song
Ranch Hand
Posts: 92
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks a lot!

-Cathy
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!