一:将数组中的字符转换为一个字符串
1、 将数组中的字符转换为一个字符串
String[3] s={"a","b","c"}
StringUtil.convString(s)="a,b,c" ;
2,static public String converString(String strToConv, String conv)
@param strToConv 要转换的字符串
@param conv 分隔符,默认以逗号分隔
String[3] s={"a","b","c"}
StringUtil.convString(s,"@")="a@b@c"
二:空值处理
3,空值检测:
public static boolean isEmpty(String str)
@param str the String to check, may be null
@return true if the String is empty or null
判断一个字符串是否为空,空格作非空处理。
StringUtils.isEmpty(null) = true ;StringUtils.isEmpty("") = true; StringUtils.isEmpty(" ") = false;
StringUtils.isEmpty("bob") = false ;StringUtils.isEmpty(" bob ") = false ;
4,非空处理:
public static boolean isNotEmpty(String str)
@param str the String to check, may be null
@return true if the String is not empty and not null
判断一个字符串是否非空,空格作非空处理.
StringUtils.isNotEmpty(null) = false; StringUtils.isNotEmpty("") = false ;StringUtils.isNotEmpty(" ") = true;
StringUtils.isNotEmpty("bob") = true ;StringUtils.isNotEmpty(" bob ") = true ;
5, 去空格处理 :返回去除空格后的对象,若是null,返回null
public static String trim(String str)
@param str the String to be trimmed, may be null
@return the trimmed string, null if null String input
格式化一个字符串中的空格,有非空判断处理;
StringUtils.trim(null) = null; StringUtils.trim("") = "" ;StringUtils.trim(" ") = ""; StringUtils.trim("abc") = "abc";
StringUtils.trim(" abc ") = "abc" ;
6,去空格处理 :返回去除空格后的对象,若是null/""/" ",返回null
public static String trimToNull(String str)
@param str the String to be trimmed, may be null
格式化一个字符串中的空格,有非空判断处理,如果为空返回null;
StringUtils.trimToNull(null) = null ;StringUtils.trimToNull("") = null ;StringUtils.trimToNull(" ") = null ;
StringUtils.trimToNull("abc") = "abc"; StringUtils.trimToNull(" abc ") = "abc";
7,去空格处理 :返回去除空格后的对象,若是null/""/" ",返回""
public static String trimToEmpty(String str)
@param str the String to be trimmed, may be null
格式化一个字符串中的空格,有非空判断处理,如果为空返回"";
StringUtils.trimToEmpty(null) = "" ;StringUtils.trimToEmpty("") = "" ;StringUtils.trimToEmpty(" ") = "" ;
StringUtils.trimToEmpty("abc") = "abc" ;StringUtils.trimToEmpty(" abc ") = "abc";
三:字符串比较:
9,public static boolean equals(String str1, String str2)
判断两个字符串是否相等,有非空处理,关注大小写。
StringUtils.equals(null, null) = true; StringUtils.equals(null, "abc") = false; StringUtils.equals("abc", null) = false;
StringUtils.equals("abc", "abc") = true ;StringUtils.equals("abc", "ABC") = false ;
10,public static boolean equalsIgnoreCase(String str1, String str2)
判断两个字符串是否相等,有非空处理。忽略大小写
StringUtils.equalsIgnoreCase(null, null) = true ;StringUtils.equalsIgnoreCase(null, "abc") = false ;
StringUtils.equalsIgnoreCase("abc", null) = false ;StringUtils.equalsIgnoreCase("abc", "abc") = true ;
StringUtils.equalsIgnoreCase("abc", "ABC") = true ;
四:IndexOf 处理
11,public static int indexOf(String str, String searchStr)
@param str the String to check, may be null
@param searchStr the String to find, may be null
@return the first index of the search String
返回要查找的字符串第一次所在位置,有非空处理
StringUtils.indexOf(null, *) = -1; StringUtils.indexOf(*, null) = -1 ;StringUtils.indexOf("", "") = 0 ;
StringUtils.indexOf("aabaabaa", "a") = 0; StringUtils.indexOf("aabaabaa", "b") = 2 ;StringUtils.indexOf("aabaabaa", "ab") = 1;
StringUtils.indexOf("aabaabaa", "") = 0 ;
12,public static int indexOf(String str, String searchStr, int startPos)
@param str the String to check, may be null
@param searchStr the String to find, may be null
@param startPos the start position, negative treated as zero
@return the first index of the search String, -1 if no match or null string input
返回要由指定位置开始查找的字符串第一次所在位置,有非空处理
StringUtils.indexOf(null, *, *) = -1; StringUtils.indexOf(*, null, *) = -1 ;StringUtils.indexOf("", "", 0) = 0 ;
StringUtils.indexOf("aabaabaa", "a", 0) = 0 ;StringUtils.indexOf("aabaabaa", "b", 0) = 2 ;StringUtils.indexOf("aabaabaa", "ab", 0) = 1;
StringUtils.indexOf("aabaabaa", "b", 3) = 5; StringUtils.indexOf("aabaabaa", "b", 9) = -1; StringUtils.indexOf("aabaabaa", "b", -1) = 2;
StringUtils.indexOf("aabaabaa", "", 2) = 2 ;StringUtils.indexOf("abc", "", 9) = 3 ;
五:子字符串处理
13,public static String substring(String str, int start)
@param str the String to get the substring from, may be null
@param start the position to start from, negative means
count back from the end of the String by this many characters
@return substring from start position, null if null String input
返回指定位置开始的字符串中的所有字符, start>0表示从左向右, start<0表示从右向左, start=0则从左第一位开始
StringUtils.substring(null, *) = null ;StringUtils.substring("", *) = "" ;StringUtils.substring("abc", 0) = "abc";
StringUtils.substring("abc", 2) = "c" ;StringUtils.substring("abc", 4) = "" ;StringUtils.substring("abc", -2) = "bc";
StringUtils.substring("abc", -4) = "abc" ;
14,public static String substring(String str, int start, int end)
@param str the String to get the substring from, may be null
@param start the position to start from, negative means
count back from the end of the String by this many characters
@param end the position to end at (exclusive), negative means
count back from the end of the String by this many characters
@return substring from start position to end positon, null if null String input
返回由开始位置到结束位置之间的子字符串,
start>0&&end>0从左开始(包括左)到右结束(不包括右),
start<0&&end<0从右开始(包括右),再向左数到end结束(包括end)
StringUtils.substring(null, *, *) = null ;StringUtils.substring("", * , *) = "";
StringUtils.substring("abc", 0, 2) = "ab" ;StringUtils.substring("abc", 2, 0) = ""; StringUtils.substring("abc", 2, 4) = "c";
StringUtils.substring("abc", 4, 6) = "" ;StringUtils.substring("abc", 2, 2) = ""; StringUtils.substring("abc", -2, -1) = "b";
StringUtils.substring("abc", -4, 2) = "ab" ;
15,public static String substringBefore(String str, String separator)
@param str the String to get a substring from, may be null
@param separator the String to search for, may be null
@return the substring before the first occurance of the separator, null if null String input
Gets the substring before the first occurance of a separator.
The separator is not returned.
A null string input will return null.
An empty ("") string input will return the empty string.
A null separator will return the input string.
返回指定字符串之前的所有字符
StringUtils.substringBefore(null, *) = null; StringUtils.substringBefore("", *) = ""; StringUtils.substringBefore("abc", "a") = "";
StringUtils.substringBefore("abcba", "b") = "a"; StringUtils.substringBefore("abc", "c") = "ab";
StringUtils.substringBefore("abc", "d") = "abc" ;StringUtils.substringBefore("abc", "") = "" ;
StringUtils.substringBefore("abc", null) = "abc" ;
16,public static String substringAfter(String str, String separator)
@param str the String to get a substring from, may be null
@param separator the String to search for, may be null
@return the substring after the first occurance of the separator, null if null String input
Gets the substring after the first occurance of a separator.
The separator is not returned.
A null string input will return null.
An empty ("") string input will return the empty string.
A null separator will return the empty string if the input string is not null.
返回指定字符串之后的所有字符
StringUtils.substringAfter(null, *) = null; StringUtils.substringAfter("", *) = "" ;StringUtils.substringAfter(*, null) = "";
StringUtils.substringAfter("abc", "a") = "bc"; StringUtils.substringAfter("abcba", "b") = "cba";
StringUtils.substringAfter("abc", "c") = ""; StringUtils.substringAfter("abc", "d") = "" ;StringUtils.substringAfter("abc", "") = "abc" ;
17,public static String substringBeforeLast(String str, String separator)
Gets the substring before the last occurance of a separator.
The separator is not returned.
A null string input will return null.
An empty ("") string input will return the empty string.
An empty or null separator will return the input string.
返回最后一个指定字符串之前的所有字符
StringUtils.substringBeforeLast(null, *) = null ;StringUtils.substringBeforeLast("", *) = "";
StringUtils.substringBeforeLast("abcba", "b") = "abc" ;StringUtils.substringBeforeLast("abc", "c") = "ab";
StringUtils.substringBeforeLast("a", "a") = ""; StringUtils.substringBeforeLast("a", "z") = "a";
StringUtils.substringBeforeLast("a", null) = "a" ;StringUtils.substringBeforeLast("a", "") = "a";
18,public static String substringAfterLast(String str, String separator)
Gets the substring after the last occurance of a separator. The separator is not returned.
A null string input will return null. An empty ("") string input will return the empty string.
An empty or null separator will return the empty string if the input string is not null.
返回最后一个指定字符串之后的所有字符
StringUtils.substringAfterLast(null, *) = null; StringUtils.substringAfterLast("", *) = "" ;StringUtils.substringAfterLast(*, "") = "";
StringUtils.substringAfterLast(*, null) = "" ;StringUtils.substringAfterLast("abc", "a") = "bc" ;
StringUtils.substringAfterLast("abcba", "b") = "a"; StringUtils.substringAfterLast("abc", "c") = "";
StringUtils.substringAfterLast("a", "a") = "" ;StringUtils.substringAfterLast("a", "z") = "";
六: Replacing(字符串替换)
19,public static String replace(String text, String repl, String with)
@param text text to search and replace in, may be null
@param repl the String to search for, may be null
@param with the String to replace with, may be null
@return the text with any replacements processed, null if null String input
Replaces all occurances of a String within another String. A null reference passed to this method is a no-op.
以指定字符串替换原来字符串的的指定字符串 ,repl是所给字符串中存在的需要被替换的字符串,with是需要被替换成的新字符串;
StringUtils.replace(null, *, *) = null; StringUtils.replace("", *, *) = ""; StringUtils.replace("aba", null, null) = "aba";
StringUtils.replace("aba", null, null) = "aba"; StringUtils.replace("aba", "a", null) = "aba";
StringUtils.replace("aba", "a", "") = "aba"; StringUtils.replace("aba", "a", "z") = "zbz" ;
20,public static String replace(String text, String repl, String with, int max)
@param text text to search and replace in, may be null
@param repl the String to search for, may be null
@param with the String to replace with, may be null
@param max maximum number of values to replace, or -1 if no maximum
@return the text with any replacements processed,null if null String input
Replaces a String with another String inside a larger String, for the first max values of the search String.
A null reference passed to this method is a no-op.
以指定字符串最大替换原来字符串的的指定字符串,max>0从左向右到max处截止(包括max)替换0到max之间指定的repl为with
StringUtils.replace(null, *, *, *) = null; StringUtils.replace("", *, *, *) = ""; StringUtils.replace("abaa", null, null, 1) = "abaa";
StringUtils.replace("abaa", null, null, 1) = "abaa"; StringUtils.replace("abaa", "a", null, 1) = "abaa" ;
StringUtils.replace("abaa", "a", "", 1) = "abaa"; StringUtils.replace("abaa", "a", "z", 0) = "abaa" ;
StringUtils.replace("abaa", "a", "z", 1) = "zbaa" ;StringUtils.replace("abaa", "a", "z", 2) = "zbza";
StringUtils.replace("abaa", "a", "z", -1) = "zbzz" ;
七,Case conversion(大小写转换)
21,public static String upperCase(String str)
@param str the String to upper case, may be null
@return the upper cased String, null if null String input
Converts a String to upper case as per {@link String#toUpperCase()}. A null input String returns null.
将一个字符串变为大写
StringUtils.upperCase(null) = null; StringUtils.upperCase("") = ""; StringUtils.upperCase("aBc") = "ABC" ;
22,public static String lowerCase(String str)
@param str the String to lower case, may be null
@return the lower cased String, null if null String input
A null input String returns null.
将一个字符串转换为小写
StringUtils.lowerCase(null) = null ;StringUtils.lowerCase("") = "" ;StringUtils.lowerCase("aBc") = "abc" ;
23,public static String capitalize(String str)
将字符串中的首字母大写
StringUtils.capitalize(null) = null; StringUtils.capitalize("") = "" ;
StringUtils.capitalize("cat") = "Cat" ;StringUtils.capitalize("cAt") = "CAt" ;