如何从字符串路径获取文件扩展名

问题描述 投票:1回答:1

我已经将文件路径保存在变量中,并且我想通过使用路径包https://pub.dev/packages/path获得文件类型的扩展名,到目前为止,我设法通过像这样分割字符串来做到这一点

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

使用路径包有什么方法可以实现这一点吗?

flutter dart
1个回答
0
投票

您可以使用extension function中的extension从文件路径获取扩展名:

path package

如果文件具有多个扩展名,例如path,则可以指定可选的import 'package:path/path.dart' as p; final path = '/some/path/to/file/file.dart'; final extensions = p.extension(path); // '.dart' 参数:

file.dart.js
© www.soinside.com 2019 - 2024. All rights reserved.