Package cn.dev33.satoken.util
Class StrFormatter
java.lang.Object
cn.dev33.satoken.util.StrFormatter
字符串格式化工具,将字符串中的 {} 按序替换为参数
本工具类 copy 自 Hutool: https://github.com/dromara/hutool/blob/v5-master/hutool-core/src/main/java/cn/hutool/core/text/StrFormatter.java
- Since:
- 1.33.0
- Author:
- Looly
-
Field Summary
FieldsModifier and TypeFieldDescriptionstatic charDeprecated.static StringDeprecated.语义不明确,建议内部使用DEFAULT_PLACEHOLDER替代 -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic String格式化字符串
此方法只是简单将占位符 {} 按照顺序替换为参数
如果想输出 {} 使用 \\转义 { 即可,如果想输出 {} 之前的 \ 使用双转义符 \\\\ 即可
例:
通常使用: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 bstatic StringformatWith(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
-
Field Details
-
EMPTY_JSON
Deprecated.语义不明确,建议内部使用DEFAULT_PLACEHOLDER替代占位符(保留原有 public 访问权限,避免破坏外部依赖) -
C_BACKSLASH
Deprecated.命名不规范,建议内部使用BACKSLASH_CHAR替代反斜杠转义字符(保留原有 public 访问权限,避免破坏外部依赖)
-
-
Constructor Details
-
StrFormatter
public StrFormatter()
-
-
Method Details
-
format
格式化字符串
此方法只是简单将占位符 {} 按照顺序替换为参数
如果想输出 {} 使用 \\转义 { 即可,如果想输出 {} 之前的 \ 使用双转义符 \\\\ 即可
例:
通常使用: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
格式化字符串
此方法只是简单将指定占位符 按照顺序替换为参数
如果想输出占位符使用 \\转义即可,如果想输出占位符之前的 \ 使用双转义符 \\\\ 即可
例:
通常使用: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:
- 格式化后的结果
- Since:
- 1.33.0
-
BACKSLASH_CHAR替代