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.StrUtil;
import java.util.Map;
/**
* 字符串格式化工具
*
@ -73,4 +75,34 @@ public class StrFormatter {
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.StrBuilder;
import cn.hutool.core.text.StrFormatter;
import cn.hutool.core.text.StrPool;
import cn.hutool.core.text.TextSimilarity;
@ -454,22 +455,6 @@ public class StrUtil extends CharSequenceUtil implements StrPool {
* @since 5.4.3
*/
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 = utf8Str(entry.getValue());
if (null == value && ignoreNull) {
continue;
}
template2 = replace(template2, "{" + entry.getKey() + "}", value);
}
return template2;
return StrFormatter.format(template, map, ignoreNull);
}
}

View File

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