This commit is contained in:
Looly 2021-08-17 16:14:28 +08:00
parent 83cbd4ea20
commit 0060d5ebbb
3 changed files with 61 additions and 44 deletions

View File

@ -3,6 +3,8 @@ package cn.hutool.core.text;
import cn.hutool.core.util.ArrayUtil; import cn.hutool.core.util.ArrayUtil;
import cn.hutool.core.util.StrUtil; import cn.hutool.core.util.StrUtil;
import java.util.Map;
/** /**
* 字符串格式化工具 * 字符串格式化工具
* *
@ -73,4 +75,34 @@ public class StrFormatter {
return sbuf.toString(); return sbuf.toString();
} }
/**
* 格式化文本使用 {varName} 占位<br>
* map = {a: "aValue", b: "bValue"} format("{a} and {b}", map) ---= aValue and bValue
*
* @param template 文本模板被替换的部分用 {key} 表示
* @param map 参数值对
* @param ignoreNull 是否忽略 {@code null} 忽略则 {@code null} 值对应的变量不被替换否则替换为""
* @return 格式化后的文本
* @since 5.7.10
*/
public static String format(CharSequence template, Map<?, ?> map, boolean ignoreNull) {
if (null == template) {
return null;
}
if (null == map || map.isEmpty()) {
return template.toString();
}
String template2 = template.toString();
String value;
for (Map.Entry<?, ?> entry : map.entrySet()) {
value = StrUtil.utf8Str(entry.getValue());
if (null == value && ignoreNull) {
continue;
}
template2 = StrUtil.replace(template2, "{" + entry.getKey() + "}", value);
}
return template2;
}
} }

View File

@ -2,6 +2,7 @@ package cn.hutool.core.util;
import cn.hutool.core.text.CharSequenceUtil; import cn.hutool.core.text.CharSequenceUtil;
import cn.hutool.core.text.StrBuilder; import cn.hutool.core.text.StrBuilder;
import cn.hutool.core.text.StrFormatter;
import cn.hutool.core.text.StrPool; import cn.hutool.core.text.StrPool;
import cn.hutool.core.text.TextSimilarity; import cn.hutool.core.text.TextSimilarity;
@ -454,22 +455,6 @@ public class StrUtil extends CharSequenceUtil implements StrPool {
* @since 5.4.3 * @since 5.4.3
*/ */
public static String format(CharSequence template, Map<?, ?> map, boolean ignoreNull) { public static String format(CharSequence template, Map<?, ?> map, boolean ignoreNull) {
if (null == template) { return StrFormatter.format(template, map, ignoreNull);
return null;
}
if (null == map || map.isEmpty()) {
return template.toString();
}
String template2 = template.toString();
String value;
for (Map.Entry<?, ?> entry : map.entrySet()) {
value = utf8Str(entry.getValue());
if (null == value && ignoreNull) {
continue;
}
template2 = replace(template2, "{" + entry.getKey() + "}", value);
}
return template2;
} }
} }

View File

