diff --git a/CHANGELOG.md b/CHANGELOG.md
index 080b4b39f..fbb06704a 100755
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -2,12 +2,13 @@
# 🚀Changelog
-------------------------------------------------------------------------------------------------------------
-# 5.8.27(2024-03-06)
+# 5.8.27(2024-03-07)
### 🐣新特性
* 【extra 】 FreemarkerEngine修改默认版本参数
* 【db 】 增加达梦数据库方言(pr#1178@Gitee)
* 【core 】 HexUtil#format方法增加prefix参数(issue#I93PU9@Gitee)
+* 【core 】 StrUtil.replace歧义,修改为replaceByCodePoint(issue#I96LWH@Gitee)
### 🐞Bug修复
* 【core 】 修复PathMover对目标已存在且只读文件报错错误问题(issue#I95CLT@Gitee)
diff --git a/hutool-core/src/main/java/cn/hutool/core/text/CharSequenceUtil.java b/hutool-core/src/main/java/cn/hutool/core/text/CharSequenceUtil.java
index 6b48d9120..90439c61b 100755
--- a/hutool-core/src/main/java/cn/hutool/core/text/CharSequenceUtil.java
+++ b/hutool-core/src/main/java/cn/hutool/core/text/CharSequenceUtil.java
@@ -16,10 +16,7 @@ import java.nio.ByteBuffer;
import java.nio.charset.Charset;
import java.text.MessageFormat;
import java.text.Normalizer;
-import java.util.HashSet;
-import java.util.LinkedList;
-import java.util.List;
-import java.util.Set;
+import java.util.*;
import java.util.function.Function;
import java.util.function.Predicate;
@@ -2461,9 +2458,7 @@ public class CharSequenceUtil {
}
char[] result = new char[count];
- for (int i = 0; i < count; i++) {
- result[i] = c;
- }
+ Arrays.fill(result, c);
return new String(result);
}
@@ -3536,7 +3531,7 @@ public class CharSequenceUtil {
if (str == null || isEmpty(prefix) || startWith(str, prefix, ignoreCase)) {
return str(str);
}
- if (prefixes != null && prefixes.length > 0) {
+ if (prefixes != null) {
for (final CharSequence s : prefixes) {
if (startWith(str, s, ignoreCase)) {
return str.toString();
@@ -3650,8 +3645,25 @@ public class CharSequenceUtil {
* @param replacedChar 被替换的字符
* @return 替换后的字符串
* @since 3.2.1
+ * @deprecated 歧义,请使用{@link #replaceByCodePoint(CharSequence, int, int, char)}
*/
+ @Deprecated
public static String replace(CharSequence str, int startInclude, int endExclude, char replacedChar) {
+ return replaceByCodePoint(str, startInclude, endExclude, replacedChar);
+ }
+
+ /**
+ * 替换指定字符串的指定区间内字符为固定字符
+ * 此方法使用{@link String#codePoints()}完成拆分替换
+ *
+ * @param str 字符串
+ * @param startInclude 开始位置(包含)
+ * @param endExclude 结束位置(不包含)
+ * @param replacedChar 被替换的字符
+ * @return 替换后的字符串
+ * @since 5.8.27
+ */
+ public static String replaceByCodePoint(CharSequence str, int startInclude, int endExclude, char replacedChar) {
if (isEmpty(str)) {
return str(str);
}
@@ -3690,8 +3702,25 @@ public class CharSequenceUtil {
* @param replacedStr 被替换的字符串
* @return 替换后的字符串
* @since 3.2.1
+ * @deprecated 歧义,请使用{@link #replaceByCodePoint(CharSequence, int, int, CharSequence)}
*/
+ @Deprecated
public static String replace(CharSequence str, int startInclude, int endExclude, CharSequence replacedStr) {
+ return replaceByCodePoint(str, startInclude, endExclude, replacedStr);
+ }
+
+ /**
+ * 替换指定字符串的指定区间内字符为指定字符串,字符串只重复一次
+ * 此方法使用{@link String#codePoints()}完成拆分替换
+ *
+ * @param str 字符串
+ * @param startInclude 开始位置(包含)
+ * @param endExclude 结束位置(不包含)
+ * @param replacedStr 被替换的字符串
+ * @return 替换后的字符串
+ * @since 5.8.27
+ */
+ public static String replaceByCodePoint(CharSequence str, int startInclude, int endExclude, CharSequence replacedStr) {
if (isEmpty(str)) {
return str(str);
}
@@ -3814,7 +3843,7 @@ public class CharSequenceUtil {
if (INDEX_NOT_FOUND == startInclude) {
return str(str);
}
- return replace(str, startInclude, startInclude + searchStr.length(), replacedStr);
+ return replaceByCodePoint(str, startInclude, startInclude + searchStr.length(), replacedStr);
}
/**
@@ -3838,7 +3867,7 @@ public class CharSequenceUtil {
* @since 4.1.14
*/
public static String hide(CharSequence str, int startInclude, int endExclude) {
- return replace(str, startInclude, endExclude, '*');
+ return replaceByCodePoint(str, startInclude, endExclude, '*');
}
/**
diff --git a/hutool-core/src/test/java/cn/hutool/core/text/CharSequenceUtilTest.java b/hutool-core/src/test/java/cn/hutool/core/text/CharSequenceUtilTest.java
index af674d705..5cfadd588 100755
--- a/hutool-core/src/test/java/cn/hutool/core/text/CharSequenceUtilTest.java
+++ b/hutool-core/src/test/java/cn/hutool/core/text/CharSequenceUtilTest.java
@@ -25,7 +25,7 @@ public class CharSequenceUtilTest {
@Test
public void replaceByStrTest() {
String replace = "SSM15930297701BeryAllen";
- String result = CharSequenceUtil.replace(replace, 5, 12, "***");
+ String result = CharSequenceUtil.replaceByCodePoint(replace, 5, 12, "***");
Assert.assertEquals("SSM15***01BeryAllen", result);
}
diff --git a/hutool-core/src/test/java/cn/hutool/core/text/IssueI96LWHTest.java b/hutool-core/src/test/java/cn/hutool/core/text/IssueI96LWHTest.java
new file mode 100644
index 000000000..bc3e6140a
--- /dev/null
+++ b/hutool-core/src/test/java/cn/hutool/core/text/IssueI96LWHTest.java
@@ -0,0 +1,15 @@
+package cn.hutool.core.text;
+
+import cn.hutool.core.lang.Console;
+import cn.hutool.core.util.StrUtil;
+import org.junit.Test;
+
+public class IssueI96LWHTest {
+ @Test
+ public void replaceTest() {
+ String str = "\uD83D\uDC46最上方点击蓝字";
+ Console.log(str.codePoints().toArray());
+ Console.log(StrUtil.replaceByCodePoint(str, 3, 4, "下"));
+ Console.log(new StringBuilder(str).replace(3, 4, "下"));
+ }
+}
diff --git a/hutool-core/src/test/java/cn/hutool/core/util/StrUtilTest.java b/hutool-core/src/test/java/cn/hutool/core/util/StrUtilTest.java
index d1e1ca03b..1108846e2 100755
--- a/hutool-core/src/test/java/cn/hutool/core/util/StrUtilTest.java
+++ b/hutool-core/src/test/java/cn/hutool/core/util/StrUtilTest.java
@@ -1,13 +1,12 @@
package cn.hutool.core.util;
-import java.nio.charset.StandardCharsets;
-import java.util.List;
-
-import cn.hutool.core.lang.Console;
import cn.hutool.core.lang.Dict;
import org.junit.Assert;
import org.junit.Test;
+import java.nio.charset.StandardCharsets;
+import java.util.List;
+
/**
* 字符串工具类单元测试
*
@@ -219,9 +218,9 @@ public class StrUtilTest {
@Test
public void replaceTest() {
- String string = StrUtil.replace("aabbccdd", 2, 6, '*');
+ String string = StrUtil.replaceByCodePoint("aabbccdd", 2, 6, '*');
Assert.assertEquals("aa****dd", string);
- string = StrUtil.replace("aabbccdd", 2, 12, '*');
+ string = StrUtil.replaceByCodePoint("aabbccdd", 2, 12, '*');
Assert.assertEquals("aa******", string);
}
@@ -251,11 +250,11 @@ public class StrUtilTest {
@Test
public void replaceTest5() {
final String a = "\uD853\uDC09秀秀";
- final String result = StrUtil.replace(a, 1, a.length(), '*');
+ final String result = StrUtil.replaceByCodePoint(a, 1, a.length(), '*');
Assert.assertEquals("\uD853\uDC09**", result);
final String aa = "规划大师";
- final String result1 = StrUtil.replace(aa, 2, a.length(), '*');
+ final String result1 = StrUtil.replaceByCodePoint(aa, 2, a.length(), '*');
Assert.assertEquals("规划**", result1);
}