怎么将String elutionbufferr = "01260A0B21A2"这样的格式转成String data[5] = {0x01,0x26,0x0A,0x0B,0x21,0xA2}

每次做项目都会遇到字符串的处理,每次都会去写一个StringUtil,完成一些功能。
但其实每次要的功能都差不多:
1.判断类(包括NULL和空串、是否是空白字符串等)
3.去空白(trim)
5.字符类型判断(是否只包含数字、字母)
6.大小写转换(首字母大小写等)
7.字符串分割
8.字符串连接
9.字符串查找
11.删除字符
12.字符串比较
下面是一个字符串的工具类,涵盖了上面列的功能,算是比较完整的。
* 有关字符串处理的工具类。
* 这个类中的每个方法都可以&安全&地处理&code&null&/code&,而不会抛出&code&NullPointerException&/code&。
9 public class StringUtil {
/* ============================================================================ */
常量和singleton。
/* ============================================================================ */
/** 空字符串。 */
public static final String EMPTY_STRING = "";
/* ============================================================================ */
判空函数。
以下方法用来判定一个字符串是否为:
2. empty - ""
3. blank - "全部是空白" - 空白由Character.isWhitespace所定义。
/* ============================================================================ */
* 检查字符串是否为&code&null&/code&或空字符串&code&""&/code&。
* StringUtil.isEmpty(null)
* StringUtil.isEmpty("")
* StringUtil.isEmpty(" ")
* StringUtil.isEmpty("bob")
* StringUtil.isEmpty("
") = false
* @param str 要检查的字符串
* @return 如果为空, 则返回&code&true&/code&
public static boolean isEmpty(String str) {
return ((str == null) || (str.length() == 0));
* 检查字符串是否不是&code&null&/code&和空字符串&code&""&/code&。
* StringUtil.isEmpty(null)
* StringUtil.isEmpty("")
* StringUtil.isEmpty(" ")
* StringUtil.isEmpty("bob")
* StringUtil.isEmpty("
* @param str 要检查的字符串
* @return 如果不为空, 则返回&code&true&/code&
public static boolean isNotEmpty(String str) {
return ((str != null) && (str.length() & 0));
* 检查字符串是否是空白:&code&null&/code&、空字符串&code&""&/code&或只有空白字符。
* StringUtil.isBlank(null)
* StringUtil.isBlank("")
* StringUtil.isBlank(" ")
* StringUtil.isBlank("bob")
* StringUtil.isBlank("
") = false
* @param str 要检查的字符串
* @return 如果为空白, 则返回&code&true&/code&
public static boolean isBlank(String str) {
if ((str == null) || ((length = str.length()) == 0)) {
return true;
for (int i = 0; i & i++) {
if (!Character.isWhitespace(str.charAt(i))) {
return false;
return true;
* 检查字符串是否不是空白:&code&null&/code&、空字符串&code&""&/code&或只有空白字符。
* StringUtil.isBlank(null)
* StringUtil.isBlank("")
* StringUtil.isBlank(" ")
* StringUtil.isBlank("bob")
* StringUtil.isBlank("
* @param str 要检查的字符串
* @return 如果为空白, 则返回&code&true&/code&
public static boolean isNotBlank(String str) {
if ((str == null) || ((length = str.length()) == 0)) {
return false;
for (int i = 0; i & i++) {
if (!Character.isWhitespace(str.charAt(i))) {
return true;
return false;
/* ============================================================================ */
默认值函数。
当字符串为null、empty或blank时,将字符串转换成指定的默认字符串。
/* ============================================================================ */
* 如果字符串是&code&null&/code&,则返回空字符串&code&""&/code&,否则返回字符串本身。
* StringUtil.defaultIfNull(null)
* StringUtil.defaultIfNull("")
* StringUtil.defaultIfNull("
* StringUtil.defaultIfNull("bat") = "bat"
* @param str 要转换的字符串
* @return 字符串本身或空字符串&code&""&/code&
public static String defaultIfNull(String str) {
return (str == null) ? EMPTY_STRING :
* 如果字符串是&code&null&/code&,则返回指定默认字符串,否则返回字符串本身。
* StringUtil.defaultIfNull(null, "default")
= "default"
* StringUtil.defaultIfNull("", "default")
* StringUtil.defaultIfNull("
", "default")
* StringUtil.defaultIfNull("bat", "default") = "bat"
* @param str 要转换的字符串
* @param defaultStr 默认字符串
* @return 字符串本身或指定的默认字符串
public static String defaultIfNull(String str, String defaultStr) {
return (str == null) ? defaultStr :
* 如果字符串是&code&null&/code&或空字符串&code&""&/code&,则返回空字符串&code&""&/code&,否则返回字符串本身。
* 此方法实际上和&code&defaultIfNull(String)&/code&等效。
* StringUtil.defaultIfEmpty(null)
* StringUtil.defaultIfEmpty("")
* StringUtil.defaultIfEmpty("
* StringUtil.defaultIfEmpty("bat") = "bat"
* @param str 要转换的字符串
* @return 字符串本身或空字符串&code&""&/code&
public static String defaultIfEmpty(String str) {
return (str == null) ? EMPTY_STRING :
* 如果字符串是&code&null&/code&或空字符串&code&""&/code&,则返回指定默认字符串,否则返回字符串本身。
* StringUtil.defaultIfEmpty(null, "default")
= "default"
* StringUtil.defaultIfEmpty("", "default")
= "default"
* StringUtil.defaultIfEmpty("
", "default")
* StringUtil.defaultIfEmpty("bat", "default") = "bat"
* @param str 要转换的字符串
* @param defaultStr 默认字符串
* @return 字符串本身或指定的默认字符串
public static String defaultIfEmpty(String str, String defaultStr) {
return ((str == null) || (str.length() == 0)) ? defaultStr :
* 如果字符串是空白:&code&null&/code&、空字符串&code&""&/code&或只有空白字符,则返回空字符串&code&""&/code&,否则返回字符串本身。
* StringUtil.defaultIfBlank(null)
* StringUtil.defaultIfBlank("")
* StringUtil.defaultIfBlank("
* StringUtil.defaultIfBlank("bat") = "bat"
* @param str 要转换的字符串
* @return 字符串本身或空字符串&code&""&/code&
public static String defaultIfBlank(String str) {
return isBlank(str) ? EMPTY_STRING :
* 如果字符串是&code&null&/code&或空字符串&code&""&/code&,则返回指定默认字符串,否则返回字符串本身。
* StringUtil.defaultIfBlank(null, "default")
= "default"
* StringUtil.defaultIfBlank("", "default")
= "default"
* StringUtil.defaultIfBlank("
", "default")
= "default"
* StringUtil.defaultIfBlank("bat", "default") = "bat"
* @param str 要转换的字符串
* @param defaultStr 默认字符串
* @return 字符串本身或指定的默认字符串
public static String defaultIfBlank(String str, String defaultStr) {
return isBlank(str) ? defaultStr :
/* ============================================================================ */
去空白(或指定字符)的函数。
以下方法用来除去一个字串中的空白或指定字符。
/* ============================================================================ */
* 除去字符串头尾部的空白,如果字符串是&code&null&/code&,依然返回&code&null&/code&。
* 注意,和&code&String.trim&/code&不同,此方法使用&code&Character.isWhitespace&/code&来判定空白,
* 因而可以除去英文字符集之外的其它空白,如中文空格。
* StringUtil.trim(null)
* StringUtil.trim("")
* StringUtil.trim("
* StringUtil.trim("abc")
* StringUtil.trim("
") = "abc"
* @param str 要处理的字符串
* @return 除去空白的字符串,如果原字串为&code&null&/code&,则返回&code&null&/code&
public static String trim(String str) {
return trim(str, null, 0);
* 除去字符串头尾部的指定字符,如果字符串是&code&null&/code&,依然返回&code&null&/code&。
* StringUtil.trim(null, *)
* StringUtil.trim("", *)
* StringUtil.trim("abc", null)
* StringUtil.trim("
abc", null)
* StringUtil.trim("abc
* StringUtil.trim(" abc ", null)
* StringUtil.trim("
abcyx", "xyz") = "
* @param str 要处理的字符串
* @param stripChars 要除去的字符,如果为&code&null&/code&表示除去空白字符
* @return 除去指定字符后的的字符串,如果原字串为&code&null&/code&,则返回&code&null&/code&
public static String trim(String str, String stripChars) {
return trim(str, stripChars, 0);
* 除去字符串头部的空白,如果字符串是&code&null&/code&,则返回&code&null&/code&。
* 注意,和&code&String.trim&/code&不同,此方法使用&code&Character.isWhitespace&/code&来判定空白,
* 因而可以除去英文字符集之外的其它空白,如中文空格。
* StringUtil.trimStart(null)
* StringUtil.trimStart("")
* StringUtil.trimStart("abc")
* StringUtil.trimStart("
* StringUtil.trimStart("abc
* StringUtil.trimStart(" abc ")
* @param str 要处理的字符串
* @return 除去空白的字符串,如果原字串为&code&null&/code&或结果字符串为&code&""&/code&,则返回&code&null&/code&
public static String trimStart(String str) {
return trim(str, null, -1);
* 除去字符串头部的指定字符,如果字符串是&code&null&/code&,依然返回&code&null&/code&。
* StringUtil.trimStart(null, *)
* StringUtil.trimStart("", *)
* StringUtil.trimStart("abc", "")
* StringUtil.trimStart("abc", null)
* StringUtil.trimStart("
abc", null)
* StringUtil.trimStart("abc
* StringUtil.trimStart(" abc ", null)
* StringUtil.trimStart("yxabc
", "xyz") = "abc
* @param str 要处理的字符串
* @param stripChars 要除去的字符,如果为&code&null&/code&表示除去空白字符
* @return 除去指定字符后的的字符串,如果原字串为&code&null&/code&,则返回&code&null&/code&
public static String trimStart(String str, String stripChars) {
return trim(str, stripChars, -1);
* 除去字符串尾部的空白,如果字符串是&code&null&/code&,则返回&code&null&/code&。
* 注意,和&code&String.trim&/code&不同,此方法使用&code&Character.isWhitespace&/code&来判定空白,
* 因而可以除去英文字符集之外的其它空白,如中文空格。
* StringUtil.trimEnd(null)
* StringUtil.trimEnd("")
* StringUtil.trimEnd("abc")
* StringUtil.trimEnd("
* StringUtil.trimEnd("abc
* StringUtil.trimEnd(" abc ")
* @param str 要处理的字符串
* @return 除去空白的字符串,如果原字串为&code&null&/code&或结果字符串为&code&""&/code&,则返回&code&null&/code&
public static String trimEnd(String str) {
return trim(str, null, 1);
* 除去字符串尾部的指定字符,如果字符串是&code&null&/code&,依然返回&code&null&/code&。
* StringUtil.trimEnd(null, *)
* StringUtil.trimEnd("", *)
* StringUtil.trimEnd("abc", "")
* StringUtil.trimEnd("abc", null)
* StringUtil.trimEnd("
abc", null)
* StringUtil.trimEnd("abc
* StringUtil.trimEnd(" abc ", null)
* StringUtil.trimEnd("
abcyx", "xyz") = "
* @param str 要处理的字符串
* @param stripChars 要除去的字符,如果为&code&null&/code&表示除去空白字符
* @return 除去指定字符后的的字符串,如果原字串为&code&null&/code&,则返回&code&null&/code&
public static String trimEnd(String str, String stripChars) {
return trim(str, stripChars, 1);
* 除去字符串头尾部的空白,如果结果字符串是空字符串&code&""&/code&,则返回&code&null&/code&。
* 注意,和&code&String.trim&/code&不同,此方法使用&code&Character.isWhitespace&/code&来判定空白,
* 因而可以除去英文字符集之外的其它空白,如中文空格。
* StringUtil.trimToNull(null)
* StringUtil.trimToNull("")
* StringUtil.trimToNull("
* StringUtil.trimToNull("abc")
* StringUtil.trimToNull("
") = "abc"
* @param str 要处理的字符串
* @return 除去空白的字符串,如果原字串为&code&null&/code&或结果字符串为&code&""&/code&,则返回&code&null&/code&
public static String trimToNull(String str) {
return trimToNull(str, null);
* 除去字符串头尾部的空白,如果结果字符串是空字符串&code&""&/code&,则返回&code&null&/code&。
* 注意,和&code&String.trim&/code&不同,此方法使用&code&Character.isWhitespace&/code&来判定空白,
* 因而可以除去英文字符集之外的其它空白,如中文空格。
* StringUtil.trim(null, *)
* StringUtil.trim("", *)
* StringUtil.trim("abc", null)
* StringUtil.trim("
abc", null)
* StringUtil.trim("abc
* StringUtil.trim(" abc ", null)
* StringUtil.trim("
abcyx", "xyz") = "
* @param str 要处理的字符串
* @param stripChars 要除去的字符,如果为&code&null&/code&表示除去空白字符
* @return 除去空白的字符串,如果原字串为&code&null&/code&或结果字符串为&code&""&/code&,则返回&code&null&/code&
public static String trimToNull(String str, String stripChars) {
String result = trim(str, stripChars);
if ((result == null) || (result.length() == 0)) {
return null;
* 除去字符串头尾部的空白,如果字符串是&code&null&/code&,则返回空字符串&code&""&/code&。
* 注意,和&code&String.trim&/code&不同,此方法使用&code&Character.isWhitespace&/code&来判定空白,
* 因而可以除去英文字符集之外的其它空白,如中文空格。
* StringUtil.trimToEmpty(null)
* StringUtil.trimToEmpty("")
* StringUtil.trimToEmpty("
* StringUtil.trimToEmpty("abc")
* StringUtil.trimToEmpty("
") = "abc"
* @param str 要处理的字符串
* @return 除去空白的字符串,如果原字串为&code&null&/code&或结果字符串为&code&""&/code&,则返回&code&null&/code&
public static String trimToEmpty(String str) {
return trimToEmpty(str, null);
* 除去字符串头尾部的空白,如果字符串是&code&null&/code&,则返回空字符串&code&""&/code&。
* 注意,和&code&String.trim&/code&不同,此方法使用&code&Character.isWhitespace&/code&来判定空白,
* 因而可以除去英文字符集之外的其它空白,如中文空格。
* StringUtil.trim(null, *)
* StringUtil.trim("", *)
* StringUtil.trim("abc", null)
* StringUtil.trim("
abc", null)
* StringUtil.trim("abc
* StringUtil.trim(" abc ", null)
* StringUtil.trim("
abcyx", "xyz") = "
* @param str 要处理的字符串
* @return 除去空白的字符串,如果原字串为&code&null&/code&或结果字符串为&code&""&/code&,则返回&code&null&/code&
public static String trimToEmpty(String str, String stripChars) {
String result = trim(str, stripChars);
if (result == null) {
return EMPTY_STRING;
* 除去字符串头尾部的指定字符,如果字符串是&code&null&/code&,依然返回&code&null&/code&。
* StringUtil.trim(null, *)
* StringUtil.trim("", *)
* StringUtil.trim("abc", null)
* StringUtil.trim("
abc", null)
* StringUtil.trim("abc
* StringUtil.trim(" abc ", null)
* StringUtil.trim("
abcyx", "xyz") = "
* @param str 要处理的字符串
* @param stripChars 要除去的字符,如果为&code&null&/code&表示除去空白字符
* @param mode &code&-1&/code&表示trimStart,&code&0&/code&表示trim全部,&code&1&/code&表示trimEnd
* @return 除去指定字符后的的字符串,如果原字串为&code&null&/code&,则返回&code&null&/code&
private static String trim(String str, String stripChars, int mode) {
if (str == null) {
return null;
int length = str.length();
int start = 0;
// 扫描字符串头部
if (mode &= 0) {
if (stripChars == null) {
while ((start & end) && (Character.isWhitespace(str.charAt(start)))) {
} else if (stripChars.length() == 0) {
while ((start & end) && (stripChars.indexOf(str.charAt(start)) != -1)) {
// 扫描字符串尾部
if (mode &= 0) {
if (stripChars == null) {
while ((start & end) && (Character.isWhitespace(str.charAt(end - 1)))) {
} else if (stripChars.length() == 0) {
while ((start & end) && (stripChars.indexOf(str.charAt(end - 1)) != -1)) {
if ((start & 0) || (end & length)) {
return str.substring(start, end);
/* ============================================================================ */
比较函数。
以下方法用来比较两个字符串是否相同。
/* ============================================================================ */
* 比较两个字符串(大小写敏感)。
* StringUtil.equals(null, null)
* StringUtil.equals(null, "abc")
* StringUtil.equals("abc", null)
* StringUtil.equals("abc", "abc") = true
* StringUtil.equals("abc", "ABC") = false
* @param str1 要比较的字符串1
* @param str2 要比较的字符串2
* @return 如果两个字符串相同,或者都是&code&null&/code&,则返回&code&true&/code&
public static boolean equals(String str1, String str2) {
if (str1 == null) {
return str2 == null;
return str1.equals(str2);
* 比较两个字符串(大小写不敏感)。
* StringUtil.equalsIgnoreCase(null, null)
* StringUtil.equalsIgnoreCase(null, "abc")
* StringUtil.equalsIgnoreCase("abc", null)
* StringUtil.equalsIgnoreCase("abc", "abc") = true
* StringUtil.equalsIgnoreCase("abc", "ABC") = true
* @param str1 要比较的字符串1
* @param str2 要比较的字符串2
* @return 如果两个字符串相同,或者都是&code&null&/code&,则返回&code&true&/code&
public static boolean equalsIgnoreCase(String str1, String str2) {
if (str1 == null) {
return str2 == null;
return str1.equalsIgnoreCase(str2);
/* ============================================================================ */
字符串类型判定函数。
判定字符串的类型是否为:字母、数字、空白等
/* ============================================================================ */
* 判断字符串是否只包含unicode字母。
* &code&null&/code&将返回&code&false&/code&,空字符串&code&""&/code&将返回&code&true&/code&。
* StringUtil.isAlpha(null)
* StringUtil.isAlpha("")
* StringUtil.isAlpha("
* StringUtil.isAlpha("abc")
* StringUtil.isAlpha("ab2c") = false
* StringUtil.isAlpha("ab-c") = false
* @param str 要检查的字符串
* @return 如果字符串非&code&null&/code&并且全由unicode字母组成,则返回&code&true&/code&
public static boolean isAlpha(String str) {
if (str == null) {
return false;
int length = str.length();
for (int i = 0; i & i++) {
if (!Character.isLetter(str.charAt(i))) {
return false;
return true;
* 判断字符串是否只包含unicode字母和空格&code&' '&/code&。
* &code&null&/code&将返回&code&false&/code&,空字符串&code&""&/code&将返回&code&true&/code&。
* StringUtil.isAlphaSpace(null)
* StringUtil.isAlphaSpace("")
* StringUtil.isAlphaSpace("
* StringUtil.isAlphaSpace("abc")
* StringUtil.isAlphaSpace("ab c") = true
* StringUtil.isAlphaSpace("ab2c") = false
* StringUtil.isAlphaSpace("ab-c") = false
* @param str 要检查的字符串
* @return 如果字符串非&code&null&/code&并且全由unicode字母和空格组成,则返回&code&true&/code&
public static boolean isAlphaSpace(String str) {
if (str == null) {
return false;
int length = str.length();
for (int i = 0; i & i++) {
if (!Character.isLetter(str.charAt(i)) && (str.charAt(i) != ' ')) {
return false;
return true;
* 判断字符串是否只包含unicode字母和数字。
* &code&null&/code&将返回&code&false&/code&,空字符串&code&""&/code&将返回&code&true&/code&。
* StringUtil.isAlphanumeric(null)
* StringUtil.isAlphanumeric("")
* StringUtil.isAlphanumeric("
* StringUtil.isAlphanumeric("abc")
* StringUtil.isAlphanumeric("ab c") = false
* StringUtil.isAlphanumeric("ab2c") = true
* StringUtil.isAlphanumeric("ab-c") = false
* @param str 要检查的字符串
* @return 如果字符串非&code&null&/code&并且全由unicode字母数字组成,则返回&code&true&/code&
public static boolean isAlphanumeric(String str) {
if (str == null) {
return false;
int length = str.length();
for (int i = 0; i & i++) {
if (!Character.isLetterOrDigit(str.charAt(i))) {
return false;
return true;
* 判断字符串是否只包含unicode字母数字和空格&code&' '&/code&。
* &code&null&/code&将返回&code&false&/code&,空字符串&code&""&/code&将返回&code&true&/code&。
* StringUtil.isAlphanumericSpace(null)
* StringUtil.isAlphanumericSpace("")
* StringUtil.isAlphanumericSpace("
* StringUtil.isAlphanumericSpace("abc")
* StringUtil.isAlphanumericSpace("ab c") = true
* StringUtil.isAlphanumericSpace("ab2c") = true
* StringUtil.isAlphanumericSpace("ab-c") = false
* @param str 要检查的字符串
* @return 如果字符串非&code&null&/code&并且全由unicode字母数字和空格组成,则返回&code&true&/code&
public static boolean isAlphanumericSpace(String str) {
if (str == null) {
return false;
int length = str.length();
for (int i = 0; i & i++) {
if (!Character.isLetterOrDigit(str.charAt(i)) && (str.charAt(i) != ' ')) {
return false;
return true;
* 判断字符串是否只包含unicode数字。
* &code&null&/code&将返回&code&false&/code&,空字符串&code&""&/code&将返回&code&true&/code&。
* StringUtil.isNumeric(null)
* StringUtil.isNumeric("")
* StringUtil.isNumeric("
* StringUtil.isNumeric("123")
* StringUtil.isNumeric("12 3") = false
* StringUtil.isNumeric("ab2c") = false
* StringUtil.isNumeric("12-3") = false
* StringUtil.isNumeric("12.3") = false
* @param str 要检查的字符串
* @return 如果字符串非&code&null&/code&并且全由unicode数字组成,则返回&code&true&/code&
public static boolean isNumeric(String str) {
if (str == null) {
return false;
int length = str.length();
for (int i = 0; i & i++) {
if (!Character.isDigit(str.charAt(i))) {
return false;
return true;
* 判断字符串是否只包含unicode数字,包括小数。
* &code&null&/code&将返回&code&false&/code&,空字符串&code&""&/code&将返回&code&true&/code&。
* StringUtil.isNumeric(null)
* StringUtil.isNumeric("")
* StringUtil.isNumeric("
* StringUtil.isNumeric("123")
* StringUtil.isNumeric("12 3") = false
* StringUtil.isNumeric("ab2c") = false
* StringUtil.isNumeric("12-3") = false
* StringUtil.isNumeric("12.3") = true
* @param str 要检查的字符串
* @return 如果字符串非&code&null&/code&并且全由unicode数字组成,则返回&code&true&/code&
public static boolean isNumber(String str) {
if (isBlank(str)) {
return false;
int index = str.indexOf(".");
if (index & 0) {
return isNumeric(str);
String num1 = str.substring(0, index);
String num2 = str.substring(index + 1);
return isNumeric(num1) && isNumeric(num2);
* 判断字符串是否只包含unicode数字和空格&code&' '&/code&。
* &code&null&/code&将返回&code&false&/code&,空字符串&code&""&/code&将返回&code&true&/code&。
* StringUtil.isNumericSpace(null)
* StringUtil.isNumericSpace("")
* StringUtil.isNumericSpace("
* StringUtil.isNumericSpace("123")
* StringUtil.isNumericSpace("12 3") = true
* StringUtil.isNumericSpace("ab2c") = false
* StringUtil.isNumericSpace("12-3") = false
* StringUtil.isNumericSpace("12.3") = false
* @param str 要检查的字符串
* @return 如果字符串非&code&null&/code&并且全由unicode数字和空格组成,则返回&code&true&/code&
public static boolean isNumericSpace(String str) {
if (str == null) {
return false;
int length = str.length();
for (int i = 0; i & i++) {
if (!Character.isDigit(str.charAt(i)) && (str.charAt(i) != ' ')) {
return false;
return true;
* 判断字符串是否只包含unicode空白。
* &code&null&/code&将返回&code&false&/code&,空字符串&code&""&/code&将返回&code&true&/code&。
* StringUtil.isWhitespace(null)
* StringUtil.isWhitespace("")
* StringUtil.isWhitespace("
* StringUtil.isWhitespace("abc")
* StringUtil.isWhitespace("ab2c") = false
* StringUtil.isWhitespace("ab-c") = false
* @param str 要检查的字符串
* @return 如果字符串非&code&null&/code&并且全由unicode空白组成,则返回&code&true&/code&
public static boolean isWhitespace(String str) {
if (str == null) {
return false;
int length = str.length();
for (int i = 0; i & i++) {
if (!Character.isWhitespace(str.charAt(i))) {
return false;
return true;
/* ============================================================================ */
大小写转换。
/* ============================================================================ */
* 将字符串转换成大写。
* 如果字符串是&code&null&/code&则返回&code&null&/code&。
* StringUtil.toUpperCase(null)
* StringUtil.toUpperCase("")
* StringUtil.toUpperCase("aBc") = "ABC"
* @param str 要转换的字符串
* @return 大写字符串,如果原字符串为&code&null&/code&,则返回&code&null&/code&
public static String toUpperCase(String str) {
if (str == null) {
return null;
return str.toUpperCase();
* 将字符串转换成小写。
* 如果字符串是&code&null&/code&则返回&code&null&/code&。
* StringUtil.toLowerCase(null)
* StringUtil.toLowerCase("")
* StringUtil.toLowerCase("aBc") = "abc"
* @param str 要转换的字符串
* @return 大写字符串,如果原字符串为&code&null&/code&,则返回&code&null&/code&
public static String toLowerCase(String str) {
if (str == null) {
return null;
return str.toLowerCase();
* 将字符串的首字符转成大写(&code&Character.toTitleCase&/code&),其它字符不变。
* 如果字符串是&code&null&/code&则返回&code&null&/code&。
* StringUtil.capitalize(null)
* StringUtil.capitalize("")
* StringUtil.capitalize("cat") = "Cat"
* StringUtil.capitalize("cAt") = "CAt"
* @param str 要转换的字符串
* @return 首字符为大写的字符串,如果原字符串为&code&null&/code&,则返回&code&null&/code&
public static String capitalize(String str) {
if ((str == null) || ((strLen = str.length()) == 0)) {
return new StringBuffer(strLen).append(Character.toTitleCase(str.charAt(0))).append(
str.substring(1)).toString();
* 将字符串的首字符转成小写,其它字符不变。
* 如果字符串是&code&null&/code&则返回&code&null&/code&。
* StringUtil.uncapitalize(null)
* StringUtil.uncapitalize("")
* StringUtil.uncapitalize("Cat") = "cat"
* StringUtil.uncapitalize("CAT") = "cAT"
* @param str 要转换的字符串
* @return 首字符为小写的字符串,如果原字符串为&code&null&/code&,则返回&code&null&/code&
public static String uncapitalize(String str) {
if ((str == null) || ((strLen = str.length()) == 0)) {
return new StringBuffer(strLen).append(Character.toLowerCase(str.charAt(0))).append(
str.substring(1)).toString();
* 反转字符串的大小写。
* 如果字符串是&code&null&/code&则返回&code&null&/code&。
* StringUtil.swapCase(null)
* StringUtil.swapCase("")
* StringUtil.swapCase("The dog has a BONE") = "tHE DOG HAS A bone"
* @param str 要转换的字符串
* @return 大小写被反转的字符串,如果原字符串为&code&null&/code&,则返回&code&null&/code&
public static String swapCase(String str) {
if ((str == null) || ((strLen = str.length()) == 0)) {
StringBuffer buffer = new StringBuffer(strLen);
char ch = 0;
for (int i = 0; i & strL i++) {
ch = str.charAt(i);
if (Character.isUpperCase(ch)) {
ch = Character.toLowerCase(ch);
} else if (Character.isTitleCase(ch)) {
ch = Character.toLowerCase(ch);
} else if (Character.isLowerCase(ch)) {
ch = Character.toUpperCase(ch);
buffer.append(ch);
return buffer.toString();
* 将字符串转换成camel case。
* 如果字符串是&code&null&/code&则返回&code&null&/code&。
* StringUtil.toCamelCase(null)
* StringUtil.toCamelCase("")
* StringUtil.toCamelCase("aBc") = "aBc"
* StringUtil.toCamelCase("aBc def") = "aBcDef"
* StringUtil.toCamelCase("aBc def_ghi") = "aBcDefGhi"
* StringUtil.toCamelCase("aBc def_ghi 123") = "aBcDefGhi123"
* 此方法会保留除了下划线和空白以外的所有分隔符。
* @param str 要转换的字符串
* @return camel case字符串,如果原字符串为&code&null&/code&,则返回&code&null&/code&
public static String toCamelCase(String str) {
return CAMEL_CASE_TOKENIZER.parse(str);
* 将字符串转换成pascal case。
* 如果字符串是&code&null&/code&则返回&code&null&/code&。
* StringUtil.toPascalCase(null)
* StringUtil.toPascalCase("")
* StringUtil.toPascalCase("aBc") = "ABc"
* StringUtil.toPascalCase("aBc def") = "ABcDef"
* StringUtil.toPascalCase("aBc def_ghi") = "ABcDefGhi"
* StringUtil.toPascalCase("aBc def_ghi 123") = "aBcDefGhi123"
* 此方法会保留除了下划线和空白以外的所有分隔符。
* @param str 要转换的字符串
* @return pascal case字符串,如果原字符串为&code&null&/code&,则返回&code&null&/code&
public static String toPascalCase(String str) {
return PASCAL_CASE_TOKENIZER.parse(str);
* 将字符串转换成下划线分隔的大写字符串。
* 如果字符串是&code&null&/code&则返回&code&null&/code&。
* StringUtil.toUpperCaseWithUnderscores(null)
* StringUtil.toUpperCaseWithUnderscores("")
* StringUtil.toUpperCaseWithUnderscores("aBc") = "A_BC"
* StringUtil.toUpperCaseWithUnderscores("aBc def") = "A_BC_DEF"
* StringUtil.toUpperCaseWithUnderscores("aBc def_ghi") = "A_BC_DEF_GHI"
* StringUtil.toUpperCaseWithUnderscores("aBc def_ghi 123") = "A_BC_DEF_GHI_123"
* StringUtil.toUpperCaseWithUnderscores("__a__Bc__") = "__A__BC__"
* 此方法会保留除了空白以外的所有分隔符。
* @param str 要转换的字符串
* @return 下划线分隔的大写字符串,如果原字符串为&code&null&/code&,则返回&code&null&/code&
public static String toUpperCaseWithUnderscores(String str) {
return UPPER_CASE_WITH_UNDERSCORES_TOKENIZER.parse(str);
* 将字符串转换成下划线分隔的小写字符串。
* 如果字符串是&code&null&/code&则返回&code&null&/code&。
* StringUtil.toLowerCaseWithUnderscores(null)
* StringUtil.toLowerCaseWithUnderscores("")
* StringUtil.toLowerCaseWithUnderscores("aBc") = "a_bc"
* StringUtil.toLowerCaseWithUnderscores("aBc def") = "a_bc_def"
* StringUtil.toLowerCaseWithUnderscores("aBc def_ghi") = "a_bc_def_ghi"
* StringUtil.toLowerCaseWithUnderscores("aBc def_ghi 123") = "a_bc_def_ghi_123"
* StringUtil.toLowerCaseWithUnderscores("__a__Bc__") = "__a__bc__"
* 此方法会保留除了空白以外的所有分隔符。
* @param str 要转换的字符串
* @return 下划线分隔的小写字符串,如果原字符串为&code&null&/code&,则返回&code&null&/code&
public static String toLowerCaseWithUnderscores(String str) {
return LOWER_CASE_WITH_UNDERSCORES_TOKENIZER.parse(str);
/** 解析单词的解析器。 */
private static final WordTokenizer CAMEL_CASE_TOKENIZER
= new WordTokenizer() {
protected void startSentence(
StringBuffer buffer,
char ch) {
.append(Character
.toLowerCase(ch));
protected void startWord(
StringBuffer buffer,
char ch) {
if (!isDelimiter(buffer
.charAt(buffer
.length() - 1))) {
.append(Character
.toUpperCase(ch));
.append(Character
.toLowerCase(ch));
protected void inWord(
StringBuffer buffer,
char ch) {
.append(Character
.toLowerCase(ch));
protected void startDigitSentence(
StringBuffer buffer,
char ch) {
.append(ch);
protected void startDigitWord(
StringBuffer buffer,
char ch) {
.append(ch);
protected void inDigitWord(
StringBuffer buffer,
char ch) {
.append(ch);
protected void inDelimiter(
StringBuffer buffer,
char ch) {
if (ch != UNDERSCORE) {
.append(ch);
private static final WordTokenizer PASCAL_CASE_TOKENIZER
= new WordTokenizer() {
protected void startSentence(
StringBuffer buffer,
char ch) {
.append(Character
.toUpperCase(ch));
protected void startWord(
StringBuffer buffer,
char ch) {
.append(Character
.toUpperCase(ch));
protected void inWord(
StringBuffer buffer,
char ch) {
.append(Character
.toLowerCase(ch));
protected void startDigitSentence(
StringBuffer buffer,
char ch) {
.append(ch);
protected void startDigitWord(
StringBuffer buffer,
char ch) {
.append(ch);
protected void inDigitWord(
StringBuffer buffer,
char ch) {
.append(ch);
protected void inDelimiter(
StringBuffer buffer,
char ch) {
if (ch != UNDERSCORE) {
.append(ch);
private static final WordTokenizer UPPER_CASE_WITH_UNDERSCORES_TOKENIZER = new WordTokenizer() {
protected void startSentence(
StringBuffer buffer,
char ch) {
.append(Character
.toUpperCase(ch));
protected void startWord(
StringBuffer buffer,
char ch) {
if (!isDelimiter(buffer
.charAt(buffer
.length() - 1))) {
.append(UNDERSCORE);
.append(Character
.toUpperCase(ch));
protected void inWord(
StringBuffer buffer,
char ch) {
.append(Character
.toUpperCase(ch));
protected void startDigitSentence(
StringBuffer buffer,
char ch) {
.append(ch);
protected void startDigitWord(
StringBuffer buffer,
char ch) {
if (!isDelimiter(buffer
.charAt(buffer
.length() - 1))) {
.append(UNDERSCORE);
.append(ch);
protected void inDigitWord(
StringBuffer buffer,
char ch) {
.append(ch);
protected void inDelimiter(
StringBuffer buffer,
char ch) {
.append(ch);
private static final WordTokenizer LOWER_CASE_WITH_UNDERSCORES_TOKENIZER = new WordTokenizer() {
protected void startSentence(
StringBuffer buffer,
char ch) {
.append(Character
.toLowerCase(ch));
protected void startWord(
StringBuffer buffer,
char ch) {
if (!isDelimiter(buffer
.charAt(buffer
.length() - 1))) {
.append(UNDERSCORE);
.append(Character
.toLowerCase(ch));
protected void inWord(
StringBuffer buffer,
char ch) {
.append(Character
.toLowerCase(ch));
protected void startDigitSentence(
StringBuffer buffer,
char ch) {
.append(ch);
protected void startDigitWord(
StringBuffer buffer,
char ch) {
if (!isDelimiter(buffer
.charAt(buffer
.length() - 1))) {
.append(UNDERSCORE);
.append(ch);
protected void inDigitWord(
StringBuffer buffer,
char ch) {
.append(ch);
protected void inDelimiter(
StringBuffer buffer,
char ch) {
.append(ch);
* 解析出下列语法所构成的&code&SENTENCE&/code&。
SENTENCE = WORD (DELIMITER* WORD)*
WORD = UPPER_CASE_WORD | LOWER_CASE_WORD | TITLE_CASE_WORD | DIGIT_WORD
UPPER_CASE_WORD = UPPER_CASE_LETTER+
LOWER_CASE_WORD = LOWER_CASE_LETTER+
TITLE_CASE_WORD = UPPER_CASE_LETTER LOWER_CASE_LETTER+
DIGIT_WORD
UPPER_CASE_LETTER = Character.isUpperCase()
LOWER_CASE_LETTER = Character.isLowerCase()
= Character.isDigit()
NON_LETTER_DIGIT
= !Character.isUpperCase() && !Character.isLowerCase() && !Character.isDigit()
DELIMITER = WHITESPACE | NON_LETTER_DIGIT
private abstract static class WordTokenizer {
protected static final char UNDERSCORE = '_';
* Parse sentence。
public String parse(String str) {
if (StringUtil.isEmpty(str)) {
int length = str.length();
StringBuffer buffer = new StringBuffer(length);
for (int index = 0; index & index++) {
char ch = str.charAt(index);
// 忽略空白。
if (Character.isWhitespace(ch)) {
// 大写字母开始:UpperCaseWord或是TitleCaseWord。
if (Character.isUpperCase(ch)) {
int wordIndex = index + 1;
while (wordIndex & length) {
char wordChar = str.charAt(wordIndex);
if (Character.isUpperCase(wordChar)) {
wordIndex++;
} else if (Character.isLowerCase(wordChar)) {
wordIndex--;
// 1. wordIndex == length,说明最后一个字母为大写,以upperCaseWord处理之。
// 2. wordIndex == index,说明index处为一个titleCaseWord。
// 3. wordIndex & index,说明index到wordIndex - 1处全部是大写,以upperCaseWord处理。
if ((wordIndex == length) || (wordIndex & index)) {
index = parseUpperCaseWord(buffer, str, index, wordIndex);
index = parseTitleCaseWord(buffer, str, index);
// 小写字母开始:LowerCaseWord。
if (Character.isLowerCase(ch)) {
index = parseLowerCaseWord(buffer, str, index);
// 数字开始:DigitWord。
if (Character.isDigit(ch)) {
index = parseDigitWord(buffer, str, index);
// 非字母数字开始:Delimiter。
inDelimiter(buffer, ch);
return buffer.toString();
private int parseUpperCaseWord(StringBuffer buffer, String str, int index, int length) {
char ch = str.charAt(index++);
// 首字母,必然存在且为大写。
if (buffer.length() == 0) {
startSentence(buffer, ch);
startWord(buffer, ch);
// 后续字母,必为小写。
for (; index & index++) {
ch = str.charAt(index);
inWord(buffer, ch);
return index - 1;
private int parseLowerCaseWord(StringBuffer buffer, String str, int index) {
char ch = str.charAt(index++);
// 首字母,必然存在且为小写。
if (buffer.length() == 0) {
startSentence(buffer, ch);
startWord(buffer, ch);
// 后续字母,必为小写。
int length = str.length();
for (; index & index++) {
ch = str.charAt(index);
if (Character.isLowerCase(ch)) {
inWord(buffer, ch);
return index - 1;
private int parseTitleCaseWord(StringBuffer buffer, String str, int index) {
char ch = str.charAt(index++);
// 首字母,必然存在且为大写。
if (buffer.length() == 0) {
startSentence(buffer, ch);
startWord(buffer, ch);
// 后续字母,必为小写。
int length = str.length();
for (; index & index++) {
ch = str.charAt(index);
if (Character.isLowerCase(ch)) {
inWord(buffer, ch);
return index - 1;
private int parseDigitWord(StringBuffer buffer, String str, int index) {
char ch = str.charAt(index++);
// 首字符,必然存在且为数字。
if (buffer.length() == 0) {
startDigitSentence(buffer, ch);
startDigitWord(buffer, ch);
// 后续字符,必为数字。
int length = str.length();
for (; index & index++) {
ch = str.charAt(index);
if (Character.isDigit(ch)) {
inDigitWord(buffer, ch);
return index - 1;
protected boolean isDelimiter(char ch) {
return !Character.isUpperCase(ch) && !Character.isLowerCase(ch)
&& !Character.isDigit(ch);
protected abstract void startSentence(StringBuffer buffer, char ch);
protected abstract void startWord(StringBuffer buffer, char ch);
protected abstract void inWord(StringBuffer buffer, char ch);
protected abstract void startDigitSentence(StringBuffer buffer, char ch);
protected abstract void startDigitWord(StringBuffer buffer, char ch);
protected abstract void inDigitWord(StringBuffer buffer, char ch);
protected abstract void inDelimiter(StringBuffer buffer, char ch);
/* ============================================================================ */
字符串分割函数。
将字符串按指定分隔符分割。
/* ============================================================================ */
* 将字符串按空白字符分割。
* 分隔符不会出现在目标数组中,连续的分隔符就被看作一个。如果字符串为&code&null&/code&,则返回&code&null&/code&。
* StringUtil.split(null)
* StringUtil.split("")
* StringUtil.split("abc def")
= ["abc", "def"]
* StringUtil.split("abc
def") = ["abc", "def"]
* StringUtil.split(" abc ")
* @param str 要分割的字符串
* @return 分割后的字符串数组,如果原字符串为&code&null&/code&,则返回&code&null&/code&
public static String[] split(String str) {
return split(str, null, -1);
* 将字符串按指定字符分割。
* 分隔符不会出现在目标数组中,连续的分隔符就被看作一个。如果字符串为&code&null&/code&,则返回&code&null&/code&。
* StringUtil.split(null, *)
* StringUtil.split("", *)
* StringUtil.split("a.b.c", '.')
= ["a", "b", "c"]
* StringUtil.split("a..b.c", '.')
= ["a", "b", "c"]
* StringUtil.split("a:b:c", '.')
= ["a:b:c"]
* StringUtil.split("a b c", ' ')
= ["a", "b", "c"]
* @param str 要分割的字符串
* @param separatorChar 分隔符
* @return 分割后的字符串数组,如果原字符串为&code&null&/code&,则返回&code&null&/code&
public static String[] split(String str, char separatorChar) {
if (str == null) {
return null;
int length = str.length();
if (length == 0) {
return ArrayUtil.EMPTY_STRING_ARRAY;
List list = new ArrayList();
int i = 0;
int start = 0;
boolean match = false;
while (i & length) {
if (str.charAt(i) == separatorChar) {
if (match) {
list.add(str.substring(start, i));
match = false;
start = ++i;
match = true;
if (match) {
list.add(str.substring(start, i));
return (String[]) list.toArray(new String[list.size()]);
* 将字符串按指定字符分割。
* 分隔符不会出现在目标数组中,连续的分隔符就被看作一个。如果字符串为&code&null&/code&,则返回&code&null&/code&。
* StringUtil.split(null, *)
* StringUtil.split("", *)
* StringUtil.split("abc def", null)
= ["abc", "def"]
* StringUtil.split("abc def", " ")
= ["abc", "def"]
* StringUtil.split("abc
def", " ")
= ["abc", "def"]
* StringUtil.split(" ab:
= ["ab", "cd", "ef"]
* StringUtil.split("abc.def", "")
= ["abc.def"]
* @param str 要分割的字符串
* @param separatorChars 分隔符
* @return 分割后的字符串数组,如果原字符串为&code&null&/code&,则返回&code&null&/code&
public static String[] split(String str, String separatorChars) {
return split(str, separatorChars, -1);
* 将字符串按指定字符分割。
* 分隔符不会出现在目标数组中,连续的分隔符就被看作一个。如果字符串为&code&null&/code&,则返回&code&null&/code&。
* StringUtil.split(null, *, *)
* StringUtil.split("", *, *)
* StringUtil.split("ab cd ef", null, 0)
= ["ab", "cd", "ef"]
* StringUtil.split("
", null, 0)
= ["ab", "cd", "ef"]
* StringUtil.split("ab:cd::ef", ":", 0)
= ["ab", "cd", "ef"]
* StringUtil.split("ab:cd:ef", ":", 2)
= ["ab", "cdef"]
* StringUtil.split("abc.def", "", 2)
= ["abc.def"]
* @param str 要分割的字符串
* @param separatorChars 分隔符
* @param max 返回的数组的最大个数,如果小于等于0,则表示无限制
* @return 分割后的字符串数组,如果原字符串为&code&null&/code&,则返回&code&null&/code&
public static String[] split(String str, String separatorChars, int max) {
if (str == null) {
return null;
int length = str.length();
if (length == 0) {
return ArrayUtil.EMPTY_STRING_ARRAY;
List list = new ArrayList();
int sizePlus1 = 1;
int i = 0;
int start = 0;
boolean match = false;
if (separatorChars == null) {
// null表示使用空白作为分隔符
while (i & length) {
if (Character.isWhitespace(str.charAt(i))) {
if (match) {
if (sizePlus1++ == max) {
list.add(str.substring(start, i));
match = false;
start = ++i;
match = true;
} else if (separatorChars.length() == 1) {
// 优化分隔符长度为1的情形
char sep = separatorChars.charAt(0);
while (i & length) {
if (str.charAt(i) == sep) {
if (match) {
if (sizePlus1++ == max) {
list.add(str.substring(start, i));
match = false;
start = ++i;
match = true;
// 一般情形
while (i & length) {
if (separatorChars.indexOf(str.charAt(i)) &= 0) {
if (match) {
if (sizePlus1++ == max) {
list.add(str.substring(start, i));
match = false;
start = ++i;
match = true;
if (match) {
list.add(str.substring(start, i));
return (String[]) list.toArray(new String[list.size()]);
/* ============================================================================ */
字符串连接函数。
将多个对象按指定分隔符连接成字符串。
/* ============================================================================ */
* 将数组中的元素连接成一个字符串。
* StringUtil.join(null)
* StringUtil.join([])
* StringUtil.join([null])
* StringUtil.join(["a", "b", "c"]) = "abc"
* StringUtil.join([null, "", "a"]) = "a"
* @param array 要连接的数组
* @return 连接后的字符串,如果原数组为&code&null&/code&,则返回&code&null&/code&
public static String join(Object[] array) {
return join(array, null);
* 将数组中的元素连接成一个字符串。
* StringUtil.join(null, *)
* StringUtil.join([], *)
* StringUtil.join([null], *)
* StringUtil.join(["a", "b", "c"], ';')
* StringUtil.join(["a", "b", "c"], null) = "abc"
* StringUtil.join([null, "", "a"], ';')
* @param array 要连接的数组
* @param separator 分隔符
* @return 连接后的字符串,如果原数组为&code&null&/code&,则返回&code&null&/code&
public static String join(Object[] array, char separator) {
if (array == null) {
return null;
int arraySize = array.
int bufSize = (arraySize == 0) ? 0 : ((((array[0] == null) ? 16 : array[0].toString()
.length()) + 1) * arraySize);
StringBuffer buf = new StringBuffer(bufSize);
for (int i = 0; i & arrayS i++) {
if (i & 0) {
buf.append(separator);
if (array[i] != null) {
buf.append(array[i]);
return buf.toString();
* 将数组中的元素连接成一个字符串。
* StringUtil.join(null, *)
* StringUtil.join([], *)
* StringUtil.join([null], *)
* StringUtil.join(["a", "b", "c"], "--")
= "a--b--c"
* StringUtil.join(["a", "b", "c"], null)
* StringUtil.join(["a", "b", "c"], "")
* StringUtil.join([null, "", "a"], ',')
* @param array 要连接的数组
* @param separator 分隔符
* @return 连接后的字符串,如果原数组为&code&null&/code&,则返回&code&null&/code&
public static String join(Object[] array, String separator) {
if (array == null) {
return null;
if (separator == null) {
separator = EMPTY_STRING;
int arraySize = array.
// ArraySize ==
0: Len = 0
// ArraySize & 0:
Len = NofStrings *(len(firstString) + len(separator))
(估计大约所有的字符串都一样长)
int bufSize = (arraySize == 0) ? 0 : (arraySize * (((array[0] == null) ? 16 : array[0]
.toString().length()) + ((separator != null) ? separator.length() : 0)));
StringBuffer buf = new StringBuffer(bufSize);
for (int i = 0; i & arrayS i++) {
if ((separator != null) && (i & 0)) {
buf.append(separator);
if (array[i] != null) {
buf.append(array[i]);
return buf.toString();
* 将&code&Iterator&/code&中的元素连接成一个字符串。
* StringUtil.join(null, *)
* StringUtil.join([], *)
* StringUtil.join([null], *)
* StringUtil.join(["a", "b", "c"], "--")
= "a--b--c"
* StringUtil.join(["a", "b", "c"], null)
* StringUtil.join(["a", "b", "c"], "")
* StringUtil.join([null, "", "a"], ',')
* @param iterator 要连接的&code&Iterator&/code&
* @param separator 分隔符
* @return 连接后的字符串,如果原数组为&code&null&/code&,则返回&code&null&/code&
public static String join(Iterator iterator, char separator) {
if (iterator == null) {
return null;
StringBuffer buf = new StringBuffer(256); // Java默认值是16, 可能偏小
while (iterator.hasNext()) {
Object obj = iterator.next();
if (obj != null) {
buf.append(obj);
if (iterator.hasNext()) {
buf.append(separator);
return buf.toString();
* 将&code&Iterator&/code&中的元素连接成一个字符串。
* StringUtil.join(null, *)
* StringUtil.join([], *)
* StringUtil.join([null], *)
* StringUtil.join(["a", "b", "c"], "--")
= "a--b--c"
* StringUtil.join(["a", "b", "c"], null)
* StringUtil.join(["a", "b", "c"], "")
* StringUtil.join([null, "", "a"], ',')
* @param iterator 要连接的&code&Iterator&/code&
* @param separator 分隔符
* @return 连接后的字符串,如果原数组为&code&null&/code&,则返回&code&null&/code&
public static String join(Iterator iterator, String separator) {
if (iterator == null) {
return null;
StringBuffer buf = new StringBuffer(256); // Java默认值是16, 可能偏小
while (iterator.hasNext()) {
Object obj = iterator.next();
if (obj != null) {
buf.append(obj);
if ((separator != null) && iterator.hasNext()) {
buf.append(separator);
return buf.toString();
/* ============================================================================ */
字符串查找函数 && 字符或字符串。
在字符串中查找指定字符或字符串。
/* ============================================================================ */
* 在字符串中查找指定字符,并返回第一个匹配的索引值。如果字符串为&code&null&/code&或未找到,则返回&code&-1&/code&。
* StringUtil.indexOf(null, *)
* StringUtil.indexOf("", *)
* StringUtil.indexOf("aabaabaa", 'a') = 0
* StringUtil.indexOf("aabaabaa", 'b') = 2
* @param str 要扫描的字符串
* @param searchChar 要查找的字符
* @return 第一个匹配的索引值。如果字符串为&code&null&/code&或未找到,则返回&code&-1&/code&
public static int indexOf(String str, char searchChar) {
if ((str == null) || (str.length() == 0)) {
return -1;
return str.indexOf(searchChar);
* 在字符串中查找指定字符,并返回第一个匹配的索引值。如果字符串为&code&null&/code&或未找到,则返回&code&-1&/code&。
* StringUtil.indexOf(null, *, *)
* StringUtil.indexOf("", *, *)
* StringUtil.indexOf("aabaabaa", 'b', 0)
* StringUtil.indexOf("aabaabaa", 'b', 3)
* StringUtil.indexOf("aabaabaa", 'b', 9)
* StringUtil.indexOf("aabaabaa", 'b', -1) = 2
* @param str 要扫描的字符串
* @param searchChar 要查找的字符
* @param startPos 开始搜索的索引值,如果小于0,则看作0
* @return 第一个匹配的索引值。如果字符串为&code&null&/code&或未找到,则返回&code&-1&/code&
public static int indexOf(String str, char searchChar, int startPos) {
if ((str == null) || (str.length() == 0)) {
return -1;
return str.indexOf(searchChar, startPos);
* 在字符串中查找指定字符串,并返回第一个匹配的索引值。如果字符串为&code&null&/code&或未找到,则返回&code&-1&/code&。
* StringUtil.indexOf(null, *)
* StringUtil.indexOf(*, null)
* StringUtil.indexOf("", "")
* StringUtil.indexOf("aabaabaa", "a")
* StringUtil.indexOf("aabaabaa", "b")
* StringUtil.indexOf("aabaabaa", "ab") = 1
* StringUtil.indexOf("aabaabaa", "")
* @param str 要扫描的字符串
* @param searchStr 要查找的字符串
* @return 第一个匹配的索引值。如果字符串为&code&null&/code&或未找到,则返回&code&-1&/code&
public static int indexOf(String str, String searchStr) {
if ((str == null) || (searchStr == null)) {
return -1;
return str.indexOf(searchStr);
* 在字符串中查找指定字符串,并返回第一个匹配的索引值。如果字符串为&code&null&/code&或未找到,则返回&code&-1&/code&。
* StringUtil.indexOf(null, *, *)
* StringUtil.indexOf(*, null, *)
* StringUtil.indexOf("", "", 0)
* StringUtil.indexOf("aabaabaa", "a", 0)
* StringUtil.indexOf("aabaabaa", "b", 0)
* StringUtil.indexOf("aabaabaa", "ab", 0) = 1
* StringUtil.indexOf("aabaabaa", "b", 3)
* StringUtil.indexOf("aabaabaa", "b", 9)
* StringUtil.indexOf("aabaabaa", "b", -1) = 2
* StringUtil.indexOf("aabaabaa", "", 2)
* StringUtil.indexOf("abc", "", 9)
* @param str 要扫描的字符串
* @param searchStr 要查找的字符串
* @param startPos 开始搜索的索引值,如果小于0,则看作0
* @return 第一个匹配的索引值。如果字符串为&code&null&/code&或未找到,则返回&code&-1&/code&
public static int indexOf(String str, String searchStr, int startPos) {
if ((str == null) || (searchStr == null)) {
return -1;
// JDK1.3及以下版本的bug:不能正确处理下面的情况
if ((searchStr.length() == 0) && (startPos &= str.length())) {
return str.length();
return str.indexOf(searchStr, startPos);
* 在字符串中查找指定字符集合中的字符,并返回第一个匹配的起始索引。 如果字符串为&code&null&/code&,则返回&code&-1&/code&。
* 如果字符集合为&code&null&/code&或空,也返回&code&-1&/code&。
* StringUtil.indexOfAny(null, *)
* StringUtil.indexOfAny("", *)
* StringUtil.indexOfAny(*, null)
* StringUtil.indexOfAny(*, [])
* StringUtil.indexOfAny("zzabyycdxx",['z','a']) = 0
* StringUtil.indexOfAny("zzabyycdxx",['b','y']) = 3
* StringUtil.indexOfAny("aba", ['z'])
* @param str 要扫描的字符串
* @param searchChars 要搜索的字符集合
* @return 第一个匹配的索引值。如果字符串为&code&null&/code&或未找到,则返回&code&-1&/code&
public static int indexOfAny(String str, char[] searchChars) {
if ((str == null) || (str.length() == 0) || (searchChars == null)
|| (searchChars.length == 0)) {
return -1;
for (int i = 0; i & str.length(); i++) {
char ch = str.charAt(i);
for (int j = 0; j & searchChars. j++) {
if (searchChars[j] == ch) {
return -1;
* 在字符串中查找指定字符集合中的字符,并返回第一个匹配的起始索引。 如果字符串为&code&null&/code&,则返回&code&-1&/code&。
* 如果字符集合为&code&null&/code&或空,也返回&code&-1&/code&。
* StringUtil.indexOfAny(null, *)
* StringUtil.indexOfAny("", *)
* StringUtil.indexOfAny(*, null)
* StringUtil.indexOfAny(*, "")
* StringUtil.indexOfAny("zzabyycdxx", "za") = 0
* StringUtil.indexOfAny("zzabyycdxx", "by") = 3
* StringUtil.indexOfAny("aba","z")
* @param str 要扫描的字符串
* @param searchChars 要搜索的字符集合
* @return 第一个匹配的索引值。如果字符串为&code&null&/code&或未找到,则返回&code&-1&/code&
public static int indexOfAny(String str, String searchChars) {
if ((str == null) || (str.length() == 0) || (searchChars == null)
|| (searchChars.length() == 0)) {
return -1;
for (int i = 0; i & str.length(); i++) {
char ch = str.charAt(i);
for (int j = 0; j & searchChars.length(); j++) {
if (searchChars.charAt(j) == ch) {
return -1;
* 在字符串中查找指定字符串集合中的字符串,并返回第一个匹配的起始索引。 如果字符串为&code&null&/code&,则返回&code&-1&/code&。
* 如果字符串集合为&code&null&/code&或空,也返回&code&-1&/code&。
* 如果字符串集合包括&code&""&/code&,并且字符串不为&code&null&/code&,则返回&code&str.length()&/code&
* StringUtil.indexOfAny(null, *)
* StringUtil.indexOfAny(*, null)
* StringUtil.indexOfAny(*, [])
* StringUtil.indexOfAny("zzabyycdxx", ["ab","cd"])
* StringUtil.indexOfAny("zzabyycdxx", ["cd","ab"])
* StringUtil.indexOfAny("zzabyycdxx", ["mn","op"])
* StringUtil.indexOfAny("zzabyycdxx", ["zab","aby"]) = 1
* StringUtil.indexOfAny("zzabyycdxx", [""])
* StringUtil.indexOfAny("", [""])
* StringUtil.indexOfAny("", ["a"])
* @param str 要扫描的字符串
* @param searchStrs 要搜索的字符串集合
* @return 第一个匹配的索引值。如果字符串为&code&null&/code&或未找到,则返回&code&-1&/code&
public static int indexOfAny(String str, String[] searchStrs) {
if ((str == null) || (searchStrs == null)) {
return -1;
int sz = searchStrs.
// String's can't have a MAX_VALUEth index.
int ret = Integer.MAX_VALUE;
int tmp = 0;
for (int i = 0; i & i++) {
String search = searchStrs[i];
if (search == null) {
tmp = str.indexOf(search);
if (tmp == -1) {
if (tmp & ret) {
return (ret == Integer.MAX_VALUE) ? (-1) :
* 在字符串中查找不在指定字符集合中的字符,并返回第一个匹配的起始索引。 如果字符串为&code&null&/code&,则返回&code&-1&/code&。
* 如果字符集合为&code&null&/code&或空,也返回&code&-1&/code&。
* StringUtil.indexOfAnyBut(null, *)
* StringUtil.indexOfAnyBut("", *)
* StringUtil.indexOfAnyBut(*, null)
* StringUtil.indexOfAnyBut(*, [])
* StringUtil.indexOfAnyBut("zzabyycdxx",'za')
* StringUtil.indexOfAnyBut("zzabyycdxx", 'by')
* StringUtil.indexOfAnyBut("aba", 'ab')
* @param str 要扫描的字符串
* @param searchChars 要搜索的字符集合
* @return 第一个匹配的索引值。如果字符串为&code&null&/code&或未找到,则返回&code&-1&/code&
public static int indexOfAnyBut(String str, char[] searchChars) {
if ((str == null) || (str.length() == 0) || (searchChars == null)
|| (searchChars.length == 0)) {
return -1;
outer: for (int i = 0; i & str.length(); i++) {
char ch = str.charAt(i);
for (int j = 0; j & searchChars. j++) {
if (searchChars[j] == ch) {
return -1;
* 在字符串中查找不在指定字符集合中的字符,并返回第一个匹配的起始索引。 如果字符串为&code&null&/code&,则返回&code&-1&/code&。
* 如果字符集合为&code&null&/code&或空,也返回&code&-1&/code&。
* StringUtil.indexOfAnyBut(null, *)
* StringUtil.indexOfAnyBut("", *)
* StringUtil.indexOfAnyBut(*, null)
* StringUtil.indexOfAnyBut(*, "")
* StringUtil.indexOfAnyBut("zzabyycdxx", "za") = 3
* StringUtil.indexOfAnyBut("zzabyycdxx", "by") = 0
* StringUtil.indexOfAnyBut("aba","ab")
* @param str 要扫描的字符串
* @param searchChars 要搜索的字符集合
* @return 第一个匹配的索引值。如果字符串为&code&null&/code&或未找到,则返回&code&-1&/code&
public static int indexOfAnyBut(String str, String searchChars) {
if ((str == null) || (str.length() == 0) || (searchChars == null)
|| (searchChars.length() == 0)) {
return -1;
for (int i = 0; i & str.length(); i++) {
if (searchChars.indexOf(str.charAt(i)) & 0) {
return -1;
* 从字符串尾部开始查找指定字符,并返回第一个匹配的索引值。如果字符串为&code&null&/code&或未找到,则返回&code&-1&/code&。
* StringUtil.lastIndexOf(null, *)
* StringUtil.lastIndexOf("", *)
* StringUtil.lastIndexOf("aabaabaa", 'a') = 7
* StringUtil.lastIndexOf("aabaabaa", 'b') = 5
* @param str 要扫描的字符串
* @param searchChar 要查找的字符
* @return 第一个匹配的索引值。如果字符串为&code&null&/code&或未找到,则返回&code&-1&/code&
public static int lastIndexOf(String str, char searchChar) {
if ((str == null) || (str.length() == 0)) {
return -1;
return str.lastIndexOf(searchChar);
* 从字符串尾部开始查找指定字符,并返回第一个匹配的索引值。如果字符串为&code&null&/code&或未找到,则返回&code&-1&/code&。
* StringUtil.lastIndexOf(null, *, *)
* StringUtil.lastIndexOf("", *,
* StringUtil.lastIndexOf("aabaabaa", 'b', 8)
* StringUtil.lastIndexOf("aabaabaa", 'b', 4)
* StringUtil.lastIndexOf("aabaabaa", 'b', 0)
* StringUtil.lastIndexOf("aabaabaa", 'b', 9)
* StringUtil.lastIndexOf("aabaabaa", 'b', -1) = -1
* StringUtil.lastIndexOf("aabaabaa", 'a', 0)
* @param str 要扫描的字符串
* @param searchChar 要查找的字符
* @param startPos 从指定索引开始向前搜索
* @return 第一个匹配的索引值。如果字符串为&code&null&/code&或未找到,则返回&code&-1&/code&
public static int lastIndexOf(String str, char searchChar, int startPos) {
if ((str == null) || (str.length() == 0)) {
return -1;
return str.lastIndexOf(searchChar, startPos);
* 从字符串尾部开始查找指定字符串,并返回第一个匹配的索引值。如果字符串为&code&null&/code&或未找到,则返回&code&-1&/code&。
* StringUtil.lastIndexOf(null, *)
* StringUtil.lastIndexOf("", *)
* StringUtil.lastIndexOf("aabaabaa", 'a') = 7
* StringUtil.lastIndexOf("aabaabaa", 'b') = 5
* @param str 要扫描的字符串
* @param searchStr 要查找的字符串
* @return 第一个匹配的索引值。如果字符串为&code&null&/code&或未找到,则返回&code&-1&/code&
public static int lastIndexOf(String str, String searchStr) {
if ((str == null) || (searchStr == null)) {
return -1;
return str.lastIndexOf(searchStr);
* 从字符串尾部开始查找指定字符串,并返回第一个匹配的索引值。如果字符串为&code&null&/code&或未找到,则返回&code&-1&/code&。
* StringUtil.lastIndexOf(null, *, *)
* StringUtil.lastIndexOf(*, null, *)
* StringUtil.lastIndexOf("aabaabaa", "a", 8)
* StringUtil.lastIndexOf("aabaabaa", "b", 8)
* StringUtil.lastIndexOf("aabaabaa", "ab", 8) = 4
* StringUtil.lastIndexOf("aabaabaa", "b", 9)
* StringUtil.lastIndexOf("aabaabaa", "b", -1) = -1
* StringUtil.lastIndexOf("aabaabaa", "a", 0)
* StringUtil.lastIndexOf("aabaabaa", "b", 0)
* @param str 要扫描的字符串
* @param searchStr 要查找的字符串
* @param startPos 从指定索引开始向前搜索
* @return 第一个匹配的索引值。如果字符串为&code&null&/code&或未找到,则返回&code&-1&/code&
public static int lastIndexOf(String str, String searchStr, int startPos) {
if ((str == null) || (searchStr == null)) {
return -1;
return str.lastIndexOf(searchStr, startPos);
* 从字符串尾部开始查找指定字符串集合中的字符串,并返回第一个匹配的起始索引。 如果字符串为&code&null&/code&,则返回&code&-1&/code&。
* 如果字符串集合为&code&null&/code&或空,也返回&code&-1&/code&。
* 如果字符串集合包括&code&""&/code&,并且字符串不为&code&null&/code&,则返回&code&str.length()&/code&
* StringUtil.lastIndexOfAny(null, *)
* StringUtil.lastIndexOfAny(*, null)
* StringUtil.lastIndexOfAny(*, [])
* StringUtil.lastIndexOfAny(*, [null])
* StringUtil.lastIndexOfAny("zzabyycdxx", ["ab","cd"]) = 6
* StringUtil.lastIndexOfAny("zzabyycdxx", ["cd","ab"]) = 6
* StringUtil.lastIndexOfAny("zzabyycdxx", ["mn","op"]) = -1
* StringUtil.lastIndexOfAny("zzabyycdxx", ["mn","op"]) = -1
* StringUtil.lastIndexOfAny("zzabyycdxx", ["mn",""])
* @param str 要扫描的字符串
* @param searchStrs 要搜索的字符串集合
* @return 第一个匹配的索引值。如果字符串为&code&null&/code&或未找到,则返回&code&-1&/code&
public static int lastIndexOfAny(String str, String[] searchStrs) {
if ((str == null) || (searchStrs == null)) {
return -1;
int searchStrsLength = searchStrs.
int index = -1;
int tmp = 0;
for (int i = 0; i & searchStrsL i++) {
String search = searchStrs[i];
if (search == null) {
tmp = str.lastIndexOf(search);
if (tmp & index) {
* 检查字符串中是否包含指定的字符。如果字符串为&code&null&/code&,将返回&code&false&/code&。
* StringUtil.contains(null, *)
* StringUtil.contains("", *)
* StringUtil.contains("abc", 'a') = true
* StringUtil.contains("abc", 'z') = false
* @param str 要扫描的字符串
* @param searchChar 要查找的字符
* @return 如果找到,则返回&code&true&/code&
public static boolean contains(String str, char searchChar) {
if ((str == null) || (str.length() == 0)) {
return false;
return str.indexOf(searchChar) &= 0;
* 检查字符串中是否包含指定的字符串。如果字符串为&code&null&/code&,将返回&code&false&/code&。
* StringUtil.contains(null, *)
* StringUtil.contains(*, null)
* StringUtil.contains("", "")
* StringUtil.contains("abc", "")
* StringUtil.contains("abc", "a")
* StringUtil.contains("abc", "z")
* @param str 要扫描的字符串
* @param searchStr 要查找的字符串
* @return 如果找到,则返回&code&true&/code&
public static boolean contains(String str, String searchStr) {
if ((str == null) || (searchStr == null)) {
return false;
return str.indexOf(searchStr) &= 0;
* 检查字符串是是否只包含指定字符集合中的字符。
* 如果字符串为&code&null&/code&,则返回&code&false&/code&。
* 如果字符集合为&code&null&/code&则返回&code&false&/code&。 但是空字符串永远返回&code&true&/code&.
* StringUtil.containsOnly(null, *)
* StringUtil.containsOnly(*, null)
* StringUtil.containsOnly("", *)
* StringUtil.containsOnly("ab", '')
* StringUtil.containsOnly("abab", 'abc') = true
* StringUtil.containsOnly("ab1", 'abc')
* StringUtil.containsOnly("abz", 'abc')
* @param str 要扫描的字符串
* @param valid 要查找的字符串
* @return 如果找到,则返回&code&true&/code&
public static boolean containsOnly(String str, char[] valid) {
if ((valid == null) || (str == null)) {
return false;
if (str.length() == 0) {
return true;
if (valid.length == 0) {
return false;
return indexOfAnyBut(str, valid) == -1;
* 检查字符串是是否只包含指定字符集合中的字符。
* 如果字符串为&code&null&/code&,则返回&code&false&/code&。
* 如果字符集合为&code&null&/code&则返回&code&false&/code&。 但是空字符串永远返回&code&true&/code&.
* StringUtil.containsOnly(null, *)
* StringUtil.containsOnly(*, null)
* StringUtil.containsOnly("", *)
* StringUtil.containsOnly("ab", "")
* StringUtil.containsOnly("abab", "abc") = true
* StringUtil.containsOnly("ab1", "abc")
* StringUtil.containsOnly("abz", "abc")
* @param str 要扫描的字符串
* @param valid 要查找的字符串
* @return 如果找到,则返回&code&true&/code&
public static boolean containsOnly(String str, String valid) {
if ((str == null) || (valid == null)) {
return false;
return containsOnly(str, valid.toCharArray());
* 检查字符串是是否不包含指定字符集合中的字符。
* 如果字符串为&code&null&/code&,则返回&code&false&/code&。 如果字符集合为&code&null&/code&则返回&code&true&/code&。
* 但是空字符串永远返回&code&true&/code&.
* StringUtil.containsNone(null, *)
* StringUtil.containsNone(*, null)
* StringUtil.containsNone("", *)
* StringUtil.containsNone("ab", '')
* StringUtil.containsNone("abab", 'xyz') = true
* StringUtil.containsNone("ab1", 'xyz')
* StringUtil.containsNone("abz", 'xyz')
* @param str 要扫描的字符串
* @param invalid 要查找的字符串
* @return 如果找到,则返回&code&true&/code&
public static boolean containsNone(String str, char[] invalid) {
if ((str == null) || (invalid == null)) {
return true;
int strSize = str.length();
int validSize = invalid.
for (int i = 0; i & strS i++) {
char ch = str.charAt(i);
for (int j = 0; j & validS j++) {
if (invalid[j] == ch) {
return false;
return true;
* 检查字符串是是否不包含指定字符集合中的字符。
* 如果字符串为&code&null&/code&,则返回&code&false&/code&。 如果字符集合为&code&null&/code&则返回&code&true&/code&。
* 但是空字符串永远返回&code&true&/code&.
* StringUtil.containsNone(null, *)
* StringUtil.containsNone(*, null)
* StringUtil.containsNone("", *)
* StringUtil.containsNone("ab", "")
* StringUtil.containsNone("abab", "xyz") = true
* StringUtil.containsNone("ab1", "xyz")
* StringUtil.containsNone("abz", "xyz")
* @param str 要扫描的字符串
* @param invalidChars 要查找的字符串
* @return 如果找到,则返回&code&true&/code&
public static boolean containsNone(String str, String invalidChars) {
if ((str == null) || (invalidChars == null)) {
return true;
return containsNone(str, invalidChars.toCharArray());
* 取得指定子串在字符串中出现的次数。
* 如果字符串为&code&null&/code&或空,则返回&code&0&/code&。
* StringUtil.countMatches(null, *)
* StringUtil.countMatches("", *)
* StringUtil.countMatches("abba", null)
* StringUtil.countMatches("abba", "")
* StringUtil.countMatches("abba", "a")
* StringUtil.countMatches("abba", "ab")
* StringUtil.countMatches("abba", "xxx") = 0
* @param str 要扫描的字符串
* @param subStr 子字符串
* @return 子串在字符串中出现的次数,如果字符串为&code&null&/code&或空,则返回&code&0&/code&
public static int countMatches(String str, String subStr) {
if ((str == null) || (str.length() == 0) || (subStr == null) || (subStr.length() == 0)) {
int count = 0;
int index = 0;
while ((index = str.indexOf(subStr, index)) != -1) {
index += subStr.length();
/* ============================================================================ */
取子串函数。
/* ============================================================================ */
* 取指定字符串的子串。
* 负的索引代表从尾部开始计算。如果字符串为&code&null&/code&,则返回&code&null&/code&。
* StringUtil.substring(null, *)
* StringUtil.substring("", *)
* StringUtil.substring("abc", 0)
* StringUtil.substring("abc", 2)
* StringUtil.substring("abc", 4)
* StringUtil.substring("abc", -2) = "bc"
* StringUtil.substring("abc", -4) = "abc"
* @param str 字符串
* @param start 起始索引,如果为负数,表示从尾部查找
* @return 子串,如果原始串为&code&null&/code&,则返回&code&null&/code&
public static String substring(String str, int start) {
if (str == null) {
return null;
if (start & 0) {
start = str.length() +
if (start & 0) {
start = 0;
if (start & str.length()) {
return EMPTY_STRING;
return str.substring(start);
* 取指定字符串的子串。
* 负的索引代表从尾部开始计算。如果字符串为&code&null&/code&,则返回&code&null&/code&。
* StringUtil.substring(null, *, *)
* StringUtil.substring("", * ,
* StringUtil.substring("abc", 0, 2)
* StringUtil.substring("abc", 2, 0)
* StringUtil.substring("abc", 2, 4)
* StringUtil.substring("abc", 4, 6)
* StringUtil.substring("abc", 2, 2)
* StringUtil.substring("abc", -2, -1) = "b"
* StringUtil.substring("abc", -4, 2)
* @param str 字符串
* @param start 起始索引,如果为负数,表示从尾部计算
* @param end 结束索引(不含),如果为负数,表示从尾部计算
* @return 子串,如果原始串为&code&null&/code&,则返回&code&null&/code&
public static String substring(String str, int start, int end) {
if (str == null) {
return null;
if (end & 0) {
end = str.length() +
if (start & 0) {
start = str.length() +
if (end & str.length()) {
end = str.length();
if (start & end) {
return EMPTY_STRING;
if (start & 0) {
start = 0;
if (end & 0) {
return str.substring(start, end);
* 取得长度为指定字符数的最左边的子串。
* StringUtil.left(null, *)
* StringUtil.left(*, -ve)
* StringUtil.left("", *)
* StringUtil.left("abc", 0)
* StringUtil.left("abc", 2)
* StringUtil.left("abc", 4)
* @param str 字符串
* @param len 最左子串的长度
* @return 子串,如果原始字串为&code&null&/code&,则返回&code&null&/code&
public static String left(String str, int len) {
if (str == null) {
return null;
if (len & 0) {
return EMPTY_STRING;
if (str.length() &= len) {
return str.substring(0, len);
* 取得长度为指定字符数的最右边的子串。
* StringUtil.right(null, *)
* StringUtil.right(*, -ve)
* StringUtil.right("", *)
* StringUtil.right("abc", 0)
* StringUtil.right("abc", 2)
* StringUtil.right("abc", 4)
* @param str 字符串
* @param len 最右子串的长度
* @return 子串,如果原始字串为&code&null&/code&,则返回&code&null&/code&
public static String right(String str, int len) {
if (str == null) {
return null;
if (len & 0) {
return EMPTY_STRING;
if (str.length() &= len) {
return str.substring(str.length() - len);
* 取得从指定索引开始计算的、长度为指定字符数的子串。
* StringUtil.mid(null, *, *)
* StringUtil.mid(*, *, -ve)
* StringUtil.mid("", 0, *)
* StringUtil.mid("abc", 0, 2)
* StringUtil.mid("abc", 0, 4)
* StringUtil.mid("abc", 2, 4)
* StringUtil.mid("abc", 4, 2)
* StringUtil.mid("abc", -2, 2)
* @param str 字符串
* @param pos 起始索引,如果为负数,则看作&code&0&/code&
* @param len 子串的长度,如果为负数,则看作长度为&code&0&/code&
* @return 子串,如果原始字串为&code&null&/code&,则返回&code&null&/code&
public static String mid(String str, int pos, int len) {
if (str == null) {
return null;
if ((len & 0) || (pos & str.length())) {
return EMPTY_STRING;
if (pos & 0) {
if (str.length() &= (pos + len)) {
return str.substring(pos);
return str.substring(pos, pos + len);
/* ============================================================================ */
搜索并取子串函数。
/* ============================================================================ */
* 取得第一个出现的分隔子串之前的子串。
* 如果字符串为&code&null&/code&,则返回&code&null&/code&。 如果分隔子串为&code&null&/code&或未找到该子串,则返回原字符串。
* StringUtil.substringBefore(null, *)
* StringUtil.substringBefore("", *)
* StringUtil.substringBefore("abc", "a")
* StringUtil.substringBefore("abcba", "b") = "a"
* StringUtil.substringBefore("abc", "c")
* StringUtil.substringBefore("abc", "d")
* StringUtil.substringBefore("abc", "")
* StringUtil.substringBefore("abc", null)
* @param str 字符串
* @param separator 要搜索的分隔子串
* @return 子串,如果原始串为&code&null&/code&,则返回&code&null&/code&
public static String substringBefore(String str, String separator) {
if ((str == null) || (separator == null) || (str.length() == 0)) {
if (separator.length() == 0) {
return EMPTY_STRING;
int pos = str.indexOf(separator);
if (pos == -1) {
return str.substring(0, pos);
* 取得第一个出现的分隔子串之后的子串。
* 如果字符串为&code&null&/code&,则返回&code&null&/code&。 如果分隔子串为&code&null&/code&或未找到该子串,则返回原字符串。
* StringUtil.substringAfter(null, *)
* StringUtil.substringAfter("", *)
* StringUtil.substringAfter(*, null)
* StringUtil.substringAfter("abc", "a")
* StringUtil.substringAfter("abcba", "b") = "cba"
* StringUtil.substringAfter("abc", "c")
* StringUtil.substringAfter("abc", "d")
* StringUtil.substringAfter("abc", "")
* @param str 字符串
* @param separator 要搜索的分隔子串
* @return 子串,如果原始串为&code&null&/code&,则返回&code&null&/code&
public static String substringAfter(String str, String separator) {
if ((str == null) || (str.length() == 0)) {
if (separator == null) {
return EMPTY_STRING;
int pos = str.indexOf(separator);
if (pos == -1) {
return EMPTY_STRING;
return str.substring(pos + separator.length());
* 取得最后一个的分隔子串之前的子串。
* 如果字符串为&code&}

我要回帖

更多关于 buffer是什么意思 的文章

更多推荐

版权声明:文章内容来源于网络,版权归原作者所有,如有侵权请点击这里与我们联系,我们将及时删除。

点击添加站长微信