32 lines
920 B
TypeScript
32 lines
920 B
TypeScript
|
|
/**
|
|||
|
|
* 获取文件的后缀名
|
|||
|
|
* @param fileName 文件名
|
|||
|
|
* @returns 文件后缀名
|
|||
|
|
*/
|
|||
|
|
export function getFileExt(fileName: string): string {
|
|||
|
|
const index = fileName.lastIndexOf(".");
|
|||
|
|
if (index === -1) {
|
|||
|
|
return "";
|
|||
|
|
}
|
|||
|
|
return fileName.substring(index + 1);
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
/**
|
|||
|
|
* 获取文件名
|
|||
|
|
* 该函数接收一个表示文件路径的字符串作为参数,返回该路径中文件名部分。
|
|||
|
|
* 首先查找路径中最后一个斜杠或反斜杠的索引,若未找到则直接返回路径;
|
|||
|
|
* 若找到,则使用substring方法提取最后一个斜杠或反斜杠后面的字符串作为文件名并返回。
|
|||
|
|
* @param path 文件路径
|
|||
|
|
* @returns 文件名
|
|||
|
|
*/
|
|||
|
|
export function getFileName(path: string): string {
|
|||
|
|
let index = path.lastIndexOf("/");
|
|||
|
|
if (index === -1) {
|
|||
|
|
index = path.lastIndexOf("\\");
|
|||
|
|
if (index === -1) {
|
|||
|
|
return path;
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
return path.substring(index + 1);
|
|||
|
|
}
|