StringUtils工具
ppublic class StringUtils {
private StringUtils() {
}
/**
* 文本左边补零
*
* @param maxLength 文本长度
* @param str
* @return
*/
public static String leftCoverFormat(int maxLength, Integer str) {
String value = "";
try {
// 得到一个NumberFormat的实例
NumberFormat nf = NumberFormat.getInstance();
// 设置是否使用分组
nf.setGroupingUsed(false);
// 设置最大整数位数
nf.setMaximumIntegerDigits(maxLength);
// 设置最小整数位数
nf.setMinimumIntegerDigits(maxLength);
value = nf.format(str);
} catch (Exception ex) {
log.error("fl.ec.product.common.utils.leftCoverFormat错误", ex);
}
return value;
}
/**
* 判断对象是否为空,如果为空返回true,否则返回false
*
* @param object
* @return
*/
public static final boolean isEmpty(final Object object) {
if (object == null) {
return true;
}
if (object instanceof List && ((List) object).size() <=0){
return true;
}
return false;
}
/**
* 判断字符串是否为空,如果为空返回true,否则返回false
*
* @param str
* @return
*/
public static final boolean isEmpty(final String str) {
if (str == null || str.trim().length() < 1) {
return true;
}
return false;
}
/**
* 判断字符串是否为空,如果不为空返回true,否则返回false
*
* @param object
* @return
*/
public static final boolean isNotEmpty(final Object object) {
if (object == null) {
return false;
}
if (object instanceof List && ((List) object).size() <=0){
return false;
}
return true;
}
/**
* 判断字符串是否为空,如果不为空返回true,否则返回false
*
* @param str
* @return
*/
public static final boolean isNotEmpty(final String str) {
if (str == null || str.trim().length() < 1) {
return false;
}
return true;
}
/**
* 把null转换成"",如果不为null则转型
*
* @param o
* @return
*/
public static String nullToString(Object o) {
String s = "";
return o != null ? String.valueOf(o) : s;
}
/**
* 功能说明:去掉字符串2端空格或空白。如果参数字符串为null,那么返回结果为空白字符串,即"";
*
* @param s 需要过滤的字符串
* @return
*/
public static String trim(String s) {
return s == null ? "" : s.trim();
}
/**
* Function: splitString<BR>
* Description: 此方法用于拼接sql in <BR>
*
* @param str 需要传入的字符串 id,id,id, 或 id,id
* @param type 需要截取的符号 比如:","
* @return 'id','id' 如果没截取出来就会返回:''
*/
public static String splitString(String str, String type) {
String sql = " ";
StringBuffer sbf = new StringBuffer();
if (!StringUtils.isEmpty(str) && !StringUtils.isEmpty(type)) {
String[] ids = str.split(type);
for (int i = 0; i < ids.length; i++) {
if (ids.length == (i + 1)) {
sbf.append("'").append(ids[i]).append("'");
} else {
sbf.append("'").append(ids[i]).append("'").append(",");
}
}
sbf.append(" ");
} else {
sbf.append("''");
}
return sbf.toString();
}
/**
* 字母变小写
*/
public static String firstCharToLowerCase(String str) {
Character firstChar = str.charAt(0);
String tail = str.substring(1);
str = Character.toLowerCase(firstChar) + tail;
return str;
}
/**
* 首字母变大写
*/
public static String firstCharToUpperCase(String str) {
Character firstChar = str.charAt(0);
String tail = str.substring(1);
str = Character.toUpperCase(firstChar) + tail;
return str;
}
/**
* 替换身份证号
*
* @param idCardNo
* @return
* @author
*/
public static String replaceIdCardNo(String idCardNo) {
// 校验原字符串与替换字符串是否为空,其一为空则返回原字符串
if (!isNotEmpty(idCardNo)) {
return idCardNo;
}
String tmp = idCardNo.substring(6, idCardNo.length() - 4);
String rep = "";
for (int i = 0; i < tmp.length(); i++) {
rep += "*";
}
return idCardNo.replace(tmp, rep);
}
/**
* 替换字符中的表情符号
*
* @param source
* @param target
* @return
*/
public static String filterEmoji(String source, String target) {
if (StringUtils.isEmpty(source)) {
return source;
}
Pattern emoji = Pattern.compile("[\ud83c\udc00-\ud83c\udfff]|[\ud83d\udc00-\ud83d\udfff]|[\u2600-\u27ff]", Pattern.UNICODE_CASE | Pattern.CASE_INSENSITIVE);
Matcher emojiMatcher = emoji.matcher(source);
if (emojiMatcher.find()) {
source = emojiMatcher.replaceAll(target);
return source;
}
return source;
}
/**
* 判断字符是否有表情符号
*
* @param source
* @return
*/
public static boolean isContainEmoji(String source) {
if (StringUtils.isEmpty(source)) {
return false;
}
Pattern emoji = Pattern.compile("[\ud83c\udc00-\ud83c\udfff]|[\ud83d\udc00-\ud83d\udfff]|[\u2600-\u27ff]", Pattern.UNICODE_CASE | Pattern.CASE_INSENSITIVE);
Matcher emojiMatcher = emoji.matcher(source);
if (emojiMatcher.find()) {
return true;
}
return false;
}
/**
* 手机号加密显示
*
* @param mobile
* @param replaceChar
* @return
*/
public static String setMobile(String mobile, String replaceChar) {
if (mobile.length() < 11) {
return mobile;
}
return mobile.substring(0, 3).concat(replaceChar).concat(replaceChar).concat(replaceChar).concat(replaceChar).concat(mobile.substring(7));
}
/**
* 把数组转换成set
*
* @param array
* @return
*/
public static Set<?> array2Set(Object[] array) {
Set<Object> set = new TreeSet<Object>();
for (Object id : array) {
if (null != id) {
set.add(id);
}
}
return set;
}
}
StringUtils工具的更多相关文章
- Spring的StringUtils工具类
本文是转载文章,感觉比较好,如有侵权,请联系本人,我将及时删除. 原文网址:<Spring的StringUtils工具类> org.springframework.util.StringU ...
- StringUtils工具类常用方法汇总2(截取、去除空白、包含、查询索引)
在上一篇中总结了StringUtils工具类在判断字符串为空,大小写转换,移除字符或字符序列,替换,反转,切割合并等方面的方法,这次再汇总一下其它常用的方法. 一.截取 StringUtils ...
- StringUtils工具类常用方法汇总1(判空、转换、移除、替换、反转)
Apache commons lang3包下的StringUtils工具类中封装了一些字符串操作的方法,非常实用,使用起来也非常方便.最近自己也经常在项目中使用到了里面的一些方法,在这里将常用的方 ...
- StringUtils工具类常用方法
前言:工作中看到项目组里的大牛写代码大量的用到了StringUtils工具类来做字符串的操作,便学习整理了一下,方便查阅. isEmpty(String str) 是否为空,空格字符为false is ...
- StringUtils工具类常用方法汇总(判空、转换、移除、替换、反转)
Apache commons lang3包下的StringUtils工具类中封装了一些字符串操作的方法,非常实用,使用起来也非常方便.最近自己也经常在项目中使用到了里面的一些方法,在这里将常用的方法总 ...
- 基于StringUtils工具类的常用方法介绍(必看篇)
前言:工作中看到项目组里的大牛写代码大量的用到了StringUtils工具类来做字符串的操作,便学习整理了一下,方便查阅. isEmpty(String str) 是否为空,空格字符为false is ...
- spring util包 StringUtils工具类中的isEmpty() 方法解析
今天在公司看到同事写的代码,无意发现在判断字符串类型时,使用的是StringUtils工具类中的isEmpty()去判断如下所示 @RequestMapping(value = "/pub/ ...
- StringUtils工具类常用方法汇总:判空、转换、移除、替换、反转。
Apache commons lang3包下的StringUtils工具类中封装了一些字符串操作的方法,非常实用,使用起来也非常方便.最近自己也经常在项目中使用到了里面的一些方法,在这里将常用的方法总 ...
- 通过CollectionUtils工具类判断集合是否为空,通过StringUtils工具类判断字符串是否为空
通过CollectionUtils工具类判断集合是否为空 先引入CollectionUtils工具类: import org.apache.commons.collections4.Collectio ...
- 利用StringUtils工具类进行String为空的判断
利用工具类进行String类型数据的非空判断,让自己的项目代码变得更加的简洁明了. 判断某字符串是否为空,为空的标准是 str==null 或 str.length()==0 下面是 St ...
随机推荐
- usb基础知识以及枚举过程介绍
一个USB设备有一个设备描述符,设备描述符里面决定了该设备有多少种配置,每种配置描述符对应着配置描述符:而在配置描述符中又定义了该配置里面有多少个接口,每个接口有对应的接口描述符:在接口描述符里面又定 ...
- bzoj1294题解
[题意分析] 给定一张网格图,每个网格可能是普通点.特殊点或障碍点,每个特殊点有一个分值.要求选定一条只经过普通点的可重复回路,使回路内部的特殊点分值和最大. [算法分析] 引理:射线法 对于平面内任 ...
- python输入输出(二)
输出 >>> print(5) 5 >>> print(5*6) 30 >>> s1 = "hello" >>&g ...
- palindrome 回文 /// Manacher算法
判断最长不连续回文 #include <bits/stdc++.h> using namespace std; int main() { ]; while(gets(ch)) { ],an ...
- vue created与activated的区别
搬运自:https://www.cnblogs.com/goloving/p/9256212.html 使用<keep-alive>会将数据保留在内存中,如果要在每次进入页面的时候获取最新 ...
- 使用mapreduce对日志进行清洗
网站日志分析项目案例(一)项目介绍:http://www.cnblogs.com/edisonchou/p/4449082.html 网站日志分析项目案例(二)数据清洗:当前页面 网站日志分析项目案例 ...
- mac os Catalina beta andriod studio crash
点击菜单 Help - Edit Custom VM Options 添加下面一行-Dsun.font.layoutengine=icu 然后我自己重启了好几下,然后 在重新创建一个新的工程,就好了 ...
- ES5-call,apply,bind的用法
区别bind()与call()和apply()? 1. Function.prototype.bind(obj) : * 作用: 将函数内的this绑定为obj, 并将函数返回2. 面试题: 区别bi ...
- JavaScript - 常用对象相关
1. String对象 length : 字符串的长度 charAt(index) : 返回指定位置的字符串, 下标从0开始 indexOf(str) : 返回指定的字符串在当前字符串中首次出现的位置 ...
- scala中Array简单实用
/** * 在scala中数组的使用 * 和java很类似,初始化后,长度就固定了,而且元素全部根据其类型初始化 * */ object arrayUse { def main(args: Array ...