首页  

string format几种方式     所属分类 java 浏览量 627
两种方式
使用 jdk String.format      功能强大 使用正则解析占位符 但是性能差一些
使用 slf4j MessageFormatter 使用 indexOf 解析占位符 然后拼接参数


import java.time.LocalDateTime;
import org.slf4j.helpers.MessageFormatter;

public class StringFormatTest {

    public static void main(String[] args) throws Exception {
        String now = LocalDateTime.now().toString();
        String str = "now time is {}";
        str = format(str, now);
        System.out.println(str);

        str = "now time is %s";
        str = String.format(str, now);
        System.out.println(str);
    }

    private static String format(String format, Object... args) {
        return MessageFormatter.arrayFormat(format, args).getMessage();
    }
}


slf4j为什么用{}而不是%s

上一篇     下一篇
springboot2 集成 prometheus

springboot 加载指定的 properties文件

Java8 Stream过滤null值

字符串格式化性能对比

单元测试 AIR 和 FIRST 原则

SimpleDateFormat并发问题实例演示