From 3599fb4d5e524b4d12a11a1ebd200e345890ee80 Mon Sep 17 00:00:00 2001 From: xuxueli <931591021@qq.com> Date: Sat, 6 Jul 2024 16:41:33 +0800 Subject: [PATCH] add new methods --- .../java/com/xxl/tool/core/StringTool.java | 130 +++++++++++++++++- .../xxl/tool/test/core/StringToolTest.java | 7 + 2 files changed, 135 insertions(+), 2 deletions(-) diff --git a/src/main/java/com/xxl/tool/core/StringTool.java b/src/main/java/com/xxl/tool/core/StringTool.java index 6cb6f8d..c709b8c 100644 --- a/src/main/java/com/xxl/tool/core/StringTool.java +++ b/src/main/java/com/xxl/tool/core/StringTool.java @@ -7,9 +7,19 @@ * (some references to other libraries) */ public class StringTool { + + /** + * The empty String {@code ""}. + */ public static final String EMPTY = ""; + /** + * Represents a failed index search. + */ + public static final int INDEX_NOT_FOUND = -1; + // ---------------------- empty ---------------------- + /** * is empty * @@ -25,7 +35,7 @@ public class StringTool { * @return */ public static boolean isEmpty(String str) { - return str == null || str.length() == 0; + return str == null || str.isEmpty(); } /** @@ -48,6 +58,7 @@ public static boolean isNotEmpty(String str) { // ---------------------- blank ---------------------- + /** * is blank * @@ -63,7 +74,7 @@ public static boolean isNotEmpty(String str) { * @return */ public static boolean isBlank(String str) { - return str == null || str.trim().length()==0; + return str == null || str.trim().isEmpty(); } /** @@ -84,7 +95,9 @@ public static boolean isNotBlank(String str) { return !StringTool.isBlank(str); } + // ---------------------- trim ---------------------- + /** * trim * @@ -140,9 +153,12 @@ public static String trimToEmpty(String str) { return str == null ? EMPTY : str.trim(); } + // ---------------------- isNumeric ---------------------- /** + * isNumeric + * *

Checks if the String contains only unicode digits. * A decimal point is not a unicode digit and returns false.

* @@ -177,5 +193,115 @@ public static boolean isNumeric(String str) { } + // ---------------------- Count matches ---------------------- + + /** + * Count matches + * + *

Counts how many times the substring appears in the larger string.

+ * + *

A {@code null} or empty ("") String input returns {@code 0}.

+ * + *
+     * StringUtils.countMatches(null, *)       = 0
+     * StringUtils.countMatches("", *)         = 0
+     * StringUtils.countMatches("abba", null)  = 0
+     * StringUtils.countMatches("abba", "")    = 0
+     * StringUtils.countMatches("abba", "a")   = 2
+     * StringUtils.countMatches("abba", "ab")  = 1
+     * StringUtils.countMatches("abba", "xxx") = 0
+     * 
+ * + * @param str the CharSequence to check, may be null + * @param sub the substring to count, may be null + * @return the number of occurrences, 0 if either CharSequence is {@code null} + */ + public static int countMatches(String str, String sub) { + if (isEmpty(str) || isEmpty(sub)) { + return 0; + } + int count = 0; + int idx = 0; + while ((idx = str.indexOf(sub, idx)) != INDEX_NOT_FOUND) { + count++; + idx += sub.length(); + } + return count; + } + + // ---------------------- Count matches ---------------------- + + /** + * upperCase first letter + * + *
+     *      StringUtils.upperCaseFirst(null, *)     = null
+     *      StringUtils.upperCaseFirst("", *)       = ""
+     *      StringUtils.countMatches("abc", *)      = "Abc"
+     * 
+ * + * @param text + * @return + */ + public static String upperCaseFirst(String text) { + if (isBlank(text)) { + return text; + } + return text.substring(0, 1).toUpperCase() + text.substring(1); + } + + /** + * lowerCase first letter + * + *
+     *      StringUtils.lowerCaseFirst(null, *)     = null
+     *      StringUtils.lowerCaseFirst("", *)       = ""
+     *      StringUtils.lowerCaseFirst("ABC", *)    = "aBC"
+     * 
+ * + * @param text + * @return + */ + public static String lowerCaseFirst(String text) { + if (isBlank(text)) { + return text; + } + return text.substring(0, 1).toLowerCase() + text.substring(1); + } + + /** + * convert underscores to hump format + * + *
+     *      StringUtils.lowerCaseFirst(null, *)             = null
+     *      StringUtils.lowerCaseFirst("", *)               = ""
+     *      StringUtils.lowerCaseFirst("aaa_bbb", *)        = "aaaBbb"
+     * 
+ * + * @param underscoreText + * @return + */ + public static String underlineToCamelCase(String underscoreText) { + if (isBlank(underscoreText)) { + return underscoreText; + } + StringBuilder result = new StringBuilder(); + boolean flag = false; + for (int i = 0; i < underscoreText.length(); i++) { + char ch = underscoreText.charAt(i); + if ("_".charAt(0) == ch) { + flag = true; + } else { + if (flag) { + result.append(Character.toUpperCase(ch)); + flag = false; + } else { + result.append(ch); + } + } + } + return result.toString(); + } + } diff --git a/src/test/java/com/xxl/tool/test/core/StringToolTest.java b/src/test/java/com/xxl/tool/test/core/StringToolTest.java index 687669a..b2944a5 100644 --- a/src/test/java/com/xxl/tool/test/core/StringToolTest.java +++ b/src/test/java/com/xxl/tool/test/core/StringToolTest.java @@ -14,4 +14,11 @@ public void test() { logger.info("" + StringTool.isBlank(" ")); } + @Test + public void underlineToCamelCaseTest() { + String text = "aaa_bbb"; + logger.info("text = " + text); + logger.info("result = " + StringTool.underlineToCamelCase(text)); + } + }