黑马程序员技术交流社区
标题:
分享:StringUtils 工具类的常用方法(上)
[打印本页]
作者:
Happe_Sun
时间:
2015-6-18 00:23
标题:
分享:StringUtils 工具类的常用方法(上)
StringUtils工具类的使用
一、数组转成字符串:
1、 将数组中的字符转换为一个字符串
将数组中的字符转换为一个字符串
@param strToConv 要转换的字符串 ,默认以逗号分隔
@return 返回一个字符串
String[3] s={"a","b","c"}
StringUtil.convString(s)="a,b,c"
2、 static public String converString(String strToConv)
@param strToConv 要转换的字符串 ,
@param conv 分隔符,默认以逗号分隔
@return 同样返回一个字符串
String[3] s={"a","b","c"}
StringUtil.convString(s,"@")="a@b@c"
static public String converString(String strToConv, String conv)
二、空值检测:
3、
Checks if a String is empty ("") or null.
判断一个字符串是否为空,空格作非空处理。 StringUtils.isEmpty(null) = true StringUtils.isEmpty("") = true StringUtils.isEmpty(" ") = false StringUtils.isEmpty("bob") = false StringUtils.isEmpty(" bob ") = false
NOTE: This method changed in Lang version 2.0.
It no longer trims the String.
That functionality is available in isBlank().
@param str the String to check, may be null
@return true if the String is empty or null
public static boolean isEmpty(String str)
三、非空处理:
4、
Checks if a 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
@param str the String to check, may be null
@return true if the String is not empty and not null
public static boolean isNotEmpty(String str)
5、
Checks if a String is not empty (""), not null and not whitespace only.
判断一个字符串是否非空,空格作空处理. StringUtils.isNotBlank(null) = false StringUtils.isNotBlank("") = false StringUtils.isNotBlank(" ") = false StringUtils.isNotBlank("bob") = true StringUtils.isNotBlank(" bob ") = true
@param str the String to check, may be null
@return true if the String is
not empty and not null and not whitespace
@since 2.0
public static boolean isNotBlank(String str)
四、 空格处理
6、
Removes control characters (char <= 32) from both
ends of this String, handling null by returning
null.
The String is trimmed using {@link String#trim()}.
Trim removes start and end characters <= 32.
To strip whitespace use {@link //strip(String)}.
To trim your choice of characters, use the
{@link //strip(String, String)} methods.
格式化一个字符串中的空格,有非空判断处理; StringUtils.trim(null) = null StringUtils.trim("") = "" StringUtils.trim(" ") = "" StringUtils.trim("abc") = "abc" StringUtils.trim(" abc ") = "abc"
@param str the String to be trimmed, may be null
@return the trimmed string, null if null String input
public static String trim(String str)
7、
Removes control characters (char <= 32) from both
ends of this String returning null if the String is
empty ("") after the trim or if it is null.
The String is trimmed using {@link String#trim()}.
Trim removes start and end characters <= 32.
To strip whitespace use {@link /stripToNull(String)}.
格式化一个字符串中的空格,有非空判断处理,如果为空返回null; StringUtils.trimToNull(null) = null StringUtils.trimToNull("") = null StringUtils.trimToNull(" ") = null StringUtils.trimToNull("abc") = "abc" StringUtils.trimToNull(" abc ") = "abc"
@param str the String to be trimmed, may be null
@return the trimmed String,
null if only chars <= 32, empty or null String input
@since 2.0
public static String trimToNull(String str)
8、
Removes control characters (char <= 32) from both
ends of this String returning an empty String ("") if the String
is empty ("") after the trim or if it is null.
The String is trimmed using {@link String#trim()}.
Trim removes start and end characters <= 32.
To strip whitespace use {@link /stripToEmpty(String)}.
格式化一个字符串中的空格,有非空判断处理,如果为空返回""; StringUtils.trimToEmpty(null) = "" StringUtils.trimToEmpty("") = "" StringUtils.trimToEmpty(" ") = "" StringUtils.trimToEmpty("abc") = "abc" StringUtils.trimToEmpty(" abc ") = "abc"
@param str the String to be trimmed, may be null
@return the trimmed String, or an empty String if null input
@since 2.0
public static String trimToEmpty(String str)
复制代码
作者:
Happe_Sun
时间:
2015-6-18 00:24
五、 字符串比较:
9、
Compares two Strings, returning true if they are equal.
nulls are handled without exceptions. Two null
references are considered to be equal. The comparison is case sensitive.
判断两个字符串是否相等,有非空处理。 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
@param str1 the first String, may be null
@param str2 the second String, may be null
@return true if the Strings are equal, case sensitive, or
both null
@see java.lang.String#equals(Object)
public static boolean equals(String str1, String str2)
10、
Compares two Strings, returning true if they are equal ignoring
the case.
nulls are handled without exceptions. Two null
references are considered equal. Comparison is case insensitive.
判断两个字符串是否相等,有非空处理。忽略大小写 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
@param str1 the first String, may be null
@param str2 the second String, may be null
@return true if the Strings are equal, case insensitive, or
both null
@see java.lang.String#equalsIgnoreCase(String)
public static boolean equalsIgnoreCase(String str1, String str2)
六、 IndexOf 处理
11、
Finds the first index within a String, handling null.
This method uses {@link String#indexOf(String)}.
A null String will return -1.
返回要查找的字符串所在位置,有非空处理 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
@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,
-1 if no match or null string input
@since 2.0
public static int indexOf(String str, String searchStr)
12、
Finds the first index within a String, handling null.
This method uses {@link String#indexOf(String, int)}.
A null String will return -1.
A negative start position is treated as zero.
An empty ("") search String always matches.
A start position greater than the string length only matches
an empty search String.
返回要由指定位置开始查找的字符串所在位置,有非空处理 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
@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
@since 2.0
public static int indexOf(String str, String searchStr, int startPos)
复制代码
欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/)
黑马程序员IT技术论坛 X3.2