概述
package com.run.zdr.report.utils;
import org.springframework.util.StringUtils;
import java.io.UnsupportedEncodingException;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.concurrent.atomic.AtomicLong;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class StringUtil extends org.apache.commons.lang3.StringUtils {
private static AtomicLong next = new AtomicLong(1);
private static SimpleDateFormat dateformat = new SimpleDateFormat(
"yyyyMMddHHmmss");
private static Pattern pattern = Pattern
.compile("\b((?!\d\d\d)\d+|1\d\d|2[0-4]\d|25[0-5])\.((?!\d\d\d)\d+|1\d\d|2[0-4]\d|25[0-5])\.((?!\d\d\d)\d+|1\d\d|2[0-4]\d|25[0-5])\.((?!\d\d\d)\d+|1\d\d|2[0-4]\d|25[0-5])\b");
private final static char[] CHR = { '0', '1', '2', '3', '4', '5', '6', '7',
'8', '9', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K',
'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X',
'Y', 'Z' };
/**
* 获得0-9的随机数
*
* @param length
* @return String
*/
public static String getRandomNumber(int length) {
Random random = new Random();
StringBuffer buffer = new StringBuffer();
for (int i = 0; i < length; i++) {
buffer.append(random.nextInt(10));
}
return buffer.toString();
}
/**
* 获得0-9的随机数 长度默认为10
*
* @return String
*/
public static String getRandomNumber() {
return getRandomNumber(10);
}
/**
* Replaces all instances of oldString with newString in line.
*
* @param line
* the String to search to perform replacements on
* @param oldString
* the String that should be replaced by newString
* @param newString
* the String that will replace all instances of oldString
* @return a String will all instances of oldString replaced by newString
*/
public static final String replace(String line, String oldString,
String newString) {
int i = 0;
if ((i = line.indexOf(oldString, i)) >= 0) {
char[] line2 = line.toCharArray();
char[] newString2 = newString.toCharArray();
int oLength = oldString.length();
StringBuffer buf = new StringBuffer(line2.length);
buf.append(line2, 0, i).append(newString2);
i += oLength;
int j = i;
while ((i = line.indexOf(oldString, i)) > 0) {
buf.append(line2, j, i - j).append(newString2);
i += oLength;
j = i;
}
buf.append(line2, j, line2.length - j);
return buf.toString();
}
return line;
}
/**
* 获得0-9,a-z,A-Z范围的随机数
*
* @param length
* 随机数长度
* @return String
*/
public static String getRandomChar(int length) {
Random random = new Random();
StringBuffer buffer = new StringBuffer();
for (int i = 0; i < length; i++) {
buffer.append(CHR[random.nextInt(36)]);
}
return buffer.toString();
}
/**
* 获得10位随机字符
*
* @return String
*/
public static String getRandomChar() {
return getRandomChar(10);
}
/**
* 获得主键
*
* @return String
*/
public synchronized static String getPrimaryKey() {
UUID uuid = UUID.randomUUID();
return dateformat.format(new Date())
+ uuid.toString().replaceAll("-", "").substring(0, 16);
}
/**
* 判断字符是否为空
*
* @param input
* 某字符串
* @return 包含则返回true,否则返回false
*/
public static boolean isEmpty(String input) {
return input == null || StringUtils.trimAllWhitespace(input).length() == 0;
}
/**
* 判断对象是否为空
*
* @param obj
* 某字符串
* @return 包含则返回true,否则返回false
*/
public static boolean isEmpty(Object obj) {
return obj == null || obj.toString().length() == 0;
}
/**
* 判断集合是否为空
*
* @param lst
* 某字符串
* @return 包含则返回true,否则返回false
*/
@SuppressWarnings("rawtypes")
public static boolean isEmpty(List lst) {
return lst == null || lst.isEmpty() || lst.size() == 0;
}
public static boolean isLong(String str) {
if(!isEmpty(str)) {
try {
Long.parseLong(str);
return true;
} catch (Exception e) {
return false;
}
}
return false;
}
public static boolean isInteger(String str){
if(!isEmpty(str)){
try{
Integer.parseInt(str);
return true;
}catch (Exception e) {
return false;
}
}
return false;
}
/**
* 将对象转换成字符串输出
*
* @param obj
* 某字对象
* @return
*/
public static String objToStr(Object obj) {
if (isEmpty(obj)) {
return "";
} else {
return obj.toString().trim();
}
}
/**
* 将对象转换成字符串输出(如果为空则用指定值替换)
*
* @param obj
* 某对象
* @param paddingV
* 替换字符串
* @return
*/
public static String objToStr(Object obj, String paddingV) {
if (isEmpty(obj)) {
return paddingV;
} else {
return obj.toString().trim();
}
}
private static final String PATTERN_LINE_START = "^";
private static final String PATTERN_LINE_END = "$";
private static final char[] META_CHARACTERS = { '$', '^', '[', ']', '(',
')', '{', '}', '|', '+', '.', '\' };
/**
* 正则表达式匹配 The function is based on regex.
*
* @param pattern
* @param str
* @return
*/
public static boolean regexMatch(String pattern, String str) {
String result = PATTERN_LINE_START;
char[] chars = pattern.toCharArray();
for (char ch : chars) {
if (Arrays.binarySearch(META_CHARACTERS, ch) >= 0) {
result += "\" + ch;
continue;
}
switch (ch) {
case '*':
result += ".*";
break;
case '?':
result += ".{0,1}";
break;
default:
result += ch;
}
}
result += PATTERN_LINE_END;
return Pattern.matches(result, str);
}
/**
* 字符串首字母大写
*
* @param str
* 字符串
* @return String
*/
public static String upperFirstChar(String str) {
if (str != null && str.length() > 0) {
return str.substring(0, 1).toUpperCase() + str.substring(1);
} else {
return str;
}
}
public static String lowerFirstChar(String str) {
if (str != null && str.length() > 0) {
return str.substring(0, 1).toLowerCase() + str.substring(1);
} else {
return str;
}
}
public static boolean isIP(String ip) {
Matcher matcher = pattern.matcher(ip);
return matcher.matches();
}
public static boolean isIn(String[] strArray, String str) {
if (strArray == null || strArray.length == 0) {
return false;
}
for (String s : strArray) {
if (s.equals(str)) {
return true;
}
}
return false;
}
/**
* 去掉头尾指定字符
*
* @param value
* @param c
* @return
*/
public static String trimChar(String value, char c) {
return StringUtils.trimTrailingCharacter(
StringUtils.trimLeadingCharacter(value, c), c);
}
/**
* 查找字符串个数
*
* @param str
* 字符串
* @param sub
* 子串
* @return int
*/
public static int findCharCount(String str, String sub) {
if (str == null || sub == null || str.length() == 0
|| sub.length() == 0) {
return 0;
}
int count = 0, pos = 0, idx = 0;
while ((idx = str.indexOf(sub, pos)) != -1) {
++count;
pos = idx + sub.length();
}
return count;
}
public static int count(String s) {
try {
byte[] bytes = s.getBytes("Unicode");
// 表示当前的字节数
int n = 0;
// 要截取的字节数,从第3个字节开始
int i = 2;
for (; i < bytes.length; i++) {
// 奇数位置,如3、5、7等,为UCS2编码中两个字节的第二个字节
if (i % 2 == 1) {
if (bytes[i] != 0) {
n++;
}
// 当UCS2编码的第二个字节不等于0时,该UCS2字符为汉字,一个汉字算两个字节
} else {
n++;
}
}
return n;
} catch (UnsupportedEncodingException e) {
return 0;
}
}
public static long getPkNum() {
return next.getAndIncrement() + System.currentTimeMillis();
}
public static boolean isNotBlank(String str) {
if ((str == null) || str.trim().equals("")) {
return false;
} else {
return true;
}
}
/**
* 判断对象不为null,或对象值不为空字符串
*/
public static boolean isNotEmptyObject(Object obj){
if(obj != null && !"".equals(obj)){
return true;
}else{
return false;
}
}
public final static String getUUID() {
UUID uuid = UUID.randomUUID();
return uuid.toString();
}
}
最后
以上就是标致大白为你收集整理的StringUtils工具类——常用判空的全部内容,希望文章能够帮你解决StringUtils工具类——常用判空所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复