|
@@ -0,0 +1,338 @@
|
|
|
+package com.rongwei.bscommon.sys.utils;
|
|
|
+
|
|
|
+import org.apache.commons.lang3.BooleanUtils;
|
|
|
+
|
|
|
+import javax.servlet.http.HttpServletRequest;
|
|
|
+import java.util.Arrays;
|
|
|
+import java.util.UUID;
|
|
|
+import java.util.regex.Matcher;
|
|
|
+import java.util.regex.Pattern;
|
|
|
+
|
|
|
+/**
|
|
|
+ * @author bootdo
|
|
|
+ */
|
|
|
+public class StringUtil extends org.apache.commons.lang3.StringUtils {
|
|
|
+
|
|
|
+ public static final char UNDERLINE = '_';
|
|
|
+ // 科学计数法的正则表达式
|
|
|
+ private static String scientificNotationPattern = "^[-+]?[0-9]*\\.?[0-9]+([eE][-+]?[0-9]+)?$";
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 驼峰格式字符串转换为下划线格式字符串
|
|
|
+ *
|
|
|
+ * @param param
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public static String camelToUnderline(String param) {
|
|
|
+ if (param == null || "".equals(param.trim())) {
|
|
|
+ return "";
|
|
|
+ }
|
|
|
+ int len = param.length();
|
|
|
+ StringBuilder sb = new StringBuilder(len);
|
|
|
+ for (int i = 0; i < len; i++) {
|
|
|
+ char c = param.charAt(i);
|
|
|
+ if (Character.isUpperCase(c)) {
|
|
|
+ sb.append(UNDERLINE);
|
|
|
+ sb.append(Character.toLowerCase(c));
|
|
|
+ } else {
|
|
|
+ sb.append(c);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return sb.toString();
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 下划线格式字符串转换为驼峰格式字符串
|
|
|
+ *
|
|
|
+ * @param param
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public static String underlineToCamel(String param) {
|
|
|
+ if (param == null || "".equals(param.trim())) {
|
|
|
+ return "";
|
|
|
+ }
|
|
|
+ int len = param.length();
|
|
|
+ StringBuilder sb = new StringBuilder(len);
|
|
|
+ for (int i = 0; i < len; i++) {
|
|
|
+ char c = param.charAt(i);
|
|
|
+ if (c == UNDERLINE) {
|
|
|
+ if (++i < len) {
|
|
|
+ sb.append(Character.toUpperCase(param.charAt(i)));
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ sb.append(c);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return sb.toString();
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 下划线格式字符串转换为驼峰格式字符串2
|
|
|
+ *
|
|
|
+ * @param param
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public static String underlineToCamel2(String param) {
|
|
|
+ if (param == null || "".equals(param.trim())) {
|
|
|
+ return "";
|
|
|
+ }
|
|
|
+ StringBuilder sb = new StringBuilder(param);
|
|
|
+ Matcher mc = Pattern.compile("_").matcher(param);
|
|
|
+ int i = 0;
|
|
|
+ while (mc.find()) {
|
|
|
+ int position = mc.end() - (i++);
|
|
|
+ sb.replace(position - 1, position + 1, sb.substring(position, position + 1).toUpperCase());
|
|
|
+ }
|
|
|
+ return sb.toString();
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 得到二位数的序号
|
|
|
+ *
|
|
|
+ * @param num
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public static String getNextCode(Integer num) {
|
|
|
+ if (num == null || num == 0) {
|
|
|
+ return "001";
|
|
|
+ } else if (num > 0 && num < 9) {
|
|
|
+ return "00" + (num + 1) + "";
|
|
|
+ } else if (num == 9) {
|
|
|
+ return "010";
|
|
|
+ } else if (num >= 10 && num < 98) {
|
|
|
+ return "0" + (num + 1) + "";
|
|
|
+ } else if (num == 99) {
|
|
|
+ return "100";
|
|
|
+ } else {
|
|
|
+ return (num + 1) + "";
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 将传入的对象转换为字符串,当传入的对象为null时返回默认值
|
|
|
+ * bw.ren 2019年6月3日09:46:33
|
|
|
+ *
|
|
|
+ * @param o
|
|
|
+ * @param dv
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public static String safeToString(Object o, String dv) {
|
|
|
+ String r = dv;
|
|
|
+ if (o != null) {
|
|
|
+ r = String.valueOf(o);
|
|
|
+ }
|
|
|
+ return r;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获得用户远程地址
|
|
|
+ */
|
|
|
+ public static String getRemoteAddr(HttpServletRequest request) {
|
|
|
+ String remoteAddr = request.getHeader("X-Real-IP");
|
|
|
+ if (isNotBlank(remoteAddr)) {
|
|
|
+ remoteAddr = request.getHeader("X-Forwarded-For");
|
|
|
+ } else if (isNotBlank(remoteAddr)) {
|
|
|
+ remoteAddr = request.getHeader("Proxy-Client-IP");
|
|
|
+ } else if (isNotBlank(remoteAddr)) {
|
|
|
+ remoteAddr = request.getHeader("WL-Proxy-Client-IP");
|
|
|
+ }
|
|
|
+ return remoteAddr != null ? remoteAddr : request.getRemoteAddr();
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 如果对象为空,则使用defaultVal值
|
|
|
+ * see: ObjectUtils.toString(obj, defaultVal)
|
|
|
+ *
|
|
|
+ * @param obj
|
|
|
+ * @param defaultVal
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public static String toString(final Object obj, final String defaultVal) {
|
|
|
+ return obj == null ? defaultVal : obj.toString();
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 转换为Boolean类型
|
|
|
+ * 'true', 'on', 'y', 't', 'yes' or '1' (case insensitive) will return true. Otherwise, false is returned.
|
|
|
+ */
|
|
|
+ public static Boolean toBoolean(final Object val) {
|
|
|
+ if (val == null) {
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+ return BooleanUtils.toBoolean(val.toString()) || "1".equals(val.toString());
|
|
|
+ }
|
|
|
+
|
|
|
+ public static boolean isNumericzidai(String str) {
|
|
|
+ Pattern pattern = Pattern.compile("-?[0-9]+\\.?[0-9]*");
|
|
|
+ Matcher isNum = pattern.matcher(str);
|
|
|
+ if (!isNum.matches()) {
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+ return true;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 正则:手机号(简单), 1字头+10位数字即可.
|
|
|
+ *
|
|
|
+ * @param in
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public static boolean validateMobilePhone(String in) {
|
|
|
+ Pattern pattern = Pattern.compile("^[1]\\d{10}$");
|
|
|
+ return pattern.matcher(in).matches();
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 正则:邮箱
|
|
|
+ *
|
|
|
+ * @param in
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public static boolean validateEmail(String in) {
|
|
|
+ Pattern pattern = Pattern.compile("[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\\.[a-zA-Z]{2,}");
|
|
|
+ return pattern.matcher(in).matches();
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 是否是数字
|
|
|
+ * @param value
|
|
|
+ */
|
|
|
+ public static boolean isNum(String value){
|
|
|
+ Pattern p=null;//正则表达式
|
|
|
+ Matcher m=null;//操作符表达式
|
|
|
+ boolean b=false;
|
|
|
+ p=p.compile("^([+-]?)\\d*\\.?\\d+$");
|
|
|
+ m=p.matcher(value);
|
|
|
+ b=m.matches();
|
|
|
+ return b;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 根据正则截取字符串
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public static String InterceptString(String projectName) {
|
|
|
+ Pattern pattern = Pattern.compile(".*?(?=\\()");
|
|
|
+ Matcher matcher = pattern.matcher(projectName);
|
|
|
+ while(matcher.find()){
|
|
|
+ return matcher.group();
|
|
|
+ }
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 判断是否是身份证号
|
|
|
+ * @param cardNo
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public static boolean isCard(String cardNo){
|
|
|
+ String regularExpression = "(^[1-9]\\d{5}(18|19|20)\\d{2}((0[1-9])|(10|11|12))(([0-2][1-9])|10|20|30|31)\\d{3}[0-9Xx]$)|" +
|
|
|
+ "(^[1-9]\\d{5}\\d{2}((0[1-9])|(10|11|12))(([0-2][1-9])|10|20|30|31)\\d{3}$)";
|
|
|
+ boolean matches = cardNo.matches(regularExpression);
|
|
|
+ //判断第18位校验值
|
|
|
+ if (matches) {
|
|
|
+ if (cardNo.length() == 18) {
|
|
|
+ try {
|
|
|
+ char[] charArray = cardNo.toCharArray();
|
|
|
+ //前十七位加权因子
|
|
|
+ int[] idCardWi = {7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2};
|
|
|
+ //这是除以11后,可能产生的11位余数对应的验证码
|
|
|
+ String[] idCardY = {"1", "0", "X", "9", "8", "7", "6", "5", "4", "3", "2"};
|
|
|
+ int sum = 0;
|
|
|
+ for (int i = 0; i < idCardWi.length; i++) {
|
|
|
+ int current = Integer.parseInt(String.valueOf(charArray[i]));
|
|
|
+ int count = current * idCardWi[i];
|
|
|
+ sum += count;
|
|
|
+ }
|
|
|
+ char idCardLast = charArray[17];
|
|
|
+ int idCardMod = sum % 11;
|
|
|
+ if (idCardY[idCardMod].toUpperCase().equals(String.valueOf(idCardLast).toUpperCase())) {
|
|
|
+ return true;
|
|
|
+ } else {
|
|
|
+ System.out.println("身份证最后一位:" + String.valueOf(idCardLast).toUpperCase() +
|
|
|
+ "错误,正确的应该是:" + idCardY[idCardMod].toUpperCase());
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+ } catch (Exception e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ System.out.println("异常:" + cardNo);
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return matches;
|
|
|
+ }
|
|
|
+
|
|
|
+ public static void main(String[] args) {
|
|
|
+ boolean a = isScience("123456");
|
|
|
+ System.out.print(a);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 根据正则截取字符串
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public static int getArrayElementPosition(String[] actualWeldInfo,String actualWeld) {
|
|
|
+ Arrays.sort(actualWeldInfo);
|
|
|
+ int position = Arrays.binarySearch(actualWeldInfo, actualWeld);
|
|
|
+ return position;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 格式化字符串中的小数点
|
|
|
+ */
|
|
|
+ public static String formatDecimal(String str) {
|
|
|
+ str = str.replaceAll("[.](.*)","");
|
|
|
+ return str;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 四舍五入小数
|
|
|
+ */
|
|
|
+ public static String formatDecimalFiveIn(String str) {
|
|
|
+ str = String.valueOf(Math.round(Float.parseFloat(str)));
|
|
|
+ return str;
|
|
|
+ }
|
|
|
+
|
|
|
+// public static void main(String args[]){
|
|
|
+//// String[] intArray = new String[]{"a","b","c","d","e","f","g","h","i","j",};
|
|
|
+//// String actualWeld = "vt1684_pt793_50_500,vt1684_ut793_30_300,vt1684_rt793_60_600,vt1684_mt793_60_600";
|
|
|
+//// String[] actualWeldInfo = actualWeld.split(",");
|
|
|
+//// Arrays.sort(actualWeldInfo);
|
|
|
+//// int positon = Arrays.binarySearch(actualWeldInfo, "vt1684_pt793_50_500");
|
|
|
+//// System.out.println("position is:"+positon);
|
|
|
+// String actualWeld = formatDecimal(String.valueOf(1000.98));
|
|
|
+// System.out.print(actualWeld);
|
|
|
+// }
|
|
|
+
|
|
|
+ public static int querySpecificCharacter(String flawInspectCode) {
|
|
|
+ int count = 0;
|
|
|
+ int strLength = flawInspectCode.length();
|
|
|
+ String searchChar = "R";
|
|
|
+ flawInspectCode = flawInspectCode.replace(searchChar, "");
|
|
|
+ int newLength = flawInspectCode.length();
|
|
|
+ count = strLength - newLength;
|
|
|
+ return count;
|
|
|
+ }
|
|
|
+
|
|
|
+ public static String getUUID(){
|
|
|
+ String uuid = UUID.randomUUID().toString().replaceAll("-","");
|
|
|
+ return uuid;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 判断是否包含字母
|
|
|
+ * @param ret
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public static boolean isScience(String ret) {
|
|
|
+ return ret.matches(scientificNotationPattern);
|
|
|
+ }
|
|
|
+
|
|
|
+}
|