From 9b413139e15181461b1dea5be537e046df3a60f1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=B3=96=E7=8C=AB=E7=8C=AB?= Date: Fri, 26 Jan 2024 07:06:13 +0000 Subject: [PATCH] =?UTF-8?q?add=20=E5=AE=9E=E7=94=A8=E5=B0=8F=E5=B7=A5?= =?UTF-8?q?=E5=85=B7/StringUtils.java.=20=E5=8F=AF=E4=BB=A5=E6=96=B9?= =?UTF-8?q?=E4=BE=BF=E7=9A=84=E5=AF=B9=E5=AD=97=E7=AC=A6=E4=B8=B2=E8=BF=9B?= =?UTF-8?q?=E8=A1=8C=E6=8F=92=E5=80=BC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: 糖猫猫 --- .../StringUtils.java" | 82 +++++++++++++++++++ 1 file changed, 82 insertions(+) create mode 100644 "\345\256\236\347\224\250\345\260\217\345\267\245\345\205\267/StringUtils.java" diff --git "a/\345\256\236\347\224\250\345\260\217\345\267\245\345\205\267/StringUtils.java" "b/\345\256\236\347\224\250\345\260\217\345\267\245\345\205\267/StringUtils.java" new file mode 100644 index 0000000..50d325e --- /dev/null +++ "b/\345\256\236\347\224\250\345\260\217\345\267\245\345\205\267/StringUtils.java" @@ -0,0 +1,82 @@ +package com.tang.commons.utils; + +import java.util.Objects; + +/** + * 字符串工具类 + * + * @see https://gitee.com/tangllty/tang-boot/blob/master/tang-commons/src/main/java/com/tang/commons/utils/StringUtils.java + * @author Tang + */ +public class StringUtils { + + private StringUtils() { + } + + /** + * 默认占位符 + */ + private static final String DEFAULT_PLACE_HOLDER = "{}"; + + /** + * 格式化字符串 + * + * @param format 模板 + * @param args 参数列表 + * @return 结果 + */ + public static String format(String format, Object... args) { + if (Objects.isNull(format) || Objects.isNull(args) || args.length == 0) { + return Objects.isNull(format) ? "" : format; + } + return formatWith(format, DEFAULT_PLACE_HOLDER, args); + } + + /** + * 格式化字符串 + * + * @param format 模板 + * @param placeHolder 占位符 + * @param args 参数列表 + * @return 结果 + */ + public static String formatWith(String format, String placeHolder, Object... args) { + if (Objects.isNull(format) || Objects.isNull(placeHolder) || Objects.isNull(args) || args.length == 0) { + return Objects.isNull(format) ? "" : format; + } + + var formatLength = format.length(); + var placeHolderLength = placeHolder.length(); + + var stringBuilder = new StringBuilder(formatLength << 1); + + // 记录已经处理到的位置 + var placeHolderIndex = 0; + // 占位符所在位置 + var placeHolderPosition = -1; + + for (Object arg : args) { + placeHolderPosition = format.indexOf(placeHolder, placeHolderIndex); + // 剩余部分无占位符 + if (placeHolderPosition == -1) { + // 不带占位符的模板直接返回 + if (placeHolderIndex == 0) { + return format; + } + // 字符串模板剩余部分不再包含占位符,加入剩余部分后返回结果 + stringBuilder.append(format, placeHolderIndex, formatLength); + return stringBuilder.toString(); + } + + stringBuilder.append(format, placeHolderIndex, placeHolderPosition); + stringBuilder.append(arg); + placeHolderIndex = placeHolderPosition + placeHolderLength; + } + + // 加入最后一个占位符后所有的字符 + stringBuilder.append(format, placeHolderIndex, formatLength); + + return stringBuilder.toString(); + } + +} -- Gitee