@ -14,7 +14,7 @@ import java.util.HashMap;
/** /**
* 二维码设置 * 二维码设置
* *
* @author looly * @author looly
* @since 4.1.2 * @since 4.1.2
*/ */
@ -33,7 +33,7 @@ public class QrConfig {
protected Integer backColor = WHITE; protected Integer backColor = WHITE;
/** 边距1~4 */ /** 边距1~4 */
protected Integer margin = 2; protected Integer margin = 2;
/** 设置二维码中的信息量,可设置1-40的整数 */ /** 设置二维码中的信息量,可设置0-40的整数 */
protected Integer qrVersion; protected Integer qrVersion;
/** 纠错级别 */ /** 纠错级别 */
protected ErrorCorrectionLevel errorCorrection = ErrorCorrectionLevel.M; protected ErrorCorrectionLevel errorCorrection = ErrorCorrectionLevel.M;
@ -43,7 +43,7 @@ public class QrConfig {
protected Image img; protected Image img;
/** 二维码中的Logo缩放的比例系数如5表示长宽最小值的1/5 */ /** 二维码中的Logo缩放的比例系数如5表示长宽最小值的1/5 */
protected int ratio = 6; protected int ratio = 6;
/** /**
* 创建QrConfig * 创建QrConfig
* @return QrConfig * @return QrConfig
@ -62,7 +62,7 @@ public class QrConfig {
/** /**
* 构造 * 构造
* *
* @param width * @param width
* @param height * @param height
*/ */
@ -73,7 +73,7 @@ public class QrConfig {
/** /**
* 获取宽度 * 获取宽度
* *
* @return 宽度 * @return 宽度
*/ */
public int getWidth() { public int getWidth() {
@ -82,7 +82,7 @@ public class QrConfig {
/** /**
* 设置宽度 * 设置宽度
* *
* @param width 宽度 * @param width 宽度
* @return this * @return this
*/ */
@ -93,7 +93,7 @@ public class QrConfig {
/** /**
* 获取高度 * 获取高度
* *
* @return 高度 * @return 高度
*/ */
public int getHeight() { public int getHeight() {
@ -102,7 +102,7 @@ public class QrConfig {
/** /**
* 设置高度 * 设置高度
* *
* @param height 高度 * @param height 高度
* @return this; * @return this;
*/ */
@ -113,7 +113,7 @@ public class QrConfig {
/** /**
* 获取前景色 * 获取前景色
* *
* @return 前景色 * @return 前景色
*/ */
public int getForeColor() { public int getForeColor() {
@ -122,7 +122,7 @@ public class QrConfig {
/** /**
* 设置前景色例如Color.BLUE.getRGB() * 设置前景色例如Color.BLUE.getRGB()
* *
* @param foreColor 前景色 * @param foreColor 前景色
* @return this * @return this
* @deprecated 请使用 {@link #setForeColor(Color)} * @deprecated 请使用 {@link #setForeColor(Color)}
@ -149,7 +149,7 @@ public class QrConfig {
/** /**
* 获取背景色 * 获取背景色
* *
* @return 背景色 * @return 背景色
*/ */
public int getBackColor() { public int getBackColor() {
@ -158,7 +158,7 @@ public class QrConfig {
/** /**
* 设置背景色例如Color.BLUE.getRGB() * 设置背景色例如Color.BLUE.getRGB()
* *
* @param backColor 背景色 * @param backColor 背景色
* @return this * @return this
* @deprecated 请使用 {@link #setBackColor(Color)} * @deprecated 请使用 {@link #setBackColor(Color)}
@ -187,7 +187,7 @@ public class QrConfig {
/** /**
* 获取边距 * 获取边距
* *
* @return 边距 * @return 边距
*/ */
public Integer getMargin() { public Integer getMargin() {
@ -196,7 +196,7 @@ public class QrConfig {
/** /**
* 设置边距 * 设置边距
* *
* @param margin 边距 * @param margin 边距
* @return this * @return this
*/ */
@ -227,7 +227,7 @@ public class QrConfig {
/** /**
* 获取纠错级别 * 获取纠错级别
* *
* @return 纠错级别 * @return 纠错级别
*/ */
public ErrorCorrectionLevel getErrorCorrection() { public ErrorCorrectionLevel getErrorCorrection() {
@ -236,7 +236,7 @@ public class QrConfig {
/** /**
* 设置纠错级别 * 设置纠错级别
* *
* @param errorCorrection 纠错级别 * @param errorCorrection 纠错级别
* @return this * @return this
*/ */
@ -247,7 +247,7 @@ public class QrConfig {
/** /**
* 获取编码 * 获取编码
* *
* @return 编码 * @return 编码
*/ */
public Charset getCharset() { public Charset getCharset() {
@ -256,7 +256,7 @@ public class QrConfig {
/** /**
* 设置编码 * 设置编码
* *
* @param charset 编码 * @param charset 编码
* @return this * @return this
*/ */
@ -267,26 +267,26 @@ public class QrConfig {
/** /**
* 获取二维码中的Logo * 获取二维码中的Logo
* *
* @return Logo图片 * @return Logo图片
*/ */
public Image getImg() { public Image getImg() {
return img; return img;
} }
/** /**
* 设置二维码中的Logo文件 * 设置二维码中的Logo文件
* *
* @param imgPath 二维码中的Logo路径 * @param imgPath 二维码中的Logo路径
* @return this; * @return this;
*/ */
public QrConfig setImg(String imgPath) { public QrConfig setImg(String imgPath) {
return setImg(FileUtil.file(imgPath)); return setImg(FileUtil.file(imgPath));
} }
/** /**
* 设置二维码中的Logo文件 * 设置二维码中的Logo文件
* *
* @param imgFile 二维码中的Logo * @param imgFile 二维码中的Logo
* @return this; * @return this;
*/ */
@ -296,7 +296,7 @@ public class QrConfig {
/** /**
* 设置二维码中的Logo * 设置二维码中的Logo
* *
* @param img 二维码中的Logo * @param img 二维码中的Logo
* @return this; * @return this;
*/ */
@ -307,7 +307,7 @@ public class QrConfig {
/** /**
* 获取二维码中的Logo缩放的比例系数如5表示长宽最小值的1/5 * 获取二维码中的Logo缩放的比例系数如5表示长宽最小值的1/5
* *
* @return 二维码中的Logo缩放的比例系数如5表示长宽最小值的1/5 * @return 二维码中的Logo缩放的比例系数如5表示长宽最小值的1/5
*/ */
public int getRatio() { public int getRatio() {
@ -316,7 +316,7 @@ public class QrConfig {
/** /**
* 设置二维码中的Logo缩放的比例系数如5表示长宽最小值的1/5 * 设置二维码中的Logo缩放的比例系数如5表示长宽最小值的1/5
* *
* @param ratio 二维码中的Logo缩放的比例系数如5表示长宽最小值的1/5 * @param ratio 二维码中的Logo缩放的比例系数如5表示长宽最小值的1/5
* @return this; * @return this;
*/ */
@ -327,7 +327,7 @@ public class QrConfig {
/** /**
* 转换为Zxing的二维码配置 * 转换为Zxing的二维码配置
* *
* @return 配置 * @return 配置
*/ */
public HashMap<EncodeHintType, Object> toHints() { public HashMap<EncodeHintType, Object> toHints() {