一、Substring
1.包含空指针异常处理的subString,开始
public static String substring(String str, int start) { if (str == null) { return null; } // handle negatives, which means last n characters if (start < 0) { start = str.length() + start; // remember start is negative } if (start < 0) { start = 0; } if (start > str.length()) { return EMPTY; } return str.substring(start); }
2.包含空指针异常处理的SubString,开始,结束
public static String substring(String str, int start, int end) { if (str == null) { return null; } // handle negatives if (end < 0) { end = str.length() + end; // remember end is negative } if (start < 0) { start = str.length() + start; // remember start is negative } // check length next if (end > str.length()) { end = str.length(); } // if start is greater than end, return "" if (start > end) { return EMPTY; } if (start < 0) { start = 0; } if (end < 0) { end = 0; } return str.substring(start, end); }
二、字符截取(Left/Right/Mid)
1.截取左边len个字符
public static String left(String str, int len) { if (str == null) { return null; } if (len < 0) { return EMPTY; } if (str.length() <= len) { return str; } return str.substring(0, len); }
2.截取右边len个字符
public static String right(String str, int len) { if (str == null) { return null; } if (len < 0) { return EMPTY; } if (str.length() <= len) { return str; } return str.substring(str.length() - len); }
3.截取中间len个字符
public static String mid(String str, int pos, int len) { if (str == null) { return null; } if (len < 0 || pos > str.length()) { return EMPTY; } if (pos < 0) { pos = 0; } if (str.length() <= (pos + len)) { return str.substring(pos); } return str.substring(pos, pos + len); }
三、字符截取(SubStringAfter/SubStringBefore)
1.指定字符串之前的字符
public static String substringBefore(String str, String separator) { if (isEmpty(str) || separator == null) { return str; } if (separator.length() == 0) { return EMPTY; } int pos = str.indexOf(separator); if (pos == -1) { return str; } return str.substring(0, pos); }
2.指定字符串之后的字符
public static String substringAfter(String str, String separator) { if (isEmpty(str)) { return str; } if (separator == null) { return EMPTY; } int pos = str.indexOf(separator); if (pos == -1) { return EMPTY; } return str.substring(pos + separator.length()); }
3.倒数。指定字符串之前的字符
public static String substringBeforeLast(String str, String separator) { if (isEmpty(str) || isEmpty(separator)) { return str; } int pos = str.lastIndexOf(separator); if (pos == -1) { return str; } return str.substring(0, pos); }
4.倒数。指定在字符串之后的字符
public static String substringAfterLast(String str, String separator) { if (isEmpty(str)) { return str; } if (isEmpty(separator)) { return EMPTY; } int pos = str.lastIndexOf(separator); if (pos == -1 || pos == (str.length() - separator.length())) { return EMPTY; } return str.substring(pos + separator.length()); }
四、子字符串(Substring between)
1. 在某指定字符串中的子字符串
public static String substringBetween(String str, String tag) { return substringBetween(str, tag, tag); }
2.在某两个指定字符串中间的子字符串,只返回第一个有效值
public static String substringBetween(String str, String open, String close) { if (str == null || open == null || close == null) { return null; } int start = str.indexOf(open); if (start != -1) { int end = str.indexOf(close, start + open.length()); if (end != -1) { return str.substring(start + open.length(), end); } } return null; }
3. 在某两个指定字符串之间的子字符串,以数组形式返回所有有效值
public static String[] substringsBetween(String str, String open, String close) { if (str == null || isEmpty(open) || isEmpty(close)) { return null; } int strLen = str.length(); if (strLen == 0) { return ArrayUtils.EMPTY_STRING_ARRAY; } int closeLen = close.length(); int openLen = open.length(); List list = new ArrayList(); int pos = 0; while (pos < (strLen - closeLen)) { int start = str.indexOf(open, pos); if (start < 0) { break; } start += openLen; int end = str.indexOf(close, start); if (end < 0) { break; } list.add(str.substring(start, end)); pos = end + closeLen; } if (list.isEmpty()) { return null; } return (String[]) list.toArray(new String [list.size()]); }
五、拆分(Splitting)
1.将包含空白字符的字符串拆分成一个数组,以空白字符作为分隔符条件
public static String[] split(String str) { return split(str, null, -1); }
2.将字符串中按照指定字符作为分隔符,拆分成一个数组
public static String[] split(String str, char separatorChar) { return splitWorker(str, separatorChar, false); }
public static String[] split(String str, String separatorChars) { return splitWorker(str, separatorChars, -1, false); }
3.将字符串拆分成指定max大小的数组
public static String[] split(String str, String separatorChars, int max) { return splitWorker(str, separatorChars, max, false); }
4.将字符串按照指定字符串拆分成数据
public static String[] splitByWholeSeparator(String str, String separator) { return splitByWholeSeparatorWorker( str, separator, -1, false ) ; }
public static String[] splitByWholeSeparator( String str, String separator, int max ) { return splitByWholeSeparatorWorker(str, separator, max, false); }
5.将字符串按照指定字符串拆分成数组,如果含有多个separator,则separtor也会作为数组值返回
public static String[] splitByWholeSeparatorPreserveAllTokens(String str, String separator) { return splitByWholeSeparatorWorker(str, separator, -1, true); }
public static String[] splitByWholeSeparatorPreserveAllTokens(String str, String separator, int max) { return splitByWholeSeparatorWorker(str, separator, max, true); }
拆分
private static String[] splitByWholeSeparatorWorker(String str, String separator, int max, boolean preserveAllTokens) { if (str == null) { return null; } int len = str.length(); if (len == 0) { return ArrayUtils.EMPTY_STRING_ARRAY; } if ((separator == null) || (EMPTY.equals(separator))) { // Split on whitespace. return splitWorker(str, null, max, preserveAllTokens); } int separatorLength = separator.length(); ArrayList substrings = new ArrayList(); int numberOfSubstrings = 0; int beg = 0; int end = 0; while (end < len) { end = str.indexOf(separator, beg); if (end > -1) { if (end > beg) { numberOfSubstrings += 1; if (numberOfSubstrings == max) { end = len; substrings.add(str.substring(beg)); } else { // The following is OK, because String.substring( beg, end ) excludes // the character at the position 'end'. substrings.add(str.substring(beg, end)); // Set the starting point for the next search. // The following is equivalent to beg = end + (separatorLength - 1) + 1, // which is the right calculation: beg = end + separatorLength; } } else { // We found a consecutive occurrence of the separator, so skip it. if (preserveAllTokens) { numberOfSubstrings += 1; if (numberOfSubstrings == max) { end = len; substrings.add(str.substring(beg)); } else { substrings.add(EMPTY); } } beg = end + separatorLength; } } else { // String.substring( beg ) goes from 'beg' to the end of the String. substrings.add(str.substring(beg)); end = len; } } return (String[]) substrings.toArray(new String[substrings.size()]); }
6.返回包含separator的数组
public static String[] splitPreserveAllTokens(String str) { return splitWorker(str, null, -1, true); }
public static String[] splitPreserveAllTokens(String str, char separatorChar) { return splitWorker(str, separatorChar, true); }
拆分
private static String[] splitWorker(String str, char separatorChar, boolean preserveAllTokens) { // Performance tuned for 2.0 (JDK1.4) if (str == null) { return null; } int len = str.length(); if (len == 0) { return ArrayUtils.EMPTY_STRING_ARRAY; } List list = new ArrayList(); int i = 0, start = 0; boolean match = false; boolean lastMatch = false; while (i < len) { if (str.charAt(i) == separatorChar) { if (match || preserveAllTokens) { list.add(str.substring(start, i)); match = false; lastMatch = true; } start = ++i; continue; } lastMatch = false; match = true; i++; } if (match || (preserveAllTokens && lastMatch)) { list.add(str.substring(start, i)); } return (String[]) list.toArray(new String[list.size()]); }
7.拆分为数组(StringUtils.splitPreserveAllTokens("ab::cd:ef", ":") = ["ab", "", cd", "ef"])
public static String[] splitPreserveAllTokens(String str, String separatorChars) { return splitWorker(str, separatorChars, -1, true); }
public static String[] splitPreserveAllTokens(String str, String separatorChars, int max) { return splitWorker(str, separatorChars, max, true); }
private static String[] splitWorker(String str, String separatorChars, int max, boolean preserveAllTokens) { // Performance tuned for 2.0 (JDK1.4) // Direct code is quicker than StringTokenizer. // Also, StringTokenizer uses isSpace() not isWhitespace() if (str == null) { return null; } int len = str.length(); if (len == 0) { return ArrayUtils.EMPTY_STRING_ARRAY; } List list = new ArrayList(); int sizePlus1 = 1; int i = 0, start = 0; boolean match = false; boolean lastMatch = false; if (separatorChars == null) { // Null separator means use whitespace while (i < len) { if (Character.isWhitespace(str.charAt(i))) { if (match || preserveAllTokens) { lastMatch = true; if (sizePlus1++ == max) { i = len; lastMatch = false; } list.add(str.substring(start, i)); match = false; } start = ++i; continue; } lastMatch = false; match = true; i++; } } else if (separatorChars.length() == 1) { // Optimise 1 character case char sep = separatorChars.charAt(0); while (i < len) { if (str.charAt(i) == sep) { if (match || preserveAllTokens) { lastMatch = true; if (sizePlus1++ == max) { i = len; lastMatch = false; } list.add(str.substring(start, i)); match = false; } start = ++i; continue; } lastMatch = false; match = true; i++; } } else { // standard case while (i < len) { if (separatorChars.indexOf(str.charAt(i)) >= 0) { if (match || preserveAllTokens) { lastMatch = true; if (sizePlus1++ == max) { i = len; lastMatch = false; } list.add(str.substring(start, i)); match = false; } start = ++i; continue; } lastMatch = false; match = true; i++; } } if (match || (preserveAllTokens && lastMatch)) { list.add(str.substring(start, i)); } return (String[]) list.toArray(new String[list.size()]); }
7.根据字符类型进行拆分(StringUtils.splitByCharacterType("foo200Bar") = ["foo", "200", "B", "ar"])
public static String[] splitByCharacterType(String str) { return splitByCharacterType(str, false); }
8.根据字符类型进行拆分,不区分大小写(StringUtils.splitByCharacterTypeCamelCase("foo200Bar") = ["foo", "200", "Bar"];StringUtils.splitByCharacterTypeCamelCase("ASFRules") = ["ASF", "Rules"])
public static String[] splitByCharacterTypeCamelCase(String str) { return splitByCharacterType(str, true); }
private static String[] splitByCharacterType(String str, boolean camelCase) { if (str == null) { return null; } if (str.length() == 0) { return ArrayUtils.EMPTY_STRING_ARRAY; } char[] c = str.toCharArray(); List list = new ArrayList(); int tokenStart = 0; int currentType = Character.getType(c[tokenStart]); for (int pos = tokenStart + 1; pos < c.length; pos++) { int type = Character.getType(c[pos]); if (type == currentType) { continue; } if (camelCase && type == Character.LOWERCASE_LETTER && currentType == Character.UPPERCASE_LETTER) { int newTokenStart = pos - 1; if (newTokenStart != tokenStart) { list.add(new String(c, tokenStart, newTokenStart - tokenStart)); tokenStart = newTokenStart; } } else { list.add(new String(c, tokenStart, pos - tokenStart)); tokenStart = pos; } currentType = type; } list.add(new String(c, tokenStart, c.length - tokenStart)); return (String[]) list.toArray(new String[list.size()]); }
相关推荐
### StringUtils 概述 `StringUtils` 是一个针对 `java.lang.String` 类型对象进行操作的工具类,它作为 JDK 内置 `String` 类方法的一种补充。与原生的 `String` 类不同的是,`StringUtils` 提供了更加丰富的字符...
《StringUtils的深度解析与应用》 在Java编程中,处理字符串是常见的任务,Apache Commons Lang库中的StringUtils类提供了丰富的字符串操作方法,极大地提高了开发效率。本文将深入探讨StringUtils的几个重要功能,...
9. **重复字符串**:`StringUtils.repeat(String str, int repeat)` 用于创建一个字符串,其中包含指定次数的原始字符串。 10. **子串截取**:`StringUtils.substring(String str, int beginIndex, int endIndex)` ...
9. **重复和填充**:`repeat`方法用于创建一个重复的字符串,`leftPad`和`rightPad`则可以在字符串两侧填充指定字符以达到指定长度。 10. **删除子串**:`removeStart`和`removeEnd`可以从字符串的开始或结束处移除...
Java开发中中经常使用的Java工具类分享,工作中用得上,直接拿来使用,不用重复造轮子。
StringUtils.java StringUtils.java
org.apache.commons.lang3.StringUtils的jar包,判断字符串为空
`StringUtils` API 是 Apache Commons Lang 库中的一个实用工具类,专门为处理 `java.lang.String` 对象提供了丰富的静态方法。这个库是对 Java 标准库中的 `String` 类方法的一个扩展,尤其在处理 `null` 和空白...
`StringUtils`工具类是Java开发中非常常用的一个类库,主要提供了一系列静态方法来处理字符串。这个工具类在处理字符串的常见操作时提供了很大的便利,比如数组转字符串、空值检测、非空处理以及空格处理等。接下来...
`StringUtils`是Apache Commons Lang库中的一个核心工具类,它提供了大量的静态方法,用于处理字符串。这个类在Java开发中非常常见,因为它弥补了Java标准库中对字符串操作的不足。`StringUtils`类包含了多种字符串...
12. **重复生成**:`StringUtils.repeat()` 方法可以生成重复的字符串。 在实际使用 `common-lang3.jar` 时,你可以通过导入 `org.apache.commons.lang3.StringUtils` 类并调用相应的方法来提升字符串处理的效率和...
### StringUtils函数全集详解 #### 一、简介 在Java编程语言中,处理字符串是非常常见的需求之一。为了方便开发者高效地进行字符串操作,Apache Commons Lang库提供了一个强大的工具类——`StringUtils`。此工具类...
\n\t\f\r") = "" //所有控制字符都被移除 StringUtils.trim("bob") = "bob" StringUtils.trim(" bob ") = "bob" 6. public static String trimToNull(String str) 和 trim() 类似,但结果为 null 时返回 null,而...
继承了org.apache.commons.lang3.StringUtils工具类,加入了部分常用方法,使用时直接添加到项目的公共utils下,同时在pom.xml加入依赖: <!-- ...
`StringUtils`是Apache Commons Lang库中的一个核心工具类,它提供了大量的静态方法,用于处理字符串。这个库在Java开发中非常常见,因为它弥补了Java标准库中对字符串操作的不足。`StringUtils`类包含了多种实用...