guoshengxiong 82cf215ab3 风险点、危险源、月度、隐患排查、排查执行
风险点、危险源、月度、隐患排查、排查执行
2025-06-13 18:28:46 +08:00

46 lines
1.5 KiB
Java
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package com.zhgd.xmgl.util;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
public class StrUtils {
/**
* 去掉前面第几个/取后面部分a/b/c/d...写一个方法得到c/d....
* 例如:
* - 输入 "a/b/c/d",返回 "c/d"
* - 输入 "single", "/", "", null返回 null
*
* @param path 输入的路径字符串
* @param cutNum 去掉前面第几个/
*/
public static String getLastSegments(String path, int cutNum) {
// 1. 处理 null 或空字符串
if (StrUtil.isBlank(path)) {
return null;
}
// 2. 分割路径并过滤空字符串
// StrUtil.split(path, '/') 会根据斜杠分割。
// 例如: "/a/b" -> ["", "a", "b"]
// "a/b/" -> ["a", "b", ""]
// "a//b" -> ["a", "", "b"]
// 我们需要过滤掉这些空字符串,只保留有效的路径段。
List<String> validSegments = Arrays.stream(path.split("/"))
.filter(StrUtil::isNotBlank) // 过滤掉空字符串和null
.collect(Collectors.toList());
// 3. 检查有效路径段的数量
if (validSegments.size() < cutNum) {
// 如果不足两部分,则无法形成 "c/d" 结构
return null;
}
// 5. 拼接结果
return String.join("/", CollUtil.sub(validSegments, cutNum, validSegments.size()));
}
}