How to trim a file extension from a String in Java

2019-01-16 01:53发布

For example, assuming that x = filename.jpg, I want to get filename, where filename could be any file name (Let's assume the file name only contains [a-zA-Z0-9-_] to simplify.).

I saw x.substring(0, x.indexOf('.jpg')) on DZone Snippets, but wouldn't x.substring(0, x.length-4) perform better? Because, length is a property and doesn't do character checking whereas indexOf() is a function and does character checking.

24条回答
\"骚年 ilove
2楼-- · 2019-01-16 02:09

If you have to process a variable that contains the complete path (ex.: thePath = "http://stackoverflow.com/directory/subdirectory/filename.jpg") and you want to return just "filename" you can use:

theName = thePath.split("/").slice(-1).join().split(".").shift();

the result will be theName == "filename";

To try it write the following command into the console window of your chrome debugger: window.location.pathname.split("/").slice(-1).join().split(".").shift()

If you have to process just the file name and its extension (ex.: theNameWithExt = "filename.jpg"):

theName = theNameWithExt.split(".").shift();

the result will be theName == "filename", the same as above;

Notes:

  1. The first one is a little bit slower cause performes more operations; but works in both cases, in other words it can extract the file name without extension from a given string that contains a path or a file name with ex. While the second works only if the given variable contains a filename with ext like filename.ext but is a little bit quicker.
  2. Both solutions work for both local and server files;

But I can't say nothing about neither performances comparison with other answers nor for browser or OS compatibility.

working snippet 1: the complete path

var thePath = "http://stackoverflow.com/directory/subdirectory/filename.jpg";
theName = thePath.split("/").slice(-1).join().split(".").shift();
alert(theName);
  

working snippet 2: the file name with extension

var theNameWithExt = "filename.jpg";
theName = theNameWithExt.split("/").slice(-1).join().split(".").shift();
alert(theName);
  

working snippet 2: the file name with double extension

var theNameWithExt = "filename.tar.gz";
theName = theNameWithExt.split("/").slice(-1).join().split(".").shift();
alert(theName);
  

查看更多
Anthone
3楼-- · 2019-01-16 02:10

This is where regular expressions come in handy! Javascript's .replace() method will take a regular expression, and you can utilize that to accomplish what you want:

// assuming var x = filename.jpg or some extension
x = x.replace(/(.*)\.[^.]+$/, "$1");
查看更多
唯我独甜
4楼-- · 2019-01-16 02:11

Here's another regex-based solution:

filename.replace(/\.[^.$]+$/, '');

This should only chop off the last segment.

查看更多
男人必须洒脱
5楼-- · 2019-01-16 02:12

You can perhaps use the assumption that the last dot will be the extension delimiter.

var x = 'filename.jpg';
var f = x.substr(0, x.lastIndexOf('.'));

If file has no extension, it will return empty string. To fix that use this function

function removeExtension(filename){
    var lastDotPosition = filename.lastIndexOf(".");
    if (lastDotPosition === -1) return filename;
    else return filename.substr(0, lastDotPosition);
}
查看更多
来,给爷笑一个
6楼-- · 2019-01-16 02:15

In Node.js versions prior to 0.12.x:

path.basename(filename, path.extname(filename))

Of course this also works in 0.12.x and later.

查看更多
等我变得足够好
7楼-- · 2019-01-16 02:16

I like this one because it is a one liner which isn't too hard to read:

filename.substring(0, filename.lastIndexOf('.')) || filename
查看更多
登录 后发表回答