如何从字符串路径获取文件扩展名 [英] How to get the File Extension from a string Path

查看:165
本文介绍了如何从字符串路径获取文件扩展名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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?

推荐答案

您可以使用 扩展函数 rel = nofollow noreferrer> path 包,以从文件路径获取扩展名:

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'

如果文件具有多个扩展名,例如 file .dart.js ,您可以指定可选的 level 参数:

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'

这篇关于如何从字符串路径获取文件扩展名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