六狼论坛

 找回密码
 立即注册

QQ登录

只需一步,快速开始

新浪微博账号登陆

只需一步,快速开始

搜索
查看: 33|回复: 0

StringUtils字符串公共处理类

[复制链接]

升级  0%

12

主题

12

主题

12

主题

秀才

Rank: 2

积分
50
 楼主| 发表于 2013-2-1 09:43:26 | 显示全部楼层 |阅读模式
public final class StringUtils {
/**
* Checks if a string is empty ("") or null.
*
* @param str
*            string to check, may be null
* @return <code>true</code> if the string is <code>null</code> or empty,
*         else <code>false</code>
*/
public static boolean isEmpty(final String str) {
return str == null || str.length() == 0;
}
/**
* Checks if a string is not empty ("") and not null.
*
* @param str
*            string to check, may be null
* @return <code>true</code> if the string is not empty and not
*         <code>null</code>, else <code>false</code>
*/
public static boolean isNotEmpty(final String str) {
return !isEmpty(str);
}
/**
* Checks if a string is whitespace, empty ("") or null. Whitespace is
* checked by {@link Character#isWhitespace(char)}.
*
* @param str
*            string to check, may be null
* @return <code>true</code> if the string is <code>null</code>, empty or
*         whitespace
*/
public static boolean isBlank(final String str) {

if (isEmpty(str))
return true;

for (char c : str.toCharArray()) {
if (!Character.isWhitespace(c))
return false;
}

return true;
}
/**
* Checks if a string is not empty (""), not null and not whitespace.
*
* @param str
*            string to check, may be null
* @return <code>true</code> if the string is not <code>null</code>, not
*         empty and not whitespace.
*/
public static boolean isNotBlank(final String str) {
return !isBlank(str);
}
/**
* Constructs a set of lower-cased strings from a delimiter-separated
* string.
*
* @param stringList
*            strings separated with a delimiter
* @param delimiter
*            separating delimiter
* @return a lower-cased set, empty set if stringList is empty
* @throws IllegalArgumentException
*             if <code>delimiter</code> is empty
*/
public static Set<String> getSet(final String stringList,
final String delimiter) {
if (isEmpty(delimiter))
throw new IllegalArgumentException(
"Argument 'delimiter' shouldn't be empty!");
if (isEmpty(stringList))
return new HashSet<String>();

Set<String> set = new HashSet<String>();
String[] strs = stringList.split(delimiter);
for(int i=0;i<strs.length;i++) {
String tmp = strs[i];
if(isNotEmpty(tmp)) {// simple empty filter
set.add(tmp.toLowerCase());
}
}
return set;
}
}
您需要登录后才可以回帖 登录 | 立即注册 新浪微博账号登陆

本版积分规则

快速回复 返回顶部 返回列表