Java - 字符串格式化

字符串格式化

package com.demon.common.core.text;

import com.demon.common.utils.StringUtils;

/**
 * 字符串格式化
 *
 * @author Demon-HY
 */
public class StrFormatter {

    private static final String EMPTY_JSON = "{}";

    /**
     * 格式化字符串<br>
     * 此方法简单将占位符 {} 按照顺序替换为参数,不支持直接打印 {}
     *
     * @param strPattern 字符串模板
     * @param args   参数列表
     * @return 结果
     */
    public static String format(final String strPattern, final Object... args) {
        if (StringUtils.isEmpty(strPattern) || StringUtils.isEmpty(args)) {
            return strPattern;
        }
        final int strPatternLength = strPattern.length();

        // 初始化定义好的长度以获得更好的性能
        StringBuilder sbuf = new StringBuilder(strPatternLength + 50);

        int handledPosition = 0;
        int delimIndex;// 占位符所在位置
        for (Object o : args) {
            delimIndex = strPattern.indexOf(EMPTY_JSON, handledPosition);
            if (delimIndex == -1) {
                if (handledPosition == 0) {
                    return strPattern;
                } else { // 字符串模板剩余部分不再包含占位符,加入剩余部分后返回结果
                    sbuf.append(strPattern, handledPosition, strPatternLength);
                    return sbuf.toString();
                }
            } else {
                // 正常占位符
                sbuf.append(strPattern, handledPosition, delimIndex);
                sbuf.append(Convert.utf8Str(o));
                handledPosition = delimIndex + 2;
            }
        }
        // 加入最后一个占位符后所有的字符
        sbuf.append(strPattern, handledPosition, strPattern.length());

        return sbuf.toString();
    }

    public static void main(String[] args) {
        System.out.println(format("test: {} {}", "a", "b"));
    }
}

里面用到的 StringUtils 工具类

package com.demon.common.utils;

import java.util.Collection;
import java.util.Map;

/**
 * 字符串工具类
 *
 * @author Demon-HY
 */
public class StringUtils extends org.apache.commons.lang3.StringUtils {

    /**
     * 判断对象是否为空,包含 String,Collection,Map
     */
    public static boolean isEmpty(Object obj) {
        if (obj == null) {
            return false;
        }

        if (obj instanceof String) {
            String str = (String) obj;
            return StringUtils.isBlank(str.trim());
        }
        if (obj instanceof Collection) {
            Collection coll = (Collection) obj;
            return coll.size() == 0;
        }
        if (obj instanceof Integer) {
            int value = (int) obj;
            // 对象为数组
            return value == 0;
        }
        if (obj instanceof Long) {
            long value = (long) obj;
            // 对象为数组
            return value == 0;
        }
        if (obj instanceof Map) {
            // 对象为Map
            Map map = (Map) obj;
            return map.size() == 0;
        }


        // 其他类型,只要不为null,即不为empty
        return false;
    }

    /**
     * 判断对象是否不为空,包含 String,Collection,Map
     */
    public static boolean isNotEmpty(Object obj) {
        return !isEmpty(obj);
    }
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Demon-HY

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值