123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165 |
- package com.yihu.platform.utils;
- import java.math.BigDecimal;
- import java.util.List;
- import java.util.regex.Matcher;
- import java.util.regex.Pattern;
- import org.apache.commons.lang3.exception.ExceptionUtils;
- /**
- * 字符处理类
- *
- * @author wengsb
- * @company yihu.com 2013-4-7上午09:54:39
- */
- public class StringUtil {
- /**
- * 判断对象是否为空或者空串
- *
- * @param str
- * @return
- */
- public static boolean isEmpty(Object obj) {
- if (obj == null)
- return true;
- if (obj instanceof String) {
- return (obj == null || obj.toString().trim().length() == 0);
- }
- if (obj instanceof Long) {
- return (obj == null || (Long) obj == 0);
- }
- if (obj instanceof Integer) {
- return (obj == null || (Integer) obj == 0);
- }
- if (obj instanceof BigDecimal) {
- return (obj == null || Integer.valueOf(obj.toString())==0);
- }
- if (obj instanceof List) {
- return (obj == null || ((List) obj).size() == 0);
- }
- return obj == null;
- }
-
- /**
- * 判断对象是否为空或者空串
- *
- * @param str
- * @return
- */
- public static boolean isNotEmpty(Object obj) {
- return !isEmpty(obj);
- }
- public static boolean isMobile(String mobiles) {
- Pattern p = Pattern.compile("^((19[0-9])|(16[0-9])|(17[0-9])|(14[0-9])|(13[0-9])|(15[^4,\\D])|(18[0-9]))\\d{8}$");
- Matcher m = p.matcher(mobiles);
- return m.matches();
- }
-
- public static boolean isEmail(String mobiles) {
- Pattern p = Pattern.compile("^([a-zA-Z0-9_\\-\\.]+)@((\\[[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}\\.)|(([a-zA-Z0-9\\-]+\\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\\]?)$");
- Matcher m = p.matcher(mobiles);
- return m.matches();
- }
- /**
- * 如果为null则返回""
- *
- * @param obj
- * @return
- */
- public static String getJSONValue(Object obj) {
- if (obj == null) {
- return "";
- } else {
- return obj.toString();
- }
- }
- /**
- * 如果为null则返回""
- *
- * @param obj
- * @return
- */
- public static Object getNullValue(Object obj) {
- if (obj == null) {
- return "";
- } else {
- return obj;
- }
- }
- /**
- * 过滤含有html、script、style标签的字符串
- *
- * @param inputString
- * 含有html、script、style标签的字符串
- * @return 文本字符串
- */
- public static String Html2Text(String inputString) {
- if(inputString == null || inputString.trim().length() == 0){
- return inputString;
- }
- String htmlStr = inputString; // 含html标签的字符串
- String textStr = "";
- java.util.regex.Pattern p_script;
- java.util.regex.Matcher m_script;
- java.util.regex.Pattern p_style;
- java.util.regex.Matcher m_style;
- java.util.regex.Pattern p_html;
- java.util.regex.Matcher m_html;
- try {
- String regEx_script = "<[\\s]*?script[^>]*?>[\\s\\S]*?<[\\s]*?\\/[\\s]*?script[\\s]*?>"; // 定义script的正则表达式{或<script[^>]*?>[\\s\\S]*?<\\/script>
- String regEx_style = "<[\\s]*?style[^>]*?>[\\s\\S]*?<[\\s]*?\\/[\\s]*?style[\\s]*?>"; // 定义style的正则表达式{或<style[^>]*?>[\\s\\S]*?<\\/style>
- String regEx_html = "<[^>]+>"; // 定义HTML标签的正则表达式
- p_script = Pattern.compile(regEx_script, Pattern.CASE_INSENSITIVE);
- m_script = p_script.matcher(htmlStr);
- htmlStr = m_script.replaceAll(""); // 过滤script标签
- p_style = Pattern.compile(regEx_style, Pattern.CASE_INSENSITIVE);
- m_style = p_style.matcher(htmlStr);
- htmlStr = m_style.replaceAll(""); // 过滤style标签
- p_html = Pattern.compile(regEx_html, Pattern.CASE_INSENSITIVE);
- m_html = p_html.matcher(htmlStr);
- htmlStr = m_html.replaceAll(""); // 过滤html标签
- htmlStr = htmlStr.replaceAll("\\\\n", "");
- htmlStr = htmlStr.replaceAll("\\\\r", "");
- htmlStr = htmlStr.replaceAll(" ", "");
- htmlStr = htmlStr.replaceAll(""", "\"");//引号
- htmlStr = htmlStr.replaceAll("<", "<");
- htmlStr = htmlStr.replaceAll(">", ">");
- textStr = htmlStr;
- } catch (Exception e) {
- System.err.println("Html2Text: " + e.getMessage());
- }
- return textStr;// 返回文本字符串
- }
-
- /**
- * 异常字符串处理方法
- *
- * @param e
- * @return
- */
- public static String getException(Exception e) {
- String exceptionStr = ExceptionUtils.getRootCauseMessage(e);
- if(StringUtil.isEmpty(exceptionStr)){
- exceptionStr = ExceptionUtils.getMessage(e) ;
- }
- return exceptionStr;
- }
-
- }
|