From f9b4c3c58c296fca9d2fda3c98034d0768829279 Mon Sep 17 00:00:00 2001 From: ZhouXY108 Date: Sun, 18 May 2025 15:18:52 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E6=96=B0=E5=A2=9E=20`StringTools#toQuo?= =?UTF-8?q?tedString`=20=E6=96=B9=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../plusone/commons/util/StringTools.java | 17 ++++++++++ .../commons/util/StringToolsTests.java | 32 +++++++++++++++++++ 2 files changed, 49 insertions(+) diff --git a/plusone-commons/src/main/java/xyz/zhouxy/plusone/commons/util/StringTools.java b/plusone-commons/src/main/java/xyz/zhouxy/plusone/commons/util/StringTools.java index 821d219..cf1aec7 100644 --- a/plusone-commons/src/main/java/xyz/zhouxy/plusone/commons/util/StringTools.java +++ b/plusone-commons/src/main/java/xyz/zhouxy/plusone/commons/util/StringTools.java @@ -220,6 +220,23 @@ public class StringTools { return String.valueOf(charArray); } + /** + * 转换为带引号的字符串 + * + * @param value 值 + * @return 带引号的字符串 + * @since 1.1.0 + */ + public static String toQuotedString(@Nullable String value) { + if (value == null) { + return "null"; + } + if (value.isEmpty()) { + return "\"\""; + } + return "\"" + value + "\""; + } + private StringTools() { throw new IllegalStateException("Utility class"); } diff --git a/plusone-commons/src/test/java/xyz/zhouxy/plusone/commons/util/StringToolsTests.java b/plusone-commons/src/test/java/xyz/zhouxy/plusone/commons/util/StringToolsTests.java index 1d43950..dd82523 100644 --- a/plusone-commons/src/test/java/xyz/zhouxy/plusone/commons/util/StringToolsTests.java +++ b/plusone-commons/src/test/java/xyz/zhouxy/plusone/commons/util/StringToolsTests.java @@ -370,6 +370,38 @@ class StringToolsTests { // #endregion - desensitize // ================================ + // ================================ + // #region - toQuotedString + // ================================ + + @Test + void toQuotedString_NullInput_ReturnsNullStr() { + String result = StringTools.toQuotedString(null); + assertEquals("null", result); + + assertEquals("The value is null.", String.format("The value is %s.", result)); + } + + @Test + void toQuotedString_EmptyString_ReturnsEmptyString() { + String result = StringTools.toQuotedString(""); + assertEquals("\"\"", result); + + assertEquals("The value is \"\".", String.format("The value is %s.", result)); + } + + @Test + void toQuotedString_ValidInput_ReturnsQuotedString() { + String result = StringTools.toQuotedString("Hello World"); + assertEquals("\"Hello World\"", result); + + assertEquals("The value is \"Hello World\".", String.format("The value is %s.", result)); + } + + // ================================ + // #endregion - toQuotedString + // ================================ + @Test void test_constructor_isNotAccessible_ThrowsIllegalStateException() { Constructor[] constructors = StringTools.class.getDeclaredConstructors();