java 字符串文本匹配并高亮显示处理

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

/**
 * @author hutf
 * @createTime 2022年07月21日 20:39:00
 */
public class MLightUtils {

    /**
     * @param list 数据源匹配 例如: {"难的","这世界","世界上最", "世界上最难", "最难的职业","意志","坚韧不拔","职业就是","程序员","他们"}
     * @param str 你所要处理的字符串 例如: 世界上最难的职业
     * @param leftTag 左标签字符串 例如:
     * @param rightTag 右标签字符串 例如:
     * @return
     */
    public static String getMRedText(List list, String str,String leftTag,String  rightTag) {
        if (leftTag == null || "".equals(leftTag)) leftTag = "";
        if (rightTag == null || "".equals(rightTag)) rightTag = "";

        List l = new ArrayList<>();

        for (String item : list) {
            if (str.contains(item)) {
                int startIndex = str.indexOf(item);
                int endIndex = startIndex + item.length() - 1;
                l.add(new MLight(startIndex, 0,leftTag));
                l.add(new MLight(endIndex, 1,rightTag));
            }
        }

        if (l.size() == 0) return str;

        Collections.sort(l, (o1, o2) -> {
            if (o1.getIndex().equals(o2.getIndex())) return o2.getType() - o1.getType();
            return o1.getIndex() - o2.getIndex();
        });

        char[] strChars = str.toCharArray();

        StringBuffer sb = new StringBuffer();
        for (int i = 0; i < strChars.length; i++) {
            char item = strChars[i];
            List currentIndexMLights = getCurrentMReds(i, l);
            if (currentIndexMLights.size() == 0) {
                sb.append(item);
                continue;
            }

            for (MLight item0 : currentIndexMLights) {
                if (item0.getType() == 0) sb.append(item0.getTagTxt());
            }

            sb.append(item);

            for (MLight item0 : currentIndexMLights) {
                if (item0.getType() == 1) sb.append(item0.getTagTxt());
            }

        }

        return sb.toString();
    }
    // 获取某个位置下的 高亮对象
    private static List getCurrentMReds(Integer index, List list) {
        List rs = new ArrayList<>();
        for (MLight mLight : list) {
            if (mLight.getIndex().equals(index)) {
                rs.add(mLight);
            }
        }
        return rs;
    }

    // 高亮显示类实体
   static class MLight {
        private Integer index;// 目标字符串的 下标
        private Integer type;// 0代表左边,1代表右边
        private String tagTxt;// 标签文本字符串

        public String getTagTxt() {
            return tagTxt;
        }

        public void setTagTxt(String tagTxt) {
            this.tagTxt = tagTxt;
        }

        public Integer getIndex() {
            return index;
        }

        public void setIndex(Integer index) {
            this.index = index;
        }

        public Integer getType() {
            return type;
        }

        public void setType(Integer type) {
            this.type = type;
        }

        public MLight(Integer index, Integer type,String tagTxt) {
            this.index = index;
            this.type = type;
            this.tagTxt = tagTxt;
        }
    }
}

你可能感兴趣的:(后端,工具,java,servlet,数据结构)