Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get the File Extension from a string Path

Tags:

flutter

dart

I've got file path saved in variable and I want to get the file type extension by using path package https://pub.dev/packages/path So far I managed to do it by splitting the string like this

final path = "/some/path/to/file/file.dart";
print(path.split(".").last); //prints dart

Is there any way to achieve this with path package?

like image 325
delmin Avatar asked Jun 13 '20 10:06

delmin


People also ask

How do I get the file extension of an input type file?

The full filename is first obtained by selecting the file input and getting its value property. This returns the filename as a string. By the help of split() method, we will split the filename into 2 parts. The first part will be the filename and the second part will be the extension of the file.

How do I get file extension from path in flutter?

Implementation. String extension(String path, [int level = 1]) => context. extension(path, level); Flutter.


2 Answers

You can use the extension function in the path package to get the extension from a file path:

import 'package:path/path.dart' as p;

final path = '/some/path/to/file/file.dart';

final extension = p.extension(path); // '.dart'

If your file has multiple extensions, like file.dart.js, you can specify the optional level parameter:

final extension = p.extension('file.dart.js', 2); // '.dart.js'
like image 136
creativecreatorormaybenot Avatar answered Oct 04 '22 00:10

creativecreatorormaybenot


No need of any extension. You can try below code snippet.

String getFileExtension(String fileName) {
 return "." + fileName.split('.').last;
}
like image 41
Yash Avatar answered Oct 03 '22 23:10

Yash