字符串格式化-StrFormatter

由来

我一直对Slf4j的字符串格式化情有独钟,通过{}这种简单的占位符完成字符串的格式化。于是参考Slf4j的源码,便有了StrFormatter

StrFormatter.format的快捷使用方式为StrUtil.format,推荐使用后者。

使用

  1. //通常使用
  2. String result1 = StrFormatter.format("this is {} for {}", "a", "b");
  3. Assert.assertEquals("this is a for b", result1);
  4. //转义{}
  5. String result2 = StrFormatter.format("this is \\{} for {}", "a", "b");
  6. Assert.assertEquals("this is {} for a", result2);
  7. //转义\
  8. String result3 = StrFormatter.format("this is \\\\{} for {}", "a", "b");
  9. Assert.assertEquals("this is \\a for b", result3);