Skip to content

Commit

Permalink
add new methods
Browse files Browse the repository at this point in the history
  • Loading branch information
xuxueli committed Jul 6, 2024
1 parent 2d2a89b commit 3599fb4
Show file tree
Hide file tree
Showing 2 changed files with 135 additions and 2 deletions.
130 changes: 128 additions & 2 deletions src/main/java/com/xxl/tool/core/StringTool.java
Original file line number Diff line number Diff line change
Expand Up @@ -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
*
Expand All @@ -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();
}

/**
Expand All @@ -48,6 +58,7 @@ public static boolean isNotEmpty(String str) {


// ---------------------- blank ----------------------

/**
* is blank
*
Expand All @@ -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();
}

/**
Expand All @@ -84,7 +95,9 @@ public static boolean isNotBlank(String str) {
return !StringTool.isBlank(str);
}


// ---------------------- trim ----------------------

/**
* trim
*
Expand Down Expand Up @@ -140,9 +153,12 @@ public static String trimToEmpty(String str) {
return str == null ? EMPTY : str.trim();
}


// ---------------------- isNumeric ----------------------

/**
* isNumeric
*
* <p>Checks if the String contains only unicode digits.
* A decimal point is not a unicode digit and returns false.</p>
*
Expand Down Expand Up @@ -177,5 +193,115 @@ public static boolean isNumeric(String str) {
}


// ---------------------- Count matches ----------------------

/**
* Count matches
*
* <p>Counts how many times the substring appears in the larger string.</p>
*
* <p>A {@code null} or empty ("") String input returns {@code 0}.</p>
*
* <pre>
* 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
* </pre>
*
* @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
*
* <pre>
* StringUtils.upperCaseFirst(null, *) = null
* StringUtils.upperCaseFirst("", *) = ""
* StringUtils.countMatches("abc", *) = "Abc"
* </pre>
*
* @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
*
* <pre>
* StringUtils.lowerCaseFirst(null, *) = null
* StringUtils.lowerCaseFirst("", *) = ""
* StringUtils.lowerCaseFirst("ABC", *) = "aBC"
* </pre>
*
* @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
*
* <pre>
* StringUtils.lowerCaseFirst(null, *) = null
* StringUtils.lowerCaseFirst("", *) = ""
* StringUtils.lowerCaseFirst("aaa_bbb", *) = "aaaBbb"
* </pre>
*
* @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();
}


}
7 changes: 7 additions & 0 deletions src/test/java/com/xxl/tool/test/core/StringToolTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}

}

0 comments on commit 3599fb4

Please sign in to comment.