字符串操作
package com.zxwa.ntmss.Excel; import org.apache.commons.lang3.ObjectUtils; import org.apache.poi.ss.formula.functions.T; import org.junit.Test; import java.math.BigDecimal; import java.text.DecimalFormat; import java.util.*; import java.util.regex.Matcher; import java.util.regex.Pattern; import java.util.regex.PatternSyntaxException; /** * 字符串判断 * * @author * */ public class StringUtils { public static final String EMPTY = ""; public static String formatNum(Double value) { BigDecimal b = new BigDecimal(value); String f1 = b.setScale(2, BigDecimal.ROUND_HALF_UP).toPlainString(); return f1; } /** * 判断字符串是否为空 * * @param str * @return */ public static boolean isEmpty(String str) { if (null == str || "".equals(parseNull(str.trim())) || str.equals("undefined") || str.equals("null")) { return true; } return false; } /** * 判断字符串是否为空 * * @return */ public static boolean isEquals(String str1,String str2) { if(isEmpty(str1)){ if(isEmpty(str2)){ return true; }else { return false; } }else { if(isEmpty(str2)){ return false; }else if(str1.equals(str2)){ return true; }else { return false; } } } /** * 判断字符串是否为空 * * @param str * @return */ public static boolean isEmptyObject(Object str) { if (null == str) { return true; } return false; } /** * 判断字符串是否为空 * * @param str * @return */ public static boolean isNotEmpty(String str) { if (null == str || "".equals(str.trim()) || "null".equals(str.trim())) { return false; } return true; } /**
* 方法名:macParse<br>
* 描述:mac地址格式化. <br>
*
* @param oldMac
* @return<br>
* 作者:zhengchuan<br>
* 日期:2015-6-29<br>
* History:<br>
* Administrator于2015-6-29 上午9:56:51创建<br>
* Administrator于2015-6-29 上午9:56:51修改,原因描述...<br>
*/
public static String parseMac(String oldMac) {
String newMac = "";
if (!StringUtils.isEmpty(oldMac)) {
String macTemp = oldMac.toUpperCase();
newMac = macTemp.substring(0, 2) + ":" + macTemp.substring(2, 4) + ":" + macTemp.substring(4, 6) + ":"
+ macTemp.substring(6, 8) + ":" + macTemp.substring(8, 10) + ":" + macTemp.substring(10, 12);
}
return newMac;
}
/**
*
* @Title: parseNull @Description: 爬虫的数据错误 当某个数据样式为 " "
* ,但又不是空时、调用该方法处理 @param: @param str @param: @return @return:
* String @throws
*/
public static String parseNull(String str) {
if (str == null || str.equals(" ") || str.equals("")) {
str = "";
}
return str;
}
public static String null2Blank(Object obj) {
if (obj == null || obj.toString().equals(" ") || obj.toString().equals("")) {
return "";
}
return obj.toString();
}
public static String null2Zero(Object obj) {
if (obj == null || obj.toString().equals(" ") || obj.toString().equals("")) {
return "0";
}
return obj.toString();
}
/**
*
* @Title: parseNull @Description: 对url后面的参数进行截取的 @param: @param
* str @param: @return @return: String @throws
*/
public static String cleanUrl(String url) {
if (StringUtils.isEmpty(url)) {
return "";
}
if (url.contains("?")) {
url = url.substring(0, url.indexOf("?"));
}
if (url.endsWith("/")) {
url = url.substring(0, url.length() - 1);
}
return url;
}
/**
* 获取32位ID
*
* @return
*/
public static String get32Uuid() {
return UUID.randomUUID().toString().replaceAll("-", "");
}
/**
* 方法名:replaceBlank<br>
* 描述:去除空白字符<br>
* 参数:
*
*/
public static String replaceBlank(String str) {
String dest = "";
if (str != null) {
Pattern p = Pattern.compile("\\s*|\t|\r|\n");
Matcher m = p.matcher(str);
dest = m.replaceAll("");
}
return dest;
}
/**
* 方法名:stringFilter 描述: 过滤特殊字符 参数:<br>
*
*/
public static String stringFilter(String str) throws PatternSyntaxException {
String regEx = "[`~!$%^&*()+=|{}':;',\\[\\]<>/?~!##¥%……&*()——+|{}【】‘;:”“’。,、?\\\\]";
Pattern p = Pattern.compile(regEx);
Matcher m = p.matcher(str);
return m.replaceAll("").trim();
}
public static String toUpperCaseFirstOne(String s) {
if (isEmpty(s)) {
return s;
}
if (Character.isUpperCase(s.charAt(0))) {
return s;
} else {
return (new StringBuilder()).append(Character.toUpperCase(s.charAt(0))).append(s.subSequence(1, s.length()))
.toString();
}
}