欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Node.js 使用substr函数从路径截取文件名称(依赖os组件库区分系统)

程序员文章站 2023-12-22 21:31:10
...
// 下载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

欢迎关注转发评论点赞沟通,让编码不在孤单。

上一篇:

下一篇: