StringUtils工具类

1、 public static boolean isEmpty(String str)

判断某字符串是否为空

3、public static boolean isBlank(String str)
判断某字符串是否为空或长度为0或由空白符(whitespace)构成

4、 public static boolean isNotBlank(String str)
判断某字符串是否不为空且长度不为0且不由空白符(whitespace)构成,
等于!isBlank(String str)

5、 public static String trim(String str)
去掉字符串两端的控制符
如果输入为null则返回null

6、public static String trimToNull(String str)
去掉字符串两端的控制符
如果变为null或””,则返回null

7、public static String trimToEmpty(String str)
去掉字符串两端的控制符
如果变为null或””,则返回””

8、public static String strip(String str)
去掉字符串两端的空白符(whitespace),
如果输入为null则返回null
(注意和trim()的区别):

9、public static String stripToNull(String str)
去掉字符串两端的空白符(whitespace),
如果变为null或””,则返回null
(注意和trimToNull()的区别):

10、public static String stripToEmpty(String str)
去掉字符串两端的空白符(whitespace),
如果变为null或””,则返回””
(注意和trimToEmpty()的区别):

11、public static String strip(String str, String stripChars)
去掉str两端的在stripChars中的字符。
如果str为null或等于””,则返回它本身;
如果stripChars为null或””,则返回strip(String str)

StringUtils.strip(“000000134_76539000″,”0″)=”134_76539”

12、 stripStart(String str,String stripChars) 去除str 前端在stripChars中的字符

13、 stripEnd(String str,String stripChars) 去除str 后端在stripChars中的字符

14、public static String[] stripAll(String[] strs)
对字符串数组中的每个字符串进行strip(String str),然后返回。
如果strs为null或strs长度为0,则返回strs本身
15、public static String[] stripAll(String[] strs, String stripChars)
对字符串数组中的每个字符串进行strip(String str, String stripChars),然后返回。
如果strs为null或strs长度为0,则返回strs本身
16、public static boolean equals(String str1, String str2)
比较两个字符串是否相等,如果两个均为空则也认为相等。
17、public static boolean equalsIgnoreCase(String str1, String str2)
比较两个字符串是否相等,不区分大小写,如果两个均为空则也认为相等。
18、public static int indexOf(String str, char searchChar)
返回字符searchChar在字符串str中第一次出现的位置。
如果searchChar没有在str中出现则返回-1,
如果str为null或””,则也返回-1

19、public static int indexOf(String str, char searchChar, int startPos)
返回字符searchChar从startPos开始在字符串str中第一次出现的位置。
如果从startPos开始searchChar没有在str中出现则返回-1,
如果str为null或””,则也返回-1
20、public static int indexOf(String str, String searchStr)
返回字符串searchStr在字符串str中第一次出现的位置。
如果str为null或searchStr为null则返回-1,
如果searchStr为””,且str为不为null,则返回0,
如果searchStr不在str中,则返回-1

21、public static int ordinalIndexOf(String str, String searchStr, int ordinal)
返回字符串searchStr在字符串str中第ordinal次出现的位置。
如果str=null或searchStr=null或ordinal

22、public static int indexOf(String str, String searchStr, int startPos)
返回字符串searchStr从startPos开始在字符串str中第一次出现的位置。
举例(*代表任意字符串):

23、public static int lastIndexOf(String str, char searchChar)
基本原理同18。
24、public static int lastIndexOf(String str, char searchChar, int startPos)
基本原理同19。
25、public static int lastIndexOf(String str, String searchStr)
基本原理同20。
26、public static int lastIndexOf(String str, String searchStr, int startPos)
基本原理同22。

27、public static boolean contains(String str, char searchChar)
判断字符串str中是否包含字符searchChar。
如果str为null或””,返回false;
如果searchChar不在str中,返回false。

28、public static boolean contains(String str, String searchStr)
判断字符串str是否包含字符串searchStr。
如果str为null或searchStr为null,返回false;
如果str为””,并且searchStr为””,返回true

