字符串工具类

继承org.apache.commons.lang3.StringUtils类

 

 

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

 

posted @ 2019-11-07 21:19  _万古如长夜  阅读(731)  评论(0编辑  收藏  举报