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.
JavaScript files have the file extension .js.
To remove the extension from a filename using Python, the easiest way is with the os module path. basename() and path. splitext() functions. You can also use the pathlib module and Path and then access the attribute 'stem' to remove the extension from a filename.
You should be using the command substitution syntax $(command) when you want to execute a command in script/command. name=$(echo "$filename" | cut -f 1 -d '.
To delete a file in Node. js, Node FS unlink(path, callback) can be used for asynchronous file operation and unlinkSync(path) can be used for synchronous file operation. In this Node.
Not sure what would perform faster but this would be more reliable when it comes to extension like .jpeg
or .html
x.replace(/\.[^/.]+$/, "")
In node.js, the name of the file without the extension can be obtained as follows.
const path = require('path'); const filename = 'hello.html'; path.parse(filename).name; //=> "hello" path.parse(filename).ext; //=> ".html" path.parse(filename).base; //=> "hello.html"
Further explanation at Node.js documentation page.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With