StringUtils工具类常用方法介绍(持续更新)
StringUtils方法的操作对象是java.lang.String类型的对象,是JDK提供的String类型操作方法的补充,并且是null安全的(即如果输入参数String为null则不会抛出NullPointerException,而是做了相应处理,例如,如果输入为null则返回也是null等,具体可以查看源代码)。
除了构造器,StringUtils中一共有130多个方法,并且都是static的,所以我们可以这样调用StringUtils.xxx()
一.isBlank与isNotBlank
1. public static boolean isBlank(String str)
判断某字符串是否为空或长度为0或由空白符(whitespace)构成
下面是示例:
StringUtils.isBlank(null) = true
StringUtils.isBlank("") = true
StringUtils.isBlank(" ") = true
StringUtils.isBlank(" ") = true
StringUtils.isBlank("/t /n /f /r") = true //对于制表符、换行符、换页符和回车符StringUtils.isBlank()均识为空白符
StringUtils.isBlank("/b") = false //"/b"为单词边界符
StringUtils.isBlank("bob") = false
StringUtils.isBlank(" bob ") = false
2.public static boolean isNotBlank(String str)
判断某字符串是否不为空且长度不为0且不由空白符(whitespace)构成,等于!isBlank(String str)
下面是示例:
StringUtils.isNotBlank(null) = false
StringUtils.isNotBlank("") = false
StringUtils.isNotBlank(" ") = false
StringUtils.isNotBlank(" ") = false
StringUtils.isNotBlank("/t /n /f /r") = false
StringUtils.isNotBlank("/b") = true
StringUtils.isNotBlank("bob") = true
StringUtils.isNotBlank(" bob ") = true
二.equals用法
比较两个字符串是否相等,如果两个均为null,则也认为相等
StringUtils.equals("", ""); //结果是true
StringUtils.equals("str", "str"); //结果是true
StringUtils.equals(null, null); //结果是true
StringUtils.equals(null, ""); //结果是falseStringUtils.equals("",null); //结果是falseStringUtils.equals(null,""); //结果是falseStringUtils.equalsIgnoreCase("ss", "Ss"); //不区分大小写--结果是true
三.isEmpty与isNotEmpty
1. public static boolean isEmpty(String str)
判断某字符串是否为空,为空的标准是str==null或str.length()==0
下面是StringUtils判断是否为空的示例:
StringUtils.isEmpty(null) = true
StringUtils.isEmpty("") = true
StringUtils.isEmpty(" ") = false //注意在StringUtils中空格作非空处理
StringUtils.isEmpty(" ") = false
StringUtils.isEmpty("bob") = false
StringUtils.isEmpty(" bob ") = false
2. public static boolean isNotEmpty(String str)
判断某字符串是否非空,等于!isEmpty(String str)
下面是示例:
StringUtils.isNotEmpty(null) = false
StringUtils.isNotEmpty("") = false
StringUtils.isNotEmpty(" ") = true
StringUtils.isNotEmpty(" ") = true
StringUtils.isNotEmpty("bob") = true
StringUtils.isNotEmpty(" bob ") = true
四.indexOf
1.普通查找字符,如果一参数为null或者""返回-1
public static int indexOf(CharSequence seq,
CharSequence searchSeq)
Finds the first index within a CharSequence, handling null. This method uses String.indexOf(String, int) if possible.
A null CharSequence will return -1.
StringUtils.indexOf(null, *) = -1
StringUtils.indexOf(*, null) = -1
StringUtils.indexOf("", "") = 0
StringUtils.indexOf("", *) = -1 (except when * = "")
StringUtils.indexOf("aabaabaa", "a") = 0
StringUtils.indexOf("aabaabaa", "b") = 2
StringUtils.indexOf("aabaabaa", "ab") = 1
StringUtils.indexOf("aabaabaa", "") = 0
- Parameters:
seq- the CharSequence to check, may be nullsearchSeq- the CharSequence to find, may be null- Returns:
- the first index of the search CharSequence, -1 if no match or
nullstring input - Since:
- 2.0, 3.0 Changed signature from indexOf(String, String) to indexOf(CharSequence, CharSequence)
2.从指定位置(三参数)开始查找
public static int indexOf(CharSequence seq,
CharSequence searchSeq,
int startPos)
Finds the first index within a CharSequence, handling null. This method uses String.indexOf(String, int) if possible.
A null CharSequence will return -1. A negative start position is treated as zero. An empty ("") search CharSequence always matches. A start position greater than the string length only matches an empty search CharSequence.
StringUtils.indexOf(null, *, *) = -1
StringUtils.indexOf(*, null, *) = -1
StringUtils.indexOf("", "", 0) = 0
StringUtils.indexOf("", *, 0) = -1 (except when * = "")
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
- Parameters:
seq- the CharSequence to check, may be nullsearchSeq- the CharSequence to find, may be nullstartPos- the start position, negative treated as zero- Returns:
- the first index of the search CharSequence (always ≥ startPos), -1 if no match or
nullstring input - Since:
- 2.0, 3.0 Changed signature from indexOf(String, String, int) to indexOf(CharSequence, CharSequence, int)
五.indexOfIgnoreCase
1.查找,不区分大小写
public static int indexOfIgnoreCase(CharSequence str,
CharSequence searchStr)
Case in-sensitive find of the first index within a CharSequence.
A null CharSequence will return -1. A negative start position is treated as zero. An empty ("") search CharSequence always matches. A start position greater than the string length only matches an empty search CharSequence.
StringUtils.indexOfIgnoreCase(null, *) = -1
StringUtils.indexOfIgnoreCase(*, null) = -1
StringUtils.indexOfIgnoreCase("", "") = 0
StringUtils.indexOfIgnoreCase("aabaabaa", "a") = 0
StringUtils.indexOfIgnoreCase("aabaabaa", "b") = 2
StringUtils.indexOfIgnoreCase("aabaabaa", "ab") = 1
- Parameters:
str- the CharSequence to check, may be nullsearchStr- the CharSequence to find, may be null- Returns:
- the first index of the search CharSequence, -1 if no match or
nullstring input - Since:
- 2.5, 3.0 Changed signature from indexOfIgnoreCase(String, String) to indexOfIgnoreCase(CharSequence, CharSequence)
2.从指定位置(三参数)开始查找,不区分大小写
public static int indexOfIgnoreCase(CharSequence str,
CharSequence searchStr,
int startPos)
Case in-sensitive find of the first index within a CharSequence from the specified position.
A null CharSequence will return -1. A negative start position is treated as zero. An empty ("") search CharSequence always matches. A start position greater than the string length only matches an empty search CharSequence.
StringUtils.indexOfIgnoreCase(null, *, *) = -1
StringUtils.indexOfIgnoreCase(*, null, *) = -1
StringUtils.indexOfIgnoreCase("", "", 0) = 0
StringUtils.indexOfIgnoreCase("aabaabaa", "A", 0) = 0
StringUtils.indexOfIgnoreCase("aabaabaa", "B", 0) = 2
StringUtils.indexOfIgnoreCase("aabaabaa", "AB", 0) = 1
StringUtils.indexOfIgnoreCase("aabaabaa", "B", 3) = 5
StringUtils.indexOfIgnoreCase("aabaabaa", "B", 9) = -1
StringUtils.indexOfIgnoreCase("aabaabaa", "B", -1) = 2
StringUtils.indexOfIgnoreCase("aabaabaa", "", 2) = 2
StringUtils.indexOfIgnoreCase("abc", "", 9) = -1
- Parameters:
str- the CharSequence to check, may be nullsearchStr- the CharSequence to find, may be nullstartPos- the start position, negative treated as zero- Returns:
- the first index of the search CharSequence (always ≥ startPos), -1 if no match or
nullstring input - Since:
- 2.5, 3.0 Changed signature from indexOfIgnoreCase(String, String, int) to indexOfIgnoreCase(CharSequence, CharSequence, int)
六.split
1.分割,以指定字符分割成数组
public static String[] split(String str,
char separatorChar)
Splits the provided text into an array, separator specified. This is an alternative to using StringTokenizer.
The separator is not included in the returned String array. Adjacent separators are treated as one separator. For more control over the split use the StrTokenizer class.
A null input String returns null.
StringUtils.split(null, *) = null
StringUtils.split("", *) = []
StringUtils.split("a.b.c", '.') = ["a", "b", "c"]
StringUtils.split("a..b.c", '.') = ["a", "b", "c"]
StringUtils.split("a:b:c", '.') = ["a:b:c"]
StringUtils.split("a b c", ' ') = ["a", "b", "c"]
- Parameters:
str- the String to parse, may be nullseparatorChar- the character used as the delimiter- Returns:
- an array of parsed Strings,
nullif null String input - Since:
- 2.0
2.用空格分割成数组,null为null
public static String[] split(String str)
Splits the provided text into an array, using whitespace as the separator. Whitespace is defined by Character.isWhitespace(char).
The separator is not included in the returned String array. Adjacent separators are treated as one separator. For more control over the split use the StrTokenizer class.
A null input String returns null.
StringUtils.split(null) = null
StringUtils.split("") = []
StringUtils.split("abc def") = ["abc", "def"]
StringUtils.split("abc def") = ["abc", "def"]
StringUtils.split(" abc ") = ["abc"]
Parameters:str- the String to parse, may be null Returns:an array of parsed Strings,nullif null String input
3.以指定字符分割成数组,第三个参数表示分隔成数组的长度,如果为0全体分割
public static String[] split(String str,
String separatorChars,
int max)
Splits the provided text into an array with a maximum length, separators specified.
The separator is not included in the returned String array. Adjacent separators are treated as one separator.
A null input String returns null. A null separatorChars splits on whitespace.
If more than max delimited substrings are found, the last returned string includes all characters after the first max - 1 returned strings (including separator characters).
StringUtils.split(null, *, *) = null
StringUtils.split("", *, *) = []
StringUtils.split("ab cd ef", null, 0) = ["ab", "cd", "ef"]
StringUtils.split("ab cd ef", null, 0) = ["ab", "cd", "ef"]
StringUtils.split("ab:cd:ef", ":", 0) = ["ab", "cd", "ef"]
StringUtils.split("ab:cd:ef", ":", 2) = ["ab", "cd:ef"]
Parameters:
str - the String to parse, may be null
separatorChars - the characters used as the delimiters, null splits on whitespace
max - the maximum number of elements to include in the array. A zero or negative value implies no limit
Returns:
an array of parsed Strings, null if null String input
七.substring,字符串截取
1.截取指定位置的字符,null返回null.""返回""
public static String substring(String str,
int start)
Gets a substring from the specified String avoiding exceptions.
A negative start position can be used to start n characters from the end of the String.
A null String will return null. An empty ("") String will return "".
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"
Parameters:
str - the String to get the substring from, may be null
start - the position to start from, negative means count back from the end of the String by this many characters
Returns:
substring from start position, null if null String input
2.截取指定区间的字符
public static String substring(String str,
int start,
int end)
Gets a substring from the specified String avoiding exceptions.
A negative start position can be used to start/end n characters from the end of the String.
The returned substring starts with the character in the start position and ends before the end position. All position counting is zero-based -- i.e., to start at the beginning of the string use start = 0. Negative start and end positions can be used to specify offsets relative to the end of the String.
If start is not strictly to the left of end, "" is returned.
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"
Parameters:
str - the String to get the substring from, may be null
start - the position to start from, negative means count back from the end of the String by this many characters
end - the position to end at (exclusive), negative means count back from the end of the String by this many characters
Returns:
substring from start position to end position, null if null String input
3.从左截取指定长度的字符串
public static String left(String str,
int len)
Gets the leftmost len characters of a String.
If len characters are not available, or the String is null, the String will be returned without an exception. An empty String is returned if len is negative.
StringUtils.left(null, *) = null
StringUtils.left(*, -ve) = ""
StringUtils.left("", *) = ""
StringUtils.left("abc", 0) = ""
StringUtils.left("abc", 2) = "ab"
StringUtils.left("abc", 4) = "abc"
Parameters:
str - the String to get the leftmost characters from, may be null
len - the length of the required String
Returns:
the leftmost characters, null if null String input
4.从右截取指定长度的字符串
public static String right(String str,
int len)
Gets the rightmost len characters of a String.
If len characters are not available, or the String is null, the String will be returned without an an exception. An empty String is returned if len is negative.
StringUtils.right(null, *) = null
StringUtils.right(*, -ve) = ""
StringUtils.right("", *) = ""
StringUtils.right("abc", 0) = ""
StringUtils.right("abc", 2) = "bc"
StringUtils.right("abc", 4) = "abc"
Parameters:
str - the String to get the rightmost characters from, may be null
len - the length of the required String
Returns:
the rightmost characters, null if null String input
5.第几个开始截取,三参数表示截取的长度
public static String mid(String str,
int pos,
int len)
Gets len characters from the middle of a String.
If len characters are not available, the remainder of the String will be returned without an exception. If the String is null, null will be returned. An empty String is returned if len is negative or exceeds the length of str.
StringUtils.mid(null, *, *) = null
StringUtils.mid(*, *, -ve) = ""
StringUtils.mid("", 0, *) = ""
StringUtils.mid("abc", 0, 2) = "ab"
StringUtils.mid("abc", 0, 4) = "abc"
StringUtils.mid("abc", 2, 4) = "c"
StringUtils.mid("abc", 4, 2) = ""
StringUtils.mid("abc", -2, 2) = "ab"
Parameters:
str - the String to get the characters from, may be null
pos - the position to start from, negative treated as zero
len - the length of the required String
Returns:
the middle characters, null if null String input
6.其他截取方法
截取到等于第二个参数的字符串为止
System.out.println(StringUtils.substringBefore("说点什么好呢", "好"));
从左往右查到相等的字符开始,保留后边的,不包含等于的字符。本例:什么好呢
System.out.println(StringUtils.substringAfter("说点什么好呢", "点"));
这个也是截取到相等的字符,但是是从右往左.本例结果:说点什么好
System.out.println(StringUtils.substringBeforeLast("说点什么好点呢", "点"));
这个截取同上是从右往左。但是保留右边的字符
System.out.println(StringUtils.substringAfterLast("说点什么好点呢?", "点"));
截取查找到第一次的位置,和第二次的位置中间的字符。如果没找到第二个返回null。本例结果:2010世界杯在
System.out.println(StringUtils.substringBetween("南非2010世界杯在南非,在南非", "南非"));
返回参数二和参数三中间的字符串,返回数组形式
ArrayToList(StringUtils.substringsBetween("[a][b][c]",
"[", "]"));
八.contains
1.检查是否查到,返回boolean,null返回假
public static boolean contains(CharSequence seq,
CharSequence searchSeq)
Checks if CharSequence contains a search CharSequence, handling null. This method uses String.indexOf(String) if possible.
A null CharSequence will return false.
StringUtils.contains(null, *) = false
StringUtils.contains(*, null) = false
StringUtils.contains("", "") = true
StringUtils.contains("abc", "") = true
StringUtils.contains("abc", "a") = true
StringUtils.contains("abc", "z") = false
Parameters:
seq - the CharSequence to check, may be null
searchSeq - the CharSequence to find, may be null
Returns:
true if the CharSequence contains the search CharSequence, false if not or null string input
Since:
2.0, 3.0 Changed signature from contains(String, String) to contains(CharSequence, CharSequence)
2.containsIgnoreCase
检查是否查到,返回boolean,null返回假,不区分大小写
public static boolean containsIgnoreCase(CharSequence str,
CharSequence searchStr)
Checks if CharSequence contains a search CharSequence irrespective of case, handling null. Case-insensitivity is defined as by String.equalsIgnoreCase(String).
A null CharSequence will return false.
StringUtils.containsIgnoreCase(null, *) = false
StringUtils.containsIgnoreCase(*, null) = false
StringUtils.containsIgnoreCase("", "") = true
StringUtils.containsIgnoreCase("abc", "") = true
StringUtils.containsIgnoreCase("abc", "a") = true
StringUtils.containsIgnoreCase("abc", "z") = false
StringUtils.containsIgnoreCase("abc", "A") = true
StringUtils.containsIgnoreCase("abc", "Z") = false
Parameters:
str - the CharSequence to check, may be null
searchStr - the CharSequence to find, may be null
Returns:
true if the CharSequence contains the search CharSequence irrespective of case or false if not or null string input
Since:
3.0 Changed signature from containsIgnoreCase(String, String) to containsIgnoreCase(CharSequence, CharSequence)
3.containsWhitespace
检查是否有含有空格,返回boolean
public static boolean containsWhitespace(CharSequence seq)
Check whether the given CharSequence contains any whitespace characters.
Parameters:
seq - the CharSequence to check (may be null)
Returns:
true if the CharSequence is not empty and contains at least 1 whitespace character
Since:
3.0
See Also:
StringUtils工具类常用方法介绍(持续更新)的更多相关文章
- StringUtils工具类常用方法汇总2(截取、去除空白、包含、查询索引)
在上一篇中总结了StringUtils工具类在判断字符串为空,大小写转换,移除字符或字符序列,替换,反转,切割合并等方面的方法,这次再汇总一下其它常用的方法. 一.截取 StringUtils ...
- StringUtils工具类常用方法汇总1(判空、转换、移除、替换、反转)
Apache commons lang3包下的StringUtils工具类中封装了一些字符串操作的方法,非常实用,使用起来也非常方便.最近自己也经常在项目中使用到了里面的一些方法,在这里将常用的方 ...
- StringUtils工具类常用方法
前言:工作中看到项目组里的大牛写代码大量的用到了StringUtils工具类来做字符串的操作,便学习整理了一下,方便查阅. isEmpty(String str) 是否为空,空格字符为false is ...
- StringUtils工具类常用方法汇总(判空、转换、移除、替换、反转)
Apache commons lang3包下的StringUtils工具类中封装了一些字符串操作的方法,非常实用,使用起来也非常方便.最近自己也经常在项目中使用到了里面的一些方法,在这里将常用的方法总 ...
- StringUtils工具类常用方法汇总:判空、转换、移除、替换、反转。
Apache commons lang3包下的StringUtils工具类中封装了一些字符串操作的方法,非常实用,使用起来也非常方便.最近自己也经常在项目中使用到了里面的一些方法,在这里将常用的方法总 ...
- StringUtils工具类常用方法详解
StringUtils 常用方法 1.isEmpty(String str) 是否为空,空格字符为false2.isNotEmpty(String str) 是否为非空,空格字符为true3.isBl ...
- StringUtils工具类常用方法汇总(截取、去除空白、包含、查询索引)
一.截取 StringUtils中常用的截取字符串的方法如下: substring(String str,int start) substring(String str,int start, in ...
- 基于StringUtils工具类的常用方法介绍(必看篇)
前言:工作中看到项目组里的大牛写代码大量的用到了StringUtils工具类来做字符串的操作,便学习整理了一下,方便查阅. isEmpty(String str) 是否为空,空格字符为false is ...
- java基础-Math类常用方法介绍
java基础-Math类常用方法介绍 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.Math类概念 Math 类包含用于执行基本数学运算的方法,如初等指数.对数.平方根和三角函 ...
随机推荐
- Sencha Cmd的简介
Sencha Cmd的简介 ~~~~~~~~~~~~~~~~~~~~~~~ Sencha cmd 是一个跨平台的命令行工具,它从你应用程序的新创建到部署入产品中的整个生命周期都提供了许多自动化的执行任 ...
- Asp.net Ajax提供PageMethods调用
页面上的Javascript通过ASP.NET AJAX除了可以调用Web服务类,还可以调用网页中的静态方法. <%@ Page Language="C#" %> &l ...
- MFC中PeekMessage的使用,非阻塞消息循环
在程序设计的时候经常要进行一个数据循环,比如播放音乐需要循环的向缓冲区里面写入数据,在这个时候比较通用的方法是建立一个线程做事情,但是有时候不想创建多线程就可以使用微软提供的PeekMessage方法 ...
- 浅谈SQL Server中的三种物理连接操作(HASH JOIN MERGE JOIN NESTED LOOP)
简介 在SQL Server中,我们所常见的表与表之间的Inner Join,Outer Join都会被执行引擎根据所选的列,数据上是否有索引,所选数据的选择性转化为Loop Join,Merge J ...
- Extjs4中的store
Extjs 4引入新的数据包,其中新增了不少新类并对旧有的类作出了修整.使数据包更强大和更容易使用. 本章我们将学习一下内容: 2.1. 概述新特性 Extjs4的数据包引入了如Mod ...
- LPC1768的SPI通讯
SPI是一种全双工串行接口,可处理多个连接到指定总线上的主机和从机.在数据传输过程中总线上只能有一个主机和一个从机通信.在数据传输中,主机总是会向从机发送一帧8到16个位的数据,而从机也总会向主机发送 ...
- Online Schema Change for MySQL
It is great to be able to build small utilities on top of an excellent RDBMS. Thank you MySQL. This ...
- IOS开发-UI学习-UITextField的各种属性设置
UITextField是IOS中非常常用的一个控件,用来接收用户输入信息,完成应用和用户的交互.它的主要属性设置如下: //初始化textfield并设置位置及大小 UITextField *text ...
- UVa 10911 - Forming Quiz Teams
题目大意:给出2*n个点,将这些点配成n对,使得所有点对中两点的距离之和尽量小. 用一个整数的二进制形式表示一个集合的子集,以每个集合为状态进行状态转移.具体参见<算法竞赛入门经典>9.5 ...
- 4、安卓数据存储——sqlite
朋友圈里的每一个消息体里面的数据,当下拉刷新从服务器下载数据包后,存入sqlite:用户名.图片url.点赞.评论等等.上拉加载的时候,从数据库里取出最近的5条数据加载到朋友圈上. Android通过 ...