29、public static boolean containsIgnoreCase(String str, String searchStr)
判断字符串str是否包含字符串searchStr,不区分大小写。
和28类似。

30、public static int indexOfAny(String str, char[] searchChars)
找出字符数组searchChars中的字符第一次出现在字符串str中的位置。
如果字符数组中的字符都不在字符串中,则返回-1
如果字符串为null或””,则返回-1
举例(*表示任意):

31、public static int indexOfAny(String str, String searchChars)
找出字符串searchChars中的字符第一次出现在字符串str中的位置。
如果字符串searchChars中的字符都不在字符串str中,则返回-1
如果searchChars或str为null或为””,则返回-1
举例(*表示任意):

32、public static boolean containsOnly(String str, char[] valid)
判断是否字符串str仅包含字符数组valid中的字符,即字符串中的字符是否都在字符数组中。
如果str为null,则返回false;如果str为””,则返回true
举例(*表示任意):

33、public static boolean containsOnly(String str, String validChars)
判断是否字符串str仅包含字符串validChars中的字符,
即字符串str中的字符是否都在字符串validChars中。
和32类似,举例(*表示任意):

34、public static String substring(String str, int start)
得到字符串str的子串。
如果start小于0,位置是从后往前数的第|start|个
如果str为null或””,则返回它本身
举例(*表示任意):

35、public static String substring(String str, int start, int end)
得到字符串str的子串。
如果start小于0,位置是从后往前数的第|start|个,
如果end小于0,位置是从后往前数的第|end|个,
如果str为null或””,则返回它本身
举例(*表示任意):

36、public static String left(String str, int len)
得到字符串str从左边数len长度的子串。
如果str为null或为””,则返回它本身
如果len小于0,则返回””
举例(*表示任意):

37、public static String right(String str, int len)
得到字符串str从右边数len长度的子串。
如果str为null或为””,则返回它本身
如果len小于0,则返回””
举例(*表示任意):

38、public static String mid(String str, int pos, int len)
得到字符串str从pos开始len长度的子串。
如果str为null或为””,则返回它本身
如果len小于0或pos大于srt的长度,则返回””
如果pos小于0,则pos设为0
举例(*表示任意):

39、public static String substringBefore(String str, String separator)
得到字符串str的在字符串separator出现前的字串,且separator不包括在内。
如果str为null或为””,则返回它本身
如果separator为null,则返回str本身
举例(*表示任意):

40、public static String substringAfter(String str, String separator)
得到字符串str的在字符串separator出现后的字串,且separator不包括在内。
如果str为null或为””,则返回它本身
如果separator为null,则返回””
举例(*表示任意):

41、public static String substringBeforeLast(String str, String separator)
和45类似,得到字符串str的在字符串separator最后一次出现前的字串。
这里不再举例。
42、public static String substringAfterLast(String str, String separator)
和46类似,得到字符串str的在字符串separator最后一次出现后的字串。
这里不再举例。
43、public static String substringBetween(String str, String tag)
得到str中的在两个字符串tag中间的字符串,即str中的tag所夹的串。
如果str为null或tag为null,返回null
举例(*表示任意):

44、public static String substringBetween(String str, String open, String close)
得到str中的在两个字符串open和close中间的字符串,即open和close所夹的串。
如果str为null或open为null或close为null,返回null
举例(*表示任意):

45、public static String[] substringsBetween(String str, String open, String close)
得到str中的在两个字符串open和close中间的字符串,即open和close所夹的串,
把所有符合的结果放在数组中返回。
和44类似,但是返回了所有的结果(44只返回了第一个匹配的结果)。
这里不再举例。
46、public static String[] split(String str)
把字符串拆分成一个字符串数组,用空白符(whitespace)作为分隔符。
如果字符串为null,返回null
如果字符串为””,返回空数组{}
举例(*表示任意):

