- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Java Program to strip a filename of its extension after the last dot
The method removeExtension() is used to strip a filename of its extension after the last dot. This method requires a single parameter i.e. the file name and it returns the file name without its extension.
A program that demonstrates this is given as follows −
Example
import java.io.File; public class Demo { public static String removeExtension(String fname) { int pos = fname.lastIndexOf('.'); if(pos > -1) return fname.substring(0, pos); else return fname; } public static void main(String[] args) { System.out.println(removeExtension("c:\JavaProgram\demo1.txt")); } }
The output of the above program is as follows −
Output
c:\JavaProgram\demo1
Now let us understand the above program.
The method removeExtension() is used to strip a filename of its extension after the last dot. A code snippet that demonstrates this is given as follows −
public static String removeExtension(String fname) { int pos = fname.lastIndexOf('.'); if(pos > -1) return fname.substring(0, pos); else return fname; }
The method main() prints the file name without the extension that was returned by the method removeExtension(). A code snippet that demonstrates this is given as follows −
public static void main(String[] args) { System.out.println(removeExtension("c:\JavaProgram\demo1.txt")); }
- Related Articles
- Fetch the substring after last dot in MySQL
- Java Program to remove file information from a filename returning only its path component
- Java Program to remove path information from a filename returning only its file component
- Insert a word before the file name’s dot extension in JavaScript?
- Strip last two characters of a column in MySQL?
- Java Program to split a string with dot
- How to get the last dirname/filename in a file path argument in Bash?
- Get an Absolute Filename Path from a Relative Filename Path in Java
- C# program to get the extension of a file in C#
- Get an Absolute Filename Path from a Relative Filename with Path in Java
- Golang program to get the file extension
- Java Program to return a Date set to the last possible millisecond of the minute
- Java Program to search for last index of a group of characters
- C++ Program to Remove the Edges in a Given Cyclic Graph such that its Linear Extension can be Found
- C++ Program to count operations to make filename valid

Advertisements