// 下载os组件库 代码可以直接使用
npm install os --save
// 引入os依赖
const os = require('os')
// 函数 lastIndexOf 获取到最后斜杠出现的位置
let filePath = 'D:\\temp\\test.txt';
let index = 0;
if(os.platform().toString() === 'win32'){
index = filePath.lastIndexOf('\\');
}else if(os.platform().toString() === 'linux'){
index = filePath.lastIndexOf('/');
}
// 进行截取 substr
let fileName = filePath.substr(index+1);
// 打印到控制台
console.log('文件名称是',fileName);
微笑的java
欢迎关注转发评论点赞沟通,让编码不在孤单。
该博客介绍了如何在Node.js环境中,根据操作系统平台(Windows或Linux)的不同,使用`os`模块来确定文件路径中最后一个斜杠的位置,并截取文件名。示例代码展示了在Windows上查找和在Linux上查找/的方法,最终打印出文件名称。
2164

被折叠的 条评论
为什么被折叠?



