com.zusmart.basic.toolkit.StringUtils Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of zusmart-basic Show documentation
Show all versions of zusmart-basic Show documentation
基础模块,提供配置,日志,SPI,图排序,路径匹配,资源扫描,包扫描,常用工具类
package com.zusmart.basic.toolkit;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.StringTokenizer;
/**
* 字符串处理工具类
*
* @author Administrator
*
*/
public final class StringUtils {
/**
* 判断字符串是否为空
*
* @param charSequence
* 字符串
* @return true 为空
*/
public static boolean isBlank(CharSequence charSequence) {
int length;
if (null == charSequence || (length = charSequence.length()) == 0) {
return true;
}
for (int i = 0; i < length; i++) {
if (!Character.isWhitespace(charSequence.charAt(i))) {
return false;
}
}
return true;
}
public static boolean isNotBlank(CharSequence charSequence) {
return !isBlank(charSequence);
}
/**
* 转换为骆驼命名法
*
* @param value
* 字符串
* @return 骆驼命名法处理后的字符串
*/
public static String toCamelName(String value) {
if (isBlank(value)) {
return value;
}
return value.substring(0, 1).toLowerCase() + value.substring(1);
}
/**
* 集合类型转string数组
*
* @param collection
* 集合内容
* @return 字符串数组
*/
public static String[] toArray(Collection collection) {
return collection.toArray(new String[0]);
}
/**
* 字符串根据分隔符转string数组
*
* @param str
* 字符串
* @param delimiters
* 分隔符
* @param trimTokens
* 是否去掉空格
* @param ignoreEmptyTokens
* 是否忽略两个分隔符中间只有空格的数据
* @return 字符串数组
*/
public static String[] toArray(String str, String delimiters, boolean trimTokens, boolean ignoreEmptyTokens) {
if (str == null) {
return new String[0];
}
StringTokenizer st = new StringTokenizer(str, delimiters);
List tokens = new ArrayList();
while (st.hasMoreTokens()) {
String token = st.nextToken();
if (trimTokens) {
token = token.trim();
}
if (!ignoreEmptyTokens || token.length() > 0) {
tokens.add(token);
}
}
return toArray(tokens);
}
}