java字符串工具类

  1 package com.zhouyy.core.utils;
  2 
  3 import org.apache.commons.lang3.RandomStringUtils;
  4 import org.apache.commons.lang3.StringEscapeUtils;
  5 
  6 import java.io.UnsupportedEncodingException;
  7 import java.math.BigDecimal;
  8 import java.net.URLDecoder;
  9 import java.nio.charset.Charset;
 10 import java.util.*;
 11 import java.util.regex.Matcher;
 12 import java.util.regex.Pattern;
 13 
 14 
 15 /**
 16  * 字符串工具类, 继承org.apache.commons.lang3.StringUtils类
 17  *
 18  * @author zhouyy
 19  */
 20 public class StringUtils extends org.apache.commons.lang3.StringUtils {
 21 
 22     /**
 23      * 首字母变小写
 24      *
 25      * @param str 字符串
 26      * @return 首字母为小写的字符串
 27      */
 28     public static String firstCharToLowerCase(String str) {
 29         char firstChar = str.charAt(0);
 30         if (firstChar >= 'A' && firstChar <= 'Z') {
 31             char[] arr = str.toCharArray();
 32             arr[0] += ('a' - 'A');
 33             return new String(arr);
 34         }
 35         return str;
 36     }
 37 
 38     /**
 39      * 首字母变大写
 40      *
 41      * @param str 字符串
 42      * @return 首字母为大写的字符串
 43      */
 44     public static String firstCharToUpperCase(String str) {
 45         char firstChar = str.charAt(0);
 46         if (firstChar >= 'a' && firstChar <= 'z') {
 47             char[] arr = str.toCharArray();
 48             arr[0] -= ('a' - 'A');
 49             return new String(arr);
 50         }
 51         return str;
 52     }
 53 
 54     /**
 55      * 字符串去除回车键
 56      *
 57      * @param str 字符串
 58      * @return 去除回车键后的字符串
 59      */
 60     public static String replaceEnter(String str) {
 61         return str.replace("\n", "");
 62     }
 63 
 64     /**
 65      * 获取UUID
 66      *
 67      * @return UUID
 68      */
 69     public static String getUUId() {
 70         return UUID.randomUUID().toString().replace("-", "");
 71     }
 72 
 73     /**
 74      * 替换掉HTML标签方法
 75      *
 76      * @param html 字符串
 77      * @return 替换掉HTML标签后的字符串
 78      */
 79     public static String replaceHtml(String html) {
 80         if (isBlank(html)) {
 81             return "";
 82         }
 83         String regEx = "<.+?>";
 84         Pattern p = Pattern.compile(regEx);
 85         Matcher m = p.matcher(html);
 86         String s = m.replaceAll("");
 87         return s;
 88     }
 89 
 90     /**
 91      * 缩略字符串(不区分中英文字符)
 92      *
 93      * @param str    目标字符串
 94      * @param length 截取长度
 95      * @return 缩略字符串
 96      */
 97     public static String abbr(String str, int length) {
 98         if (str == null) {
 99             return "";
100         }
101         try {
102             StringBuilder sb = new StringBuilder();
103             int currentLength = 0;
104             for (char c : replaceHtml(StringEscapeUtils.unescapeHtml4(str)).toCharArray()) {
105                 currentLength += String.valueOf(c).getBytes("GBK").length;
106                 if (currentLength <= length - 3) {
107                     sb.append(c);
108                 } else {
109                     sb.append("...");
110                     break;
111                 }
112             }
113             return sb.toString();
114         } catch (UnsupportedEncodingException e) {
115             e.printStackTrace();
116         }
117         return "";
118     }
119 
120 
121     /**
122      * 自定义的分隔字符串函数 例如: 1,2,3 =&gt;[1,2,3] 3个元素 ,2,3=&gt;[,2,3] 3个元素 ,2,3,=&gt;[,2,3,] 4个元素 ,,,=&gt;[,,,] 4个元素
123      * <p>
124      * 5.22算法修改,为提高速度不用正则表达式 两个间隔符,,返回""元素
125      *
126      * @param split 分割字符 默认,
127      * @param src   输入字符串
128      * @return 分隔后的list
129      */
130     public static List<String> splitToList(String split, String src) {
131         // 默认,
132         String sp = ",";
133         if (split != null && split.length() == 1) {
134             sp = split;
135         }
136         List<String> r = new ArrayList<String>();
137         int lastIndex = -1;
138         int index = src.indexOf(sp);
139         if (-1 == index && src != null) {
140             r.add(src);
141             return r;
142         }
143         while (index >= 0) {
144             if (index > lastIndex) {
145                 r.add(src.substring(lastIndex + 1, index));
146             } else {
147                 r.add("");
148             }
149 
150             lastIndex = index;
151             index = src.indexOf(sp, index + 1);
152             if (index == -1) {
153                 r.add(src.substring(lastIndex + 1, src.length()));
154             }
155         }
156         return r;
157     }
158 
159     /**
160      * 判断两个字符串是否相等 如果都为null则判断为相等,一个为null另一个not null则判断不相等 否则如果s1=s2则相等
161      *
162      * @param a 比较对象A
163      * @param b 比较对象B
164      * @return Boolean
165      */
166     public static boolean equals(String a, String b) {
167         return a == null ? b == null : a.equals(b);
168     }
169 
170 
171     /**
172      * 随即生成指定位数的含验证码字符串
173      *
174      * @param bit 指定生成验证码位数
175      * @return 指定位数的含验证码字符串
176      * @since 2007-5-9
177      */
178     public static String random(int bit) {
179         if (bit == 0)
180             bit = 6; // 默认6位
181         // 因为o和0,l和1很难区分,所以,去掉大小写的o和l
182         String str = "";
183         str = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijkmnpqrstuvwxyz";// 初始化种子
184         return RandomStringUtils.random(bit, str);// 返回6位的字符串
185     }
186 
187 
188     /**
189      * 页面中去除字符串中的空格、回车、换行符、制表符
190      *
191      * @param str 字符串
192      * @return String
193      * @since 2007-08-17
194      */
195     public static String replaceBlank(String str) {
196         if (str != null) {
197             Pattern p = Pattern.compile("\\s*|\t|\r|\n");
198             Matcher m = p.matcher(str);
199             str = m.replaceAll("");
200         }
201         return str;
202     }
203 
204 
205     /**
206      * 泛型方法(通用),把list转换成以“,”相隔的字符串 调用时注意类型初始化(申明类型)
207      * <p>
208      * 如:List&lt;Integer&gt; intList = new ArrayList&lt;Integer&gt;(); 调用方法:StringUtils.listTtoString(intList);
209      * <p>
210      * 效率:list中4条信息,1000000次调用时间为850ms左右
211      *
212      * @param <T>  泛型
213      * @param list list列表
214      * @return 以“,”相隔的字符串
215      * @serialData 2008-01-09
216      */
217     public static <T> String listTtoString(List<T> list) {
218         if (list == null || list.size() < 1) {
219             return "";
220         }
221         Iterator<T> i = list.iterator();
222         if (!i.hasNext()) {
223             return "";
224         }
225         StringBuilder sb = new StringBuilder();
226         for (; ; ) {
227             T e = i.next();
228             sb.append(e);
229             if (!i.hasNext()) {
230                 return sb.toString();
231             }
232             sb.append(",");
233         }
234     }
235 
236 
237     /**
238      * 判断文字内容重复
239      *
240      * @param content 文本内容
241      * @return 是否重复
242      */
243     public static boolean isContentRepeat(String content) {
244         int similarNum = 0;
245         int forNum = 0;
246         int subNum = 0;
247         int thousandNum = 0;
248         String startStr = "";
249         String nextStr = "";
250         boolean result = false;
251         float endNum = (float) 0.0;
252         if (content != null && content.length() > 0) {
253             if (content.length() % 1000 > 0) {
254                 thousandNum = (int) Math.floor(content.length() / 1000) + 1;
255             } else {
256                 thousandNum = (int) Math.floor(content.length() / 1000);
257             }
258             if (thousandNum < 3) {
259                 subNum = 100 * thousandNum;
260             } else if (thousandNum < 6) {
261                 subNum = 200 * thousandNum;
262             } else if (thousandNum < 9) {
263                 subNum = 300 * thousandNum;
264             } else {
265                 subNum = 3000;
266             }
267             for (int j = 1; j < subNum; j++) {
268                 if (content.length() % j > 0) {
269                     forNum = (int) Math.floor(content.length() / j) + 1;
270                 } else {
271                     forNum = (int) Math.floor(content.length() / j);
272                 }
273                 if (result || j >= content.length()) {
274                     break;
275                 } else {
276                     for (int m = 0; m < forNum; m++) {
277                         if (m * j > content.length() || (m + 1) * j > content.length() || (m + 2) * j > content.length()) {
278                             break;
279                         }
280                         startStr = content.substring(m * j, (m + 1) * j);
281                         nextStr = content.substring((m + 1) * j, (m + 2) * j);
282                         if (startStr.equals(nextStr)) {
283                             similarNum = similarNum + 1;
284                             endNum = (float) similarNum / forNum;
285                             if (endNum > 0.4) {
286                                 result = true;
287                                 break;
288                             }
289                         } else {
290                             similarNum = 0;
291                         }
292                     }
293                 }
294             }
295         }
296         return result;
297     }
298 
299     /**
300      * 判断是否是空字符串 null和"" null返回result,否则返回字符串
301      *
302      * @param s      判断字符串
303      * @param result 备用字符串
304      * @return 判断字符串为空则然后备用字符串, 否则返回字符串
305      */
306     public static String isEmpty(String s, String result) {
307         if (s != null && !"".equals(s)) {
308             return s;
309         }
310         return result;
311     }
312 
313     /**
314      * 判断对象是否为空
315      *
316      * @param str 对象
317      * @return 是否为空
318      */
319     public static boolean isNotEmpty(Object str) {
320         boolean flag = false;
321         if (str != null && !"".equals(str)) {
322             if (str.toString().trim().length() > 0) {
323                 flag = true;
324             }
325         } else {
326             flag = false;
327         }
328         return flag;
329     }
330 
331     /**
332      * 判断字符串数据是否为空
333      *
334      * @param strArr 字符串数组
335      * @return 是否为空
336      */
337     public static boolean isEmpty(String[] strArr) {
338         return !isNotEmpty(strArr);
339     }
340 
341     /**
342      * 判断字符串数据是否不为空
343      *
344      * @param strArr 字符串
345      * @return 是否不为空
346      */
347     public static boolean isNotEmpty(String[] strArr) {
348         boolean flag = true;
349         if (strArr == null || strArr.length == 0) {
350             return false;
351         }
352         for (String str : strArr) {
353             if (!isNotEmpty(str)) {
354                 return false;
355             }
356         }
357         return flag;
358     }
359 
360 
361     private static String getProperty(String property) {
362         if (property.contains("_")) {
363             return property.replaceAll("_", "\\.");
364         }
365         return property;
366     }
367 
368     /**
369      * 解析前台encodeURIComponent编码后的参数
370      *
371      * @param property 解析字符串
372      * @return String
373      */
374     public static String getEncodePra(String property) {
375         String trem = "";
376         if (isNotEmpty(property)) {
377             try {
378                 trem = URLDecoder.decode(property, "UTF-8");
379             } catch (UnsupportedEncodingException e) {
380                 e.printStackTrace();
381             }
382         }
383         return trem;
384     }
385 
386     /**
387      * 解析前台参数乱码问题,把ISO8895-1编码解析为UTF-8
388      *
389      * @param str 解析字符串
390      * @return String 解析后的字符串
391      */
392     public static String getUtf8String(String str) {
393         if (str != null && str.length() > 0) {
394             String needEncodeCode = "ISO-8859-1";
395             try {
396                 if (Charset.forName(needEncodeCode).newEncoder().canEncode(str)) {//这个方法是关键,可以判断乱码字符串是否为指定的编码
397                     str = new String(str.getBytes(needEncodeCode), "UTF-8");
398                 }
399             } catch (UnsupportedEncodingException e) {
400                 e.printStackTrace();
401             }
402         }
403         return str;
404     }
405 
406     /**
407      * 判断一个字符串是否都为数字
408      *
409      * @param strNum 数字字符串
410      * @return 是否都为数字
411      */
412     public boolean isDigit(String strNum) {
413         Pattern pattern = Pattern.compile("[0-9]{1,}");
414         Matcher matcher = pattern.matcher((CharSequence) strNum);
415         return matcher.matches();
416     }
417 
418     /**
419      * 截取数字
420      *
421      * @param content 字符串
422      * @return 截取后的数字
423      */
424     public String getNumbers(String content) {
425         Pattern pattern = Pattern.compile("\\d+");
426         Matcher matcher = pattern.matcher(content);
427         while (matcher.find()) {
428             return matcher.group(0);
429         }
430         return "";
431     }
432 
433     /**
434      * 截取非数字
435      *
436      * @param content 字符串
437      * @return 截取后的非数字
438      */
439     public String splitNotNumber(String content) {
440         Pattern pattern = Pattern.compile("\\D+");
441         Matcher matcher = pattern.matcher(content);
442         while (matcher.find()) {
443             return matcher.group(0);
444         }
445         return "";
446     }
447 
448     /**
449      * 判断某个字符串是否存在于数组中
450      *
451      * @param stringArray 原数组
452      * @param source      查找的字符串
453      * @return 是否找到
454      */
455     public static boolean contains(String[] stringArray, String source) {
456         // 转换为list
457         List<String> tempList = Arrays.asList(stringArray);
458 
459         // 利用list的包含方法,进行判断
460         if (tempList.contains(source)) {
461             return true;
462         } else {
463             return false;
464         }
465     }
466 
467     /**
468      * 中文日期转换
469      *
470      * @param num 传yyy或MM或dd
471      * @return 转换后字符串
472      */
473     public static String chineseYMD(String num) {
474         String[] chinese = {"零", "壹", "贰", "叁", "肆", "伍", "陆", "柒", "捌", "玖"};
475         int slen = num.length();
476         String result = "";
477         if (slen > 2) {
478             for (int i = 0; i < slen; i++) {
479                 int numInt = Integer.parseInt(num.substring(i, i + 1));
480                 result += chinese[numInt];
481             }
482         } else {
483             int numInt = Integer.parseInt(num);
484             if (numInt < 10) {
485                 result = "零" + chinese[numInt];
486             } else {
487                 for (int i = 0; i < slen; i++) {
488                     int num1 = Integer.parseInt(num.substring(i, i + 1));
489                     if (i == 0) {
490                         result += chinese[num1] + "拾";
491                     } else if (num1 != 0) {
492                         result += chinese[num1];
493                     }
494                 }
495             }
496             if (numInt == 10 || numInt == 20 || numInt == 30) {
497                 result = "零" + result;
498             }
499         }
500         return result;
501     }
502 
503     /**
504      * 数字金额转换
505      *
506      * @param num 金额信息
507      * @return 转换后字符串
508      */
509     public static String numToChinese(String num) {
510         String[] chinese = {"零", "壹", "贰", "叁", "肆", "伍", "陆", "柒", "捌", "玖"};
511         int slen = num.length();
512         String result = "";
513         for (int i = 0; i < slen; i++) {
514             int numInt = Integer.parseInt(num.substring(i, i + 1));
515             result += chinese[numInt];
516         }
517         return result;
518     }
519 
520     /**
521      * 中文金额
522      *
523      * @param smallMoney 金额
524      * @return 中文金额
525      */
526     public static String chineseMoney(BigDecimal smallMoney) {
527         if (smallMoney == null) {
528             smallMoney = BigDecimal.ZERO;
529         }
530         return chineseMoney(smallMoney.doubleValue());
531     }
532 
533     /**
534      * 中文金额
535      *
536      * @param smallMoney 金额
537      * @return 中文金额
538      */
539     public static String chineseMoney(double smallMoney) {
540         String value = String.valueOf(smallMoney);
541         if (null == value || "".equals(value.trim()) || smallMoney == 0) {
542             return "零";
543         }
544 
545         // String strCheck,strArr,strFen,strDW,strNum,strBig,strNow;
546         String strCheck, strFen, strDW, strNum, strBig, strNow;
547 
548         strCheck = value + ".";
549         int dot = strCheck.indexOf(".");
550         if (dot > 12) {
551             return "数据" + value + "过大,无法处理!";
552         }
553 
554         try {
555             int i = 0;
556             strBig = "";
557             strDW = "";
558             strNum = "";
559             if (smallMoney < 0) {
560                 smallMoney = smallMoney * -1;
561             }
562             double dFen = smallMoney * 100;
563             // strFen = String.valueOf(intFen);
564             strFen = NumberUtil.formatNumeric(dFen, "###0");
565             int lenIntFen = strFen.length();
566             while (lenIntFen != 0) {
567                 i++;
568                 switch (i) {
569                     case 1:
570                         strDW = "分";
571                         break;
572                     case 2:
573                         strDW = "角";
574                         break;
575                     case 3:
576                         strDW = "元";
577                         break;
578                     case 4:
579                         strDW = "拾";
580                         break;
581                     case 5:
582                         strDW = "佰";
583                         break;
584                     case 6:
585                         strDW = "仟";
586                         break;
587                     case 7:
588                         strDW = "万";
589                         break;
590                     case 8:
591                         strDW = "拾";
592                         break;
593                     case 9:
594                         strDW = "佰";
595                         break;
596                     case 10:
597                         strDW = "仟";
598                         break;
599                     case 11:
600                         strDW = "亿";
601                         break;
602                     case 12:
603                         strDW = "拾";
604                         break;
605                     case 13:
606                         strDW = "佰";
607                         break;
608                     case 14:
609                         strDW = "仟";
610                         break;
611                 }
612                 switch (strFen.charAt(lenIntFen - 1)) // 选择数字
613                 {
614                     case '1':
615                         strNum = "壹";
616                         break;
617                     case '2':
618                         strNum = "贰";
619                         break;
620                     case '3':
621                         strNum = "叁";
622                         break;
623                     case '4':
624                         strNum = "肆";
625                         break;
626                     case '5':
627                         strNum = "伍";
628                         break;
629                     case '6':
630                         strNum = "陆";
631                         break;
632                     case '7':
633                         strNum = "柒";
634                         break;
635                     case '8':
636                         strNum = "捌";
637                         break;
638                     case '9':
639                         strNum = "玖";
640                         break;
641                     case '0':
642                         strNum = "零";
643                         break;
644                 }
645                 // 处理特殊情况
646                 strNow = strBig;
647                 // 分为零时的情况
648                 if ((i == 1) && (strFen.charAt(lenIntFen - 1) == '0')) {
649                     strBig = "整";
650                 }
651                 // 角为零时的情况
652                 else if ((i == 2) && (strFen.charAt(lenIntFen - 1) == '0')) { // 角分同时为零时的情况
653                     if (!strBig.equals("整")) {
654                         strBig = "零" + strBig;
655                     }
656                 }
657                 // 元为零的情况
658                 else if ((i == 3) && (strFen.charAt(lenIntFen - 1) == '0')) {
659                     strBig = "元" + strBig;
660                 }
661                 // 拾-仟中一位为零且其前一位(元以上)不为零的情况时补零
662                 else if ((i < 7) && (i > 3)
663                         && (strFen.charAt(lenIntFen - 1) == '0')
664                         && (strNow.charAt(0) != '零')
665                         && (strNow.charAt(0) != '元')) {
666                     strBig = "零" + strBig;
667                 }
668                 // 拾-仟中一位为零且其前一位(元以上)也为零的情况时跨过
669                 else if ((i < 7) && (i > 3)
670                         && (strFen.charAt(lenIntFen - 1) == '0')
671                         && (strNow.charAt(0) == '零')) {
672                 }
673                 // 拾-仟中一位为零且其前一位是元且为零的情况时跨过
674                 else if ((i < 7) && (i > 3)
675                         && (strFen.charAt(lenIntFen - 1) == '0')
676                         && (strNow.charAt(0) == '元')) {
677                 }
678                 // 当万为零时必须补上万字
679                 else if ((i == 7) && (strFen.charAt(lenIntFen - 1) == '0')) {
680                     strBig = "万" + strBig;
681                 }
682                 // 拾万-仟万中一位为零且其前一位(万以上)不为零的情况时补零
683                 else if ((i < 11) && (i > 7)
684                         && (strFen.charAt(lenIntFen - 1) == '0')
685                         && (strNow.charAt(0) != '零')
686                         && (strNow.charAt(0) != '万')) {
687                     strBig = "零" + strBig;
688                 }
689                 // 拾万-仟万中一位为零且其前一位(万以上)也为零的情况时跨过
690                 else if ((i < 11) && (i > 7)
691                         && (strFen.charAt(lenIntFen - 1) == '0')
692                         && (strNow.charAt(0) == '万')) {
693                 }
694                 // 拾万-仟万中一位为零且其前一位为万位且为零的情况时跨过
695                 else if ((i < 11) && (i > 7)
696                         && (strFen.charAt(lenIntFen - 1) == '0')
697                         && (strNow.charAt(0) == '零')) {
698                 }
699                 // 万位为零且存在仟位和十万以上时,在万仟间补零
700                 else if ((i < 11) && (i > 8)
701                         && (strFen.charAt(lenIntFen - 1) == '0')
702                         && (strNow.charAt(0) == '万')
703                         && (strNow.charAt(2) == '仟')) {
704                     strBig = strNum + strDW + "万零"
705                             + strBig.substring(1, strBig.length());
706                 }
707                 // 单独处理亿位
708                 else if (i == 11) {
709                     // 亿位为零且万全为零存在仟位时,去掉万补为零
710                     if ((strFen.charAt(lenIntFen - 1) == '0')
711                             && (strNow.charAt(0) == '万')
712                             && (strNow.charAt(2) == '仟')) {
713                         strBig = "亿" + "零"
714                                 + strBig.substring(1, strBig.length());
715                     }
716                     // 亿位为零且万全为零不存在仟位时,去掉万
717                     else if ((strFen.charAt(lenIntFen - 1) == '0')
718                             && (strNow.charAt(0) == '万')
719                             && (strNow.charAt(2) != '仟')) {
720                         strBig = "亿" + strBig.substring(1, strBig.length());
721                     }
722                     // 亿位不为零且万全为零存在仟位时,去掉万补为零
723                     else if ((strNow.charAt(0) == '万')
724                             && (strNow.charAt(2) == '仟')) {
725                         strBig = strNum + strDW + "零"
726                                 + strBig.substring(1, strBig.length());
727                     }
728                     // 亿位不为零且万全为零不存在仟位时,去掉万
729                     else if ((strNow.charAt(0) == '万')
730                             && (strNow.charAt(2) != '仟')) {
731                         strBig = strNum + strDW
732                                 + strBig.substring(1, strBig.length());
733                     }
734                     // 其他正常情况
735                     else {
736                         strBig = strNum + strDW + strBig;
737                     }
738                 }
739                 // 拾亿-仟亿中一位为零且其前一位(亿以上)不为零的情况时补零
740                 else if ((i < 15) && (i > 11)
741                         && (strFen.charAt(lenIntFen - 1) == '0')
742                         && (strNow.charAt(0) != '零')
743                         && (strNow.charAt(0) != '亿')) {
744                     strBig = "零" + strBig;
745                 }
746                 // 拾亿-仟亿中一位为零且其前一位(亿以上)也为零的情况时跨过
747                 else if ((i < 15) && (i > 11)
748                         && (strFen.charAt(lenIntFen - 1) == '0')
749                         && (strNow.charAt(0) == '亿')) {
750                 }
751                 // 拾亿-仟亿中一位为零且其前一位为亿位且为零的情况时跨过
752                 else if ((i < 15) && (i > 11)
753                         && (strFen.charAt(lenIntFen - 1) == '0')
754                         && (strNow.charAt(0) == '零')) {
755                 }
756                 // 亿位为零且不存在仟万位和十亿以上时去掉上次写入的零
757                 else if ((i < 15) && (i > 11)
758                         && (strFen.charAt(lenIntFen - 1) != '0')
759                         && (strNow.charAt(0) == '零')
760                         && (strNow.charAt(1) == '亿')
761                         && (strNow.charAt(3) != '仟')) {
762                     strBig = strNum + strDW
763                             + strBig.substring(1, strBig.length());
764                 }
765                 // 亿位为零且存在仟万位和十亿以上时,在亿仟万间补零
766                 else if ((i < 15) && (i > 11)
767                         && (strFen.charAt(lenIntFen - 1) != '0')
768                         && (strNow.charAt(0) == '零')
769                         && (strNow.charAt(1) == '亿')
770                         && (strNow.charAt(3) == '仟')) {
771                     strBig = strNum + strDW + "亿零"
772                             + strBig.substring(2, strBig.length());
773                 } else {
774                     strBig = strNum + strDW + strBig;
775                 }
776                 strFen = strFen.substring(0, lenIntFen - 1);
777                 lenIntFen--;
778             }
779             // if(flag<0)strBig="负"+strBig;
780             return strBig;
781         } catch (Exception e) {
782             return "";
783         }
784     }
785 
786     /**
787      * 判断是否null值
788      *
789      * @param css 可变字符串参数
790      * @return 是否null值
791      */
792     public static boolean isNoneBlank(CharSequence... css) {
793         return !isAnyBlank(css);
794     }
795 
796     /**
797      * 判断是否null值
798      *
799      * @param css 可变字符串参数
800      * @return 是否null值
801      */
802     public static boolean isAnyBlank(CharSequence... css) {
803         if (css == null || css.length == 0) {
804             return true;
805         } else {
806             CharSequence[] arr$ = css;
807             int len$ = css.length;
808 
809             for (int i$ = 0; i$ < len$; ++i$) {
810                 CharSequence cs = arr$[i$];
811                 if (cs == null) {
812                     return true;
813                 }
814             }
815 
816             return false;
817         }
818     }
819 
820     /**
821      * 把null格式化成""
822      *
823      * @param str 字符串
824      * @return 格式化后字符串
825      */
826     public static String formatNull(String str) {
827         if (str == null || "null".equals(str)) {
828             return "";
829         } else {
830             return str;
831         }
832     }
833 
834     /**
835      * 判断是否为null值
836      *
837      * @param strings 字符串
838      * @return 是否为null值
839      */
840     public static boolean isAllBlank(String... strings) {
841         String[] arr$ = strings;
842         int len$ = strings.length;
843 
844         for (int i$ = 0; i$ < len$; ++i$) {
845             String string = arr$[i$];
846             if (!isBlank(string)) {
847                 return false;
848             }
849         }
850 
851         return true;
852     }
853 
854     public static final char UNDERLINE = '_';
855 
856     /**
857      * 驼峰格式字符串转换为下划线格式字符串
858      *
859      * @param param
860      * @return
861      */
862     public static String camelToUnderline(String param) {
863         if (param == null || "".equals(param.trim())) {
864             return "";
865         }
866         int len = param.length();
867         StringBuilder sb = new StringBuilder(len);
868         for (int i = 0; i < len; i++) {
869             char c = param.charAt(i);
870             if (Character.isUpperCase(c)) {
871                 sb.append(UNDERLINE);
872                 sb.append(Character.toLowerCase(c));
873             } else {
874                 sb.append(c);
875             }
876         }
877         return sb.toString();
878     }
879 
880     /**
881      * 下划线格式字符串转换为驼峰格式字符串
882      *
883      * @param param
884      * @return
885      */
886     public static String underlineToCamel(String param) {
887         if (param == null || "".equals(param.trim())) {
888             return "";
889         }
890         StringBuilder sb = new StringBuilder(param);
891         Matcher mc = Pattern.compile("_").matcher(param);
892         int i = 0;
893         while (mc.find()) {
894             int position = mc.end() - (i++);
895             sb.replace(position - 1, position + 1, sb.substring(position, position + 1).toUpperCase());
896         }
897         return sb.toString();
898     }
899 
900     /**
901      * 对xss攻击字符串进行正则表达式匹配过滤
902      *
903      * @param value
904      * @return
905      */
906     public static String xssEscape(String value) {
907         if (StringUtils.isNotEmpty(value)) {
908             // Avoid anything between script tags
909             Pattern scriptPattern = Pattern.compile("<script>(.*?)</script>", Pattern.CASE_INSENSITIVE);
910             value = scriptPattern.matcher(value).replaceAll("");
911 
912             // Avoid anything in a src='...' type of e­xpression
913             scriptPattern = Pattern.compile("src[\r\n]*=[\r\n]*\\\'(.*?)\\\'", Pattern.CASE_INSENSITIVE | Pattern.MULTILINE | Pattern.DOTALL);
914             value = scriptPattern.matcher(value).replaceAll("");
915 
916             scriptPattern = Pattern.compile("src[\r\n]*=[\r\n]*\\\"(.*?)\\\"", Pattern.CASE_INSENSITIVE | Pattern.MULTILINE | Pattern.DOTALL);
917             value = scriptPattern.matcher(value).replaceAll("");
918 
919             // Remove any lonesome </script> tag
920             scriptPattern = Pattern.compile("</script>", Pattern.CASE_INSENSITIVE);
921             value = scriptPattern.matcher(value).replaceAll("");
922 
923             // Remove any lonesome <script ...> tag
924             scriptPattern = Pattern.compile("<script(.*?)>", Pattern.CASE_INSENSITIVE | Pattern.MULTILINE | Pattern.DOTALL);
925             value = scriptPattern.matcher(value).replaceAll("");
926 
927             // Avoid eval(...) e­xpressions
928             scriptPattern = Pattern.compile("eval\\((.*?)\\)", Pattern.CASE_INSENSITIVE | Pattern.MULTILINE | Pattern.DOTALL);
929             value = scriptPattern.matcher(value).replaceAll("");
930 
931             // Avoid e­xpression(...) e­xpressions
932             scriptPattern = Pattern.compile("e­xpression\\((.*?)\\)", Pattern.CASE_INSENSITIVE | Pattern.MULTILINE | Pattern.DOTALL);
933             value = scriptPattern.matcher(value).replaceAll("");
934 
935             // Avoid javascript:... e­xpressions
936             scriptPattern = Pattern.compile("javascript:", Pattern.CASE_INSENSITIVE);
937             value = scriptPattern.matcher(value).replaceAll("");
938 
939             // Avoid vbscript:... e­xpressions
940             scriptPattern = Pattern.compile("vbscript:", Pattern.CASE_INSENSITIVE);
941             value = scriptPattern.matcher(value).replaceAll("");
942 
943             // Avoid onload= e­xpressions
944             scriptPattern = Pattern.compile("onload(.*?)=", Pattern.CASE_INSENSITIVE | Pattern.MULTILINE | Pattern.DOTALL);
945             value = scriptPattern.matcher(value).replaceAll("");
946         }
947         return value;
948     }
949 }

 

posted @ 2021-03-09 16:09  _万古如长夜  阅读(650)  评论(0编辑  收藏  举报