searchusermenu
  • 发布文章
  • 消息中心
点赞
收藏
评论
分享
原创

一种字符串的切片方法

2024-09-05 09:26:35
3
0

概述

   字符串作为无法做算数运算的类型,但是在工作中经常会需要对字符串数组做分块切割,本文介绍一种分块切割方式。

详细说明

  总体流程:

    1. 找出最大和最小的字符串

    2. 计算最大最小值的公共字符串,

    3. 字符串所有的字母作为集合 添加到公共串中,如果该字符串在最大最小值之间,则归入分界点。

    public static List<String> computePoint(String min, String max, int splitCount) {
        Set<Character> keys = new HashSet<>();
        if (min != null) {
            for (int i = 0; i < min.length(); i++) {
                keys.add(min.charAt(i));
            }
        }
        if (max != null) {
            for (int i = 0; i < max.length(); i++) {
                keys.add(max.charAt(i));
            }
        }
  
        keys.remove('_');
        keys.remove('[');
        keys.remove('\\');
        keys.remove(']');
        keys.remove('^');
        keys.remove('`');
        if (keys.size() <= 0 || splitCount <= 0) {
            return null;
        }

        char[] maps = new char[keys.size()];
        int i = 0;
        for (Character key : keys) {
            maps[i++] = key;
        }
        Arrays.sort(maps);

        String base = base(min, max);

        ArrayList<String> seeds = new ArrayList<>();
        Set<String> ids = new HashSet<>();
        for (int j = 0; j < maps.length; j++) {
            String tmp = base + maps[j];
            if (tmp.compareTo(min) > 0 && tmp.compareTo(max) < 0) {
                log.info("split str : {}", tmp);
                seeds.add(tmp);
                ids.add(tmp);
            }
        }

        seeds.clear();
        if (min != null) {
            ids.add(min);
        }
        if (max != null) {
            ids.add(max);
        }
        for (String id : ids) {
            seeds.add(id);
        }
        return seeds;
    }

 

 

0条评论
0 / 1000
卢****雨
5文章数
0粉丝数
卢****雨
5 文章 | 0 粉丝
原创

一种字符串的切片方法

2024-09-05 09:26:35
3
0

概述

   字符串作为无法做算数运算的类型,但是在工作中经常会需要对字符串数组做分块切割,本文介绍一种分块切割方式。

详细说明

  总体流程:

    1. 找出最大和最小的字符串

    2. 计算最大最小值的公共字符串,

    3. 字符串所有的字母作为集合 添加到公共串中,如果该字符串在最大最小值之间,则归入分界点。

    public static List<String> computePoint(String min, String max, int splitCount) {
        Set<Character> keys = new HashSet<>();
        if (min != null) {
            for (int i = 0; i < min.length(); i++) {
                keys.add(min.charAt(i));
            }
        }
        if (max != null) {
            for (int i = 0; i < max.length(); i++) {
                keys.add(max.charAt(i));
            }
        }
  
        keys.remove('_');
        keys.remove('[');
        keys.remove('\\');
        keys.remove(']');
        keys.remove('^');
        keys.remove('`');
        if (keys.size() <= 0 || splitCount <= 0) {
            return null;
        }

        char[] maps = new char[keys.size()];
        int i = 0;
        for (Character key : keys) {
            maps[i++] = key;
        }
        Arrays.sort(maps);

        String base = base(min, max);

        ArrayList<String> seeds = new ArrayList<>();
        Set<String> ids = new HashSet<>();
        for (int j = 0; j < maps.length; j++) {
            String tmp = base + maps[j];
            if (tmp.compareTo(min) > 0 && tmp.compareTo(max) < 0) {
                log.info("split str : {}", tmp);
                seeds.add(tmp);
                ids.add(tmp);
            }
        }

        seeds.clear();
        if (min != null) {
            ids.add(min);
        }
        if (max != null) {
            ids.add(max);
        }
        for (String id : ids) {
            seeds.add(id);
        }
        return seeds;
    }

 

 

文章来自个人专栏
字符串分块
1 文章 | 1 订阅
0条评论
0 / 1000
请输入你的评论
0
0