2b72c8cb9c94e1b8ad0b9892d3aadea283faf898.svn-base 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. package com.yihu.platform.utils;
  2. import java.math.BigDecimal;
  3. import java.util.List;
  4. import java.util.regex.Matcher;
  5. import java.util.regex.Pattern;
  6. import org.apache.commons.lang3.exception.ExceptionUtils;
  7. /**
  8. * 字符处理类
  9. *
  10. * @author wengsb
  11. * @company yihu.com 2013-4-7上午09:54:39
  12. */
  13. public class StringUtil {
  14. /**
  15. * 判断对象是否为空或者空串
  16. *
  17. * @param str
  18. * @return
  19. */
  20. public static boolean isEmpty(Object obj) {
  21. if (obj == null)
  22. return true;
  23. if (obj instanceof String) {
  24. return (obj == null || obj.toString().trim().length() == 0);
  25. }
  26. if (obj instanceof Long) {
  27. return (obj == null || (Long) obj == 0);
  28. }
  29. if (obj instanceof Integer) {
  30. return (obj == null || (Integer) obj == 0);
  31. }
  32. if (obj instanceof BigDecimal) {
  33. return (obj == null || Integer.valueOf(obj.toString())==0);
  34. }
  35. if (obj instanceof List) {
  36. return (obj == null || ((List) obj).size() == 0);
  37. }
  38. return obj == null;
  39. }
  40. /**
  41. * 判断对象是否为空或者空串
  42. *
  43. * @param str
  44. * @return
  45. */
  46. public static boolean isNotEmpty(Object obj) {
  47. return !isEmpty(obj);
  48. }
  49. public static boolean isMobile(String mobiles) {
  50. 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}$");
  51. Matcher m = p.matcher(mobiles);
  52. return m.matches();
  53. }
  54. public static boolean isEmail(String mobiles) {
  55. 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})(\\]?)$");
  56. Matcher m = p.matcher(mobiles);
  57. return m.matches();
  58. }
  59. /**
  60. * 如果为null则返回""
  61. *
  62. * @param obj
  63. * @return
  64. */
  65. public static String getJSONValue(Object obj) {
  66. if (obj == null) {
  67. return "";
  68. } else {
  69. return obj.toString();
  70. }
  71. }
  72. /**
  73. * 如果为null则返回""
  74. *
  75. * @param obj
  76. * @return
  77. */
  78. public static Object getNullValue(Object obj) {
  79. if (obj == null) {
  80. return "";
  81. } else {
  82. return obj;
  83. }
  84. }
  85. /**
  86. * 过滤含有html、script、style标签的字符串
  87. *
  88. * @param inputString
  89. * 含有html、script、style标签的字符串
  90. * @return 文本字符串
  91. */
  92. public static String Html2Text(String inputString) {
  93. if(inputString == null || inputString.trim().length() == 0){
  94. return inputString;
  95. }
  96. String htmlStr = inputString; // 含html标签的字符串
  97. String textStr = "";
  98. java.util.regex.Pattern p_script;
  99. java.util.regex.Matcher m_script;
  100. java.util.regex.Pattern p_style;
  101. java.util.regex.Matcher m_style;
  102. java.util.regex.Pattern p_html;
  103. java.util.regex.Matcher m_html;
  104. try {
  105. String regEx_script = "<[\\s]*?script[^>]*?>[\\s\\S]*?<[\\s]*?\\/[\\s]*?script[\\s]*?>"; // 定义script的正则表达式{或<script[^>]*?>[\\s\\S]*?<\\/script>
  106. String regEx_style = "<[\\s]*?style[^>]*?>[\\s\\S]*?<[\\s]*?\\/[\\s]*?style[\\s]*?>"; // 定义style的正则表达式{或<style[^>]*?>[\\s\\S]*?<\\/style>
  107. String regEx_html = "<[^>]+>"; // 定义HTML标签的正则表达式
  108. p_script = Pattern.compile(regEx_script, Pattern.CASE_INSENSITIVE);
  109. m_script = p_script.matcher(htmlStr);
  110. htmlStr = m_script.replaceAll(""); // 过滤script标签
  111. p_style = Pattern.compile(regEx_style, Pattern.CASE_INSENSITIVE);
  112. m_style = p_style.matcher(htmlStr);
  113. htmlStr = m_style.replaceAll(""); // 过滤style标签
  114. p_html = Pattern.compile(regEx_html, Pattern.CASE_INSENSITIVE);
  115. m_html = p_html.matcher(htmlStr);
  116. htmlStr = m_html.replaceAll(""); // 过滤html标签
  117. htmlStr = htmlStr.replaceAll("\\\\n", "");
  118. htmlStr = htmlStr.replaceAll("\\\\r", "");
  119. htmlStr = htmlStr.replaceAll("&nbsp;", "");
  120. htmlStr = htmlStr.replaceAll("&quot;", "\"");//引号
  121. htmlStr = htmlStr.replaceAll("&lt;", "<");
  122. htmlStr = htmlStr.replaceAll("&gt;", ">");
  123. textStr = htmlStr;
  124. } catch (Exception e) {
  125. System.err.println("Html2Text: " + e.getMessage());
  126. }
  127. return textStr;// 返回文本字符串
  128. }
  129. /**
  130. * 异常字符串处理方法
  131. *
  132. * @param e
  133. * @return
  134. */
  135. public static String getException(Exception e) {
  136. String exceptionStr = ExceptionUtils.getRootCauseMessage(e);
  137. if(StringUtil.isEmpty(exceptionStr)){
  138. exceptionStr = ExceptionUtils.getMessage(e) ;
  139. }
  140. return exceptionStr;
  141. }
  142. }