| 26 | public static SEPARATOR = '/'; |
| 27 | |
| 28 | public static join(...paths: Nullable<string>[]): string { |
| 29 | // 过滤掉空字符串 |
| 30 | const filteredPaths:string[] = (paths |
| 31 | .filter(path => StrUtil.isNotBlank(path)) as string[]); |
| 32 | |
| 33 | if (filteredPaths.length === 0) { |
| 34 | return ''; |
| 35 | } |
| 36 | |
| 37 | // 判断第一个路径是否为绝对路径(以分隔符开头) |
| 38 | const isAbsolutePath = filteredPaths[0].startsWith(PathUtil.SEPARATOR); |
| 39 | |
| 40 | // 去除所有路径段的首尾分隔符 |
| 41 | const processedPaths = filteredPaths.map(path => |
| 42 | path.replace(new RegExp(`^${PathUtil.SEPARATOR}+|${PathUtil.SEPARATOR}+$`, 'g'), '') |
| 43 | ); |
| 44 | |
| 45 | // 使用分隔符连接所有处理过的路径段 |
| 46 | let joinedPath = processedPaths.join(PathUtil.SEPARATOR); |
| 47 | |
| 48 | // 如果第一个路径是绝对路径,确保结果以分隔符开头 |
| 49 | if (isAbsolutePath) { |
| 50 | joinedPath = PathUtil.SEPARATOR + joinedPath; |
| 51 | } |
| 52 | |
| 53 | return joinedPath; |
| 54 | } |
| 55 | |
| 56 | /** |
| 57 | * 解析一个路径字符串,返回包含目录、文件名、扩展名的信息 |