我已经将文件路径保存在变量中,并且我想通过使用路径包https://pub.dev/packages/path来获取文件类型扩展名到目前为止,我设法通过分割字符串来做到这一点
final path = "/some/path/to/file/file.dart";
print(path.split(".").last); //prints dart
有什么办法可以通过路径包来实现?
您可以使用该extension
功能在path
包获取某个文件路径的扩展:
import 'package:path/path.dart' as p;
final path = '/some/path/to/file/file.dart';
final extension = p.extension(path); // '.dart'
如果文件具有多个扩展名(如)file.dart.js
,则可以指定可选level
参数:
final extension = p.extension('file.dart.js', 2); // '.dart.js'
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句