package com.gkhy.hazmat.common.utils;
|
|
import cn.hutool.core.util.ObjectUtil;
|
import cn.hutool.core.util.StrUtil;
|
import org.springframework.util.AntPathMatcher;
|
|
import java.util.Collection;
|
import java.util.List;
|
import java.util.Map;
|
|
import static org.apache.commons.lang3.StringUtils.startsWithAny;
|
|
public class StringUtils extends StrUtil {
|
|
/**
|
* 是否为http(s)://开头
|
*
|
* @param link 链接
|
* @return 结果
|
*/
|
public static boolean ishttp(String link)
|
{
|
/**
|
* http请求
|
*/
|
String HTTP = "http://";
|
|
/**
|
* https请求
|
*/
|
String HTTPS = "https://";
|
return startsWithAny(link, HTTP,HTTPS);
|
}
|
|
public static String capitalize(final String str){
|
return org.apache.commons.lang3.StringUtils.capitalize(str);
|
}
|
|
public static String replaceEach(final String text, final String[] searchList, final String[] replacementList){
|
return org.apache.commons.lang3.StringUtils.replaceEach(text, searchList, replacementList);
|
}
|
|
|
/**
|
* 查找指定字符串是否匹配指定字符串列表中的任意一个字符串
|
*
|
* @param str 指定字符串
|
* @param strs 需要检查的字符串数组
|
* @return 是否匹配
|
*/
|
public static boolean matches(String str, List<String> strs)
|
{
|
if (isBlank(str) || ObjectUtil.isEmpty(strs))
|
{
|
return false;
|
}
|
for (String pattern : strs)
|
{
|
if (isMatch(pattern, str))
|
{
|
return true;
|
}
|
}
|
return false;
|
}
|
|
/**
|
* 判断url是否与规则配置:
|
* ? 表示单个字符;
|
* * 表示一层路径内的任意字符串,不可跨层级;
|
* ** 表示任意层路径;
|
*
|
* @param pattern 匹配规则
|
* @param url 需要匹配的url
|
* @return
|
*/
|
public static boolean isMatch(String pattern, String url)
|
{
|
AntPathMatcher matcher = new AntPathMatcher();
|
return matcher.match(pattern, url);
|
}
|
|
public static String addZeroForNum(String str, int strLength) {
|
int strLen = str.length();
|
if (strLen < strLength) {
|
while (strLen < strLength) {
|
StringBuffer sb = new StringBuffer();
|
sb.append("0").append(str);// 左补0
|
// sb.append(str).append("0");//右补0
|
str = sb.toString();
|
strLen = str.length();
|
}
|
}
|
|
return str;
|
}
|
|
/**
|
* * 判断一个Collection是否为空, 包含List,Set,Queue
|
*
|
* @param coll 要判断的Collection
|
* @return true:为空 false:非空
|
*/
|
public static boolean isEmpty(Collection<?> coll)
|
{
|
return isNull(coll) || coll.isEmpty();
|
}
|
|
/**
|
* * 判断一个Collection是否非空,包含List,Set,Queue
|
*
|
* @param coll 要判断的Collection
|
* @return true:非空 false:空
|
*/
|
public static boolean isNotEmpty(Collection<?> coll)
|
{
|
return !isEmpty(coll);
|
}
|
|
/**
|
* * 判断一个Map是否为空
|
*
|
* @param map 要判断的Map
|
* @return true:非空 false:空
|
*/
|
public static boolean isNotEmpty(Map<?, ?> map)
|
{
|
return !isEmpty(map);
|
}
|
/**
|
* * 判断一个Map是否为空
|
*
|
* @param map 要判断的Map
|
* @return true:为空 false:非空
|
*/
|
public static boolean isEmpty(Map<?, ?> map)
|
{
|
return isNull(map) || map.isEmpty();
|
}
|
/**
|
* * 判断一个对象是否为空
|
*
|
* @param object Object
|
* @return true:为空 false:非空
|
*/
|
public static boolean isNull(Object object)
|
{
|
return object == null;
|
}
|
|
/**
|
* * 判断一个对象是否非空
|
*
|
* @param object Object
|
* @return true:非空 false:空
|
*/
|
public static boolean isNotNull(Object object)
|
{
|
return !isNull(object);
|
}
|
|
/**
|
* * 判断一个对象数组是否非空
|
*
|
* @param objects 要判断的对象数组
|
* @return true:非空 false:空
|
*/
|
public static boolean isNotEmpty(Object[] objects)
|
{
|
return !isEmpty(objects);
|
}
|
|
/**
|
* * 判断一个对象数组是否为空
|
*
|
* @param objects 要判断的对象数组
|
** @return true:为空 false:非空
|
*/
|
public static boolean isEmpty(Object[] objects)
|
{
|
return isNull(objects) || (objects.length == 0);
|
}
|
|
}
|