重写一部分String.format功能

该文章介绍了一个Java类StringFormatUtils,用于实现类似于String.format的功能,特别是在格式化字符串时,如果替换字符不足指定宽度,它会使用中文填充而不是默认的空格。此方法对于处理中英文混合的对齐问题有一定改善,但并非完美解决所有情况。
摘要由CSDN通过智能技术生成
String.format("xxxx%6sxxxxx", "111")//是将中间的%6s进行替换,如果替换字符不足6会填充空格,空格是根据系统来的

改成指定以中文填充

public class StringFormatUtils {

    public static String format(String s, String... args) {
        if (args == null || args.length == 0) {
            return s;
        }
        StringBuffer sb = new StringBuffer();
        Pattern p = Pattern.compile("(%\\d+?s)");
        Matcher matcher = p.matcher(s);
        int index = -1;
        while (matcher.find()) {
            int groupCount = matcher.groupCount();
            if (groupCount < 1) {
                continue;
            }
            String group1 = matcher.group(1);
            Pattern p2 = Pattern.compile("^%(\\d+?)s$");
            Matcher matcher1 = p2.matcher(group1);
            int n = 0;
            if (matcher1.find()) {
                n = Integer.valueOf(matcher1.group(1));
            }
            String replacement = fillSpace(args[++index], n);
            matcher.appendReplacement(sb, replacement);
        }
        matcher.appendTail(sb);
        return sb.toString();
    }

    public static double len(String s) {
        double len = 0;
        for (int i = 0; i < s.length(); i++) {
            String str = s.substring(i, i + 1);
            if (str.matches("[0-9A-Za-z\\.\\+\\-%]")) {
                len += 0.5;
            } else {
                len += 1;
            }
        }
        return len;
    }

    public static String fillSpace(String s, int fullLen) {
        return fillSpace(s, fullLen, true, false);
    }

    public static String fillSpace(String s, int fullLen, boolean debug) {
        return fillSpace(s, fullLen, true, debug);
    }

    public static String fillSpace(String s, int fullLen, boolean left, boolean debug) {
        double len = len(s);
        if (len >= fullLen) {
            return s;
        }
        StringBuilder s1 = new StringBuilder(s);
        while (len(s1.toString()) < fullLen) {
            double diff = fullLen - len(s1.toString());
            String zhspace = debug ? "空" : " ";
            String enspace = debug ? "s" : " ";
            if (diff == 0.5d) {
                if (left) {
                    s1.insert(0, enspace);
                } else {
                    s1.append(enspace);
                }
            } else {
                if (left) {
                    s1.insert(0, zhspace);
                } else {
                    s1.append(zhspace);
                }
            }
        }
        return s1.toString();
    }

测试代码

        System.out.println(StringFormatUtils.format(s, "sss"));
        System.out.println(StringFormatUtils.format(s, "dd你你"));
        System.out.println(StringFormatUtils.format(s, "+10%"));
        System.out.println(StringFormatUtils.format(s, "-150%"));

        System.out.println("--------------------------------");

        System.out.println(String.format(s, "sss"));
        System.out.println(String.format(s, "dd你你"));
        System.out.println(String.format(s, "+10%"));
        System.out.println(String.format(s, "-150%"));

输出

 

 

这里发现改了之后,对齐效果仍然不是特别完美。但是比原生的还是好一点,原生的除非说输入的字符串规则完全一致,否则一旦遇到中英文混杂的情况,对齐效果感观上差异比较大。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值