47、public static String[] split(String str, char separatorChar)
把字符串拆分成一个字符串数组,用指定的字符separatorChar作为分隔符。
如果字符串为null,返回null
如果字符串为””,返回空数组{}
举例(*表示任意):

48、public static String[] split(String str, String separatorChars)
把字符串拆分成一个字符串数组,用指定的字符串separatorChars作为分隔符。
如果字符串str为null,返回null
如果字符串str为””,返回空数组{}
如果separatorChars为null,则默认为空白符
和47类似。
举例(*表示任意):

49、public static String[] split(String str, String separatorChars, int max)
把字符串拆分成一个字符串数组,用指定的字符串separatorChars作为分隔符,
数组的最大长度为max。
如果字符串str为null,返回null
如果字符串str为””,返回空数组{}
如果separatorChars为null,则默认为空白符
如果max小于等于0,认为是没有限制
举例(*表示任意):

50、public static String join(Object[] array)
把数组中的元素连接成一个字符串返回。
举例(*表示任意):

51、public static String join(Object[] array, char separator)
把数组中的元素连接成一个字符串返回,把分隔符separator也加上。
举例(*表示任意):

52、public static String join(Object[] array, char separator, int startIndex, int endIndex)
把数组中的元素连接成一个字符串返回,把分隔符separator也加上。
连接的开始位置为startIndex,结束位置为endIndex。
这里不再举例。

53、public static String join(Object[] array, String separator)
与51类似,这里不再举例。
54、public static String join(Object[] array, String separator, int startIndex, int endIndex)
与52类似,这里不再举例。

55、public static String deleteWhitespace(String str)
删除字符串中的所有空白符(whitespace),空白符是这样定义的{@link Character#isWhitespace(char)}。
举例(*表示任意):

56、public static String replaceOnce(String text, String repl, String with)
在字符串text中用with代替repl,仅一次。
这里不再举例。
57、public static String replace(String text, String repl, String with)
在字符串text中用with代替repl,替换所有。
这里不再举例。
58、public static String replace(String text, String repl, String with, int max)
在字符串text中用with代替repl,max为最大替换次数。
如果max小于0,则替换所有。
这里不再举例。
59、public static String replaceChars(String str, char searchChar, char replaceChar)
在字符串str中用字符replaceChar代替所有字符searchChar,
如果字符串为null或””,则返回它本身。
这里不再举例。
60、public static String replaceChars(String str, String searchChars, String replaceChars)
用replaceChars代替str中的searchChars。
replaceChars的长度应该和searchChars的长度相等,
如果replaceChars的长度大于searchChars的长度,超过长度的字符将被忽略,
如果replaceChars的长度小于searchChars的长度,超过长度的字符将被删除。
举例(*表示任意):

61、public static String overlay(String str, String overlay, int start, int end)
用字符串overlay覆盖字符串str从start到end之间的串。
如果str为null,则返回null
如果start或end小于0,则设为0
如果start大于end,则两者交换
如果start或end大于str的长度,则认为等于str的长度
举例(*表示任意):

62、public static String repeat(String str, int repeat)
重复字符串repeat次,组合成一个新串返回。
如果字符串str为null或””,则返回它本身
如果repeat小于0,则返回””
举例(*表示任意):

63、public static int countMatches(String str, String sub)
计算字符串sub在字符串str中出现的次数。
如果str为null或””,则返回0
举例(*表示任意):

参考文章:

Original: https://www.cnblogs.com/better-farther-world2099/p/13646052.html
Author: 奕锋博客
Title: StringUtils工具类

原创文章受到原创版权保护。转载请注明出处:https://www.johngo689.com/685020/

转载文章受原作者版权保护。转载请注明原作者出处!

(0)

大家都在看

亲爱的 Coder【最近整理,可免费获取】👉 最新必读书单  | 👏 面试题下载  | 🌎 免费的AI知识星球