当前位置:   article > 正文

Character的常用工具类_character空指针

character空指针
  1. package com.cmcc.flow.common.util;
  2. import java.io.UnsupportedEncodingException;
  3. import java.util.Map;
  4. import java.util.regex.Matcher;
  5. import java.util.regex.Pattern;
  6. import javax.servlet.http.HttpServletRequest;
  7. public class CharacterUtil {
  8. /**
  9. * 手机电话号码正则表达式
  10. */
  11. private final static String MOBILE_PHONE_NUM_REGEXP="^1[3-8][0-9]{9}$";
  12. private final static String EMAIL_ADDR_REGEXP="^\\w+((-\\w+)|(\\.\\w+))*@[A-Za-z0-9]+((\\.|-)[A-Za-z0-9]+)*\\.[A-Za-z0-9]+$" ;
  13. public static String stringTranscoding(String str){
  14. if(str!=null && str.trim().length()>0 && str.trim()!=""){ //TODO:chendw 重复判断
  15. try {
  16. return new String(str.toString().getBytes("iso-8859-1"),"UTF-8");
  17. } catch (UnsupportedEncodingException e) {
  18. // TODO Auto-generated catch block
  19. // e.printStackTrace();
  20. return null;
  21. }
  22. }
  23. return str;
  24. }
  25. /**
  26. * @throws UnsupportedEncodingException
  27. *
  28. * @Title: getStringFromRequest
  29. * @Description: 得到请求参数
  30. * @param @param request
  31. * @param @param parameterName
  32. * @param @return
  33. * @return String
  34. * @throws
  35. */
  36. public static String getStringFromRequest(HttpServletRequest request,
  37. String parameterName){
  38. try {
  39. request.setCharacterEncoding("utf-8");
  40. } catch (UnsupportedEncodingException e) {
  41. // TODO Auto-generated catch block
  42. e.printStackTrace();
  43. }
  44. if(request==null || parameterName==null)
  45. return null;
  46. String szName = request.getParameter(parameterName);
  47. // if(szName!=null &&
  48. // request.getMethod().equalsIgnoreCase("get")){
  49. // try {
  50. String szRequestCharset = request.getCharacterEncoding();
  51. // szName = new String(szName.getBytes("ISO-8859-1"), "UTF-8");
  52. // } catch (UnsupportedEncodingException e) {
  53. // // TODO Auto-generated catch block
  54. // //e.printStackTrace();
  55. // return null;
  56. // }
  57. // }
  58. szName = trimStringSafe(szName);
  59. return szName;
  60. }
  61. /**
  62. *
  63. * @Title: getStringFromRequest
  64. * @Description: 从页面请求获取相应的参数,如果参数为空或者NULL,统一返回空字符
  65. * @param @param request
  66. * @param @param parameterName
  67. * @param @return
  68. * @return String
  69. * @throws
  70. *//*
  71. public static String getStringFromRequest(HttpServletRequest request,
  72. String parameterName)
  73. {
  74. try {
  75. return getStringFromRequest(request, parameterName, null);
  76. } catch (Exception e) {
  77. // TODO: handle exception
  78. e.printStackTrace();
  79. return "";
  80. }
  81. }*/
  82. public static String trimStringSafe(String oriString)
  83. {
  84. if(oriString!=null)
  85. return oriString.trim();
  86. return null;
  87. }
  88. public static String addQueryString(Map<String, String> queryParams, String oriString)
  89. {
  90. if(oriString==null || oriString.equals("")){
  91. return "";
  92. }
  93. oriString+="?";
  94. for(String szKey : queryParams.keySet()){
  95. String szTmpString = szKey + "=" + queryParams.get(szKey);
  96. oriString+=szTmpString;
  97. oriString+="&";
  98. }
  99. int nLength = oriString.length();
  100. return oriString.substring(0, nLength-1);
  101. }
  102. /**
  103. *
  104. * @Title: isNullOrEmpty
  105. * @Description: 判断字符串是否为空值或空指针,如果是,抛出异常,并设置相应的异常信息
  106. * @param @param str
  107. * @param @param errorMsg
  108. * @param @throws Exception
  109. * @return void
  110. * @throws
  111. */
  112. public static void isNullOrEmpty(String str, String errorMsg)
  113. throws Exception
  114. {
  115. if(str==null || str.isEmpty()){
  116. Exception exception = new Exception(errorMsg);
  117. throw exception;
  118. }
  119. }
  120. /**
  121. *
  122. * @Title:isValidMobilePhone
  123. * @Description: 判断一个字符串是否为有效的手机号码
  124. * @param szMobilePhone
  125. * @return
  126. * @throws
  127. */
  128. public static boolean isValidMobilePhone(String szMobilePhone){
  129. return isValid(MOBILE_PHONE_NUM_REGEXP, szMobilePhone);
  130. }
  131. /**
  132. *
  133. * @Title:isValidEmailAddr
  134. * @Description: 判断一个字符串是否为有效的邮箱地址
  135. * @param szEmailAddr
  136. * @return
  137. * @throws
  138. */
  139. public static boolean isValidEmailAddr(String szEmailAddr){
  140. return isValid(EMAIL_ADDR_REGEXP, szEmailAddr);
  141. }
  142. /**
  143. *
  144. * @Title:isValid
  145. * @Description: 通用的验证方法
  146. * @param regExp
  147. * @param szValue
  148. * @return
  149. * @throws
  150. */
  151. public static boolean isValid(String regExp, String szValue){
  152. Pattern p = Pattern.compile(regExp);
  153. Matcher m = p.matcher(szValue);
  154. return m.find();
  155. }
  156. }

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/Gausst松鼠会/article/detail/100161
推荐阅读
相关标签
  

闽ICP备14008679号