Class StringFormatter

java.lang.Object
org.miaixz.bus.core.text.placeholder.StringFormatter

public class StringFormatter extends Object
字符串格式化工具
Since:
Java 17+
Author:
Kimi Liu
  • Constructor Summary

    Constructors
    Constructor
    Description
     
  • Method Summary

    Modifier and Type
    Method
    Description
    static void
    清空缓存
    static String
    format(CharSequence template, Map<?,?> map, boolean ignoreNull)
    格式化文本,使用 {varName} 占位 map = {a: "aValue", b: "bValue"} format("{a} and {b}", map) = aValue and bValue
    static String
    format(String strPattern, Object... argArray)
    格式化字符串 此方法只是简单将占位符 {} 按照顺序替换为参数 如果想输出 {} 使用 \\转义 { 即可,如果想输出 {} 之前的 \ 使用双转义符 \\\\ 即可 例: 通常使用:format("this is {} for {}", "a", "b") = this is a for b 转义{}: format("this is \\{} for {}", "a", "b") = this is {} for a 转义\: format("this is \\\\{} for {}", "a", "b") = this is \a for b
    static String
    formatWith(String strPattern, String placeHolder, Object... argArray)
    格式化字符串 此方法只是简单将指定占位符 按照顺序替换为参数 如果想输出占位符使用 \\转义即可,如果想输出占位符之前的 \ 使用双转义符 \\\\ 即可 例: 通常使用:format("this is {} for {}", "{}", "a", "b") = this is a for b 转义{}: format("this is \\{} for {}", "{}", "a", "b") = this is {} for a 转义\: format("this is \\\\{} for {}", "{}", "a", "b") = this is \a for b

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Constructor Details

    • StringFormatter

      public StringFormatter()
  • Method Details

    • format

      public static String format(String strPattern, Object... argArray)
      格式化字符串 此方法只是简单将占位符 {} 按照顺序替换为参数 如果想输出 {} 使用 \\转义 { 即可,如果想输出 {} 之前的 \ 使用双转义符 \\\\ 即可 例: 通常使用:format("this is {} for {}", "a", "b") = this is a for b 转义{}: format("this is \\{} for {}", "a", "b") = this is {} for a 转义\: format("this is \\\\{} for {}", "a", "b") = this is \a for b
      Parameters:
      strPattern - 字符串模板
      argArray - 参数列表
      Returns:
      结果
    • formatWith

      public static String formatWith(String strPattern, String placeHolder, Object... argArray)
      格式化字符串 此方法只是简单将指定占位符 按照顺序替换为参数 如果想输出占位符使用 \\转义即可,如果想输出占位符之前的 \ 使用双转义符 \\\\ 即可 例: 通常使用:format("this is {} for {}", "{}", "a", "b") = this is a for b 转义{}: format("this is \\{} for {}", "{}", "a", "b") = this is {} for a 转义\: format("this is \\\\{} for {}", "{}", "a", "b") = this is \a for b
      Parameters:
      strPattern - 字符串模板
      placeHolder - 占位符,例如{}
      argArray - 参数列表
      Returns:
      结果
    • format

      public static String format(CharSequence template, Map<?,?> map, boolean ignoreNull)
      格式化文本,使用 {varName} 占位 map = {a: "aValue", b: "bValue"} format("{a} and {b}", map) = aValue and bValue
      Parameters:
      template - 文本模板,被替换的部分用 {data} 表示
      map - 参数值对
      ignoreNull - 是否忽略 null 值,忽略则 null 值对应的变量不被替换,否则替换为""
      Returns:
      格式化后的文本
    • clear

      public static void clear()
      清空缓存