1
zj
2024-08-12 11bb86a81c99672e5e51ca7289f49a57346739e8
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
package util;
 
import java.beans.Introspector;
import java.beans.PropertyDescriptor;
import java.io.IOException;
import java.io.InputStream;
import java.math.BigDecimal;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.List;
import java.util.StringTokenizer;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
 
public class Strings {
 
    public static String unqualify(String name) {
        return unqualify(name, '.');
    }
 
    public static String unqualify(String name, char sep) {
        return name.substring(name.lastIndexOf(sep) + 1, name.length());
    }
    
    /**list是否为空
     * true 空;false 不空
     * @param list
     * @return
     */
    public static boolean ListIsNull(List list){
        if(null!=list&&list.size()>=1)
            return false;
        else return true;
    }
    
    public static boolean isEmpty(String string) {
        if("null".equals(string))return true;
        return string == null || string.trim().length() == 0;
    }
    public static boolean isEmpty(Integer integer) {
        
        return integer==null;
    }
    public static String nullIfEmpty(String string) {
        return isEmpty(string) ? null : string;
    }
 
    public static String emptyIfNull(String string) {
        return (string == null || "null".equals(string)) ? "" : string.trim();
    }
    public static String trimString(String string){
        
        return string != null ? string.trim() : string;
    }
 
 
    public static String toString(Object component) {
        try {
            PropertyDescriptor[] props = Introspector.getBeanInfo(
                    component.getClass()).getPropertyDescriptors();
            StringBuilder builder = new StringBuilder();
            for (PropertyDescriptor descriptor : props) {
                builder.append(descriptor.getName()).append("=").append(
                        descriptor.getReadMethod().invoke(component)).append(
                        "; ");
            }
            return builder.toString();
        } catch (Exception e) {
            return "";
        }
    }
 
    public static String[] split(String strings, String delims) {
        if (strings == null) {
            return new String[0];
        } else {
            StringTokenizer tokens = new StringTokenizer(strings, delims);
            String[] result = new String[tokens.countTokens()];
            int i = 0;
            while (tokens.hasMoreTokens()) {
                result[i++] = tokens.nextToken();
            }
            return result;
        }
    }
 
    public static String toString(Object... objects) {
        return toString(" ", objects);
    }
 
    public static String toString(String sep, Object... objects) {
        if (objects.length == 0)
            return "";
        StringBuilder builder = new StringBuilder();
        for (Object object : objects) {
            builder.append(sep).append(object);
        }
        return builder.substring(2);
    }
 
    public static String toClassNameString(String sep, Object... objects) {
        if (objects.length == 0)
            return "";
        StringBuilder builder = new StringBuilder();
        for (Object object : objects) {
            builder.append(sep);
            if (object == null) {
                builder.append("null");
            } else {
                builder.append(object.getClass().getName());
            }
        }
        return builder.substring(2);
    }
 
    public static String toString(String sep, Class... classes) {
        if (classes.length == 0)
            return "";
        StringBuilder builder = new StringBuilder();
        for (Class clazz : classes) {
            builder.append(sep).append(clazz.getName());
        }
        return builder.substring(2);
    }
 
    public static String toString(InputStream in) throws IOException {
        StringBuffer out = new StringBuffer();
        byte[] b = new byte[4096];
        for (int n; (n = in.read(b)) != -1;) {
            out.append(new String(b, 0, n));
        }
        return out.toString();
    }
 
    public static String capfirstChar(String msg) {
        char[] msgs = msg.toCharArray();
        msgs[0] = (char) (msgs[0] - 32);
        return String.valueOf(msgs);
    }
    
    /**
     * 截取字符串
     * @param str
     * @param length
     * @return
     */
    public static String getStr(String str,int length)
    {
        if(isEmpty(str))
        {
            return "";
        }
        else
        {
            if(str.length()<=length)
            {
                return str;
            }
            else
            {
                return str.substring(0,length);
            }
        }
            
    }
    
    /**
     * 截取字符串
     * @param str
     * @param length
     * @return
     */
    public static String getStrMore(String str,int length,String more)
    {
        if(isEmpty(str))
        {
            return "";
        }
        else
        {
            if(str.length()<=length)
            {
                return str;
            }
            else
            {
                return str.substring(0,length)+more;
            }
        }
            
    }
 
 
    
   /**
    * 校验搜索关键字是否含有非法字符
    * @param s
    * @return true 含有非法字符  false 没有含有非法字符
    */
 
    public static boolean isSearch(String s){
        //用来关键字搜索。匹配由数字、26个英文字母或者下划线组成的字符串 
        
 
        if(!Strings.isEmpty(s)){
            //return RegexUtil.match(s.trim(),"^[\\u4E00-\\u9FA5\\uF900-\\uFA2D\\w]{1,6}$");
            //String all  = "^[\\u4E00-\\u9FA5\\uF900-\\uFA2D\\w]{1,6}$";   
            //Pattern pattern = Pattern.compile(all);   
            //return pattern.matcher(s).matches();   
 
        }
        return true;
    }
    
    
      /*
       * 按字节长度截取字符串
       * @param str 将要截取的字符串参数
       * @param toCount 截取的字节长度
       * @return 返回截取后的字符串
       * @日期 2008-01-25 cwj
       */
      public static String substring(String str, int size){
          if(Strings.isEmpty(str))return "";
          int n=0;
          int k=0;
          String t="";;
          for(int i=0; i<str.length(); i++) {
              if(k<size){
                    t=t+String.valueOf(str.charAt(i));
                    n = (int)str.charAt(i);
                    if((19968 <= n && n <40623)) {//是中文
                        
                        k=k+2;
                    }else{
                        k=k+1;
                    }
              }
        }
        return t.length()<str.length() ? t+".." : t;
 
 
    
          } 
    
      
        /**
          * 用来判断是否为数字
          * @param   str   String   
          * @return  true 匹配,false 不匹配
          */
      public static boolean  verifeNum(String str){
             try{ 
                 Double.valueOf(str);
             }catch(NumberFormatException nb){
                 return false;
                 
             }
             return true;
      }
      /**
          * 用来判断是否为数字
          * @param   str   String   
          * @return  true 匹配,false 不匹配
          */
   public static boolean  verifeLong(String str){
          try{ 
              Long.valueOf(str);
          }catch(NumberFormatException nb){
              return false;
              
          }
          return true;
   }
        /**
          * 用来判断是否为Float数
          * @param   str   String   
          * @return  true 匹配,false 不匹配
          */
   public static boolean  verifeFloat(String str){
          try{ 
              Float.valueOf(str);
          }catch(NumberFormatException nb){
              return false;
              
          }
          return true;
   }
      /**
       * 随机数
       * @param num
       * @return
       */
      public  static String getRandomNum(String beginNum,int count){
        
              for(int i=0;i<count;i++){
                  int j = (int)(java.lang.Math.random()*10);
                  beginNum+=String.valueOf(j);
              }
              return beginNum;
    } 
      
 
      //保留两位小数点
    public static float roundHalfUp(float f){
    
        BigDecimal   b   =   new   BigDecimal(f);  
        return b.setScale(4,   BigDecimal.ROUND_HALF_UP).floatValue();  
 
    }
      //保留两位小数点
    public static double roundHalfUp(double f){
    
        BigDecimal   b   =   new   BigDecimal(f);  
        return b.setScale(2,   BigDecimal.ROUND_HALF_UP).doubleValue();  
 
    }
    /**   
      * 用来判断邮箱是否合法   
      * @param   email   String   
      * @return   boolean   
      */   
     public static boolean verifyEmail(String   email){   
         Pattern pattern = 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})(\\]?)$");
 
         Matcher matcher = pattern.matcher(email);
 
         return matcher.matches();   
      }
     
     /**   
      * 帐号名称必须为3-30位的英文、数字、下划线或小数点
      * @param   email   String   
      * @return   boolean    true 合法,false 不合法
      */   
     public static boolean verifyUserName(String userName){
          String  patten = "^[a-z0-9_.]{3,30}$";   
          if(userName.length()!=0)   
              if(!userName.matches(patten)){   
                return   false;   
            }   
           return true;   
      }
     
//     //过滤关键字
//     public static String htmlEncoding(String input){
//         if(!Strings.isEmpty(input)){
//             input = HtmlUtils.htmlEscapeDecimal(input);
//             input = StringEscapeUtils.escapeSql(input);
//             return input;
//         }
//         return "";
//     }
//     
     public static String split(String value){
        if(value==null){
            return null;
        }
        StringBuffer result=new StringBuffer(value.length());
        for(int i=0;i<value.length();++i){
            switch(value.charAt(i)){
            case '|':
                result.append("");
                break;
            case '&':
                result.append("");
                break;
            case ';':
                result.append("");
                break;
            case '$':
                result.append("");
                break;
            case '%':
                result.append("");
                break;
            case '\'':
                result.append("");
                break;
            case '"':
                result.append("");
                break;
            case '\\':
                result.append("");
                break;
            case '<':
                result.append("");
                break;
            case '>':
                result.append("");
                break;
            case '+':
                result.append("");
                break;
            case '\r':
                result.append("");
                break;
            case '\n':
                result.append("");
                break;
            default:
                result.append(value.charAt(i));
                break;
            }
            
        }
        return result.toString();
 
 
        
    }
    public static Integer[] convertionToInt(String[] strs){
        // 将String数组转换为Long类型数组 
        Integer[] ints = new Integer[strs.length]; 
        //声明long类型的数组 
        for(int i = 0;i<strs.length;i++){   
            String str = strs[i];       
            //将strs字符串数组中的第i个值赋值给str   
            int theint = Integer.valueOf(str);
            //将str转换为long类型,并赋值给thelong   
            ints[i] = theint;//将thelong赋值给 longs数组中对应的地方  }
        }
        return ints;
    }
     
    public static String fullZero(String str){
        if(str.length()==1){
            return "00"+str;
        }else if(str.length()==2){
            return "0"+str;
        }
        return str;
        
    }
    public static void main(String[] args) {
        
        System.out.print((int)(1.2/0.5));
        
//        for(int i=0;i<20;i++)
//            //System.out.println(RandomUtil.randomInt(0,1));
        
        //System.out.println(Strings.verifyEmail("a1123"));
        
        // TODO Auto-generated method stub
//        //System.out.println(HtmlUtils.htmlEscape(""));
//        //System.out.println(HtmlUtils.htmlEscape(HtmlUtils.htmlEscape("<asd>")));
//
//        
//        //System.out.println(JavaScriptUtils.javaScriptEscape("753+Main+Street%27%22%3E%3Ciframe+src%3Dhttp%3A%2F%2Fdemo.testfire.net%3E"));
//        //System.out.println(StringEscapeUtils.escapeHtml("753+Main+Street%27%22%3E%3Ciframe+src%3Dhttp%3A%2F%2Fdemo.testfire.net%3E"));
    }
    
    /**
     * 正则表达式:验证数字
     */
    public static final String REGEX_NUMBER = "^-?\\d+$";
    /**
     * 正则表达式:验证用户名
     */
    public static final String REGEX_USERNAME = "^[a-zA-Z]\\w{5,17}$";
 
    /**
     * 正则表达式:验证密码
     */
    public static final String REGEX_PASSWORD = "^[a-zA-Z0-9]{6,16}$";
 
    /**
     * 正则表达式:验证手机号
     */
    public static final String REGEX_MOBILE = "^((13[0-9])|(15[^4,\\D])|(14[0-9])|(18[0-9])|(17[0-9]))\\d{8}$";
 
    public static final String REGEX_TEL_PHONE = "^((13[3])|(15[3])|(18[0])|(18[1])|(18[9])|(17[7]))\\d{8}$";
    public static final String REGEX_UNICOM_PHONE = "^((13[0])|(13[1])|(13[2])|(15[5])|(15[6])|(18[5])|(18[6])|(14[5])|(17[7]))\\d{8}$";
    public static final String REGEX_MOBILE_PHONE = "^((13[4])|(13[5])|(13[6])|(13[7])|(13[8])|(13[9])|(14[7])|(15[0])|(15[1])|(15[2])|(15[7])|(15[8])|(15[9])|(17[8])|(18[2])|(18[3])|(18[4])|(18[7])|(18[8]))\\d{8}$";
 
 
    /**
     * 正则表达式:验证邮箱
     */
    public static final String REGEX_EMAIL = "^([a-z0-9A-Z]+[-|\\.]?)+[a-z0-9A-Z]@([a-z0-9A-Z]+(-[a-z0-9A-Z]+)?\\.)+[a-zA-Z]{2,}$";
 
    /**
     * 正则表达式:验证汉字
     */
    public static final String REGEX_CHINESE = "^[\u4e00-\u9fa5],{0,}$";
 
    /**
     * 正则表达式:验证身份证
     */
    public static final String REGEX_ID_CARD = "(^\\d{18}$)|(^\\d{15}$)";
 
    /**
     * 正则表达式:验证URL
     */
    public static final String REGEX_URL = "http(s)?://([\\w-]+\\.)+[\\w-]+(/[\\w- ./?%&=]*)?";
 
    /**
     * 正则表达式:验证IP地址
     */
    public static final String REGEX_IP_ADDR = "(25[0-5]|2[0-4]\\d|[0-1]\\d{2}|[1-9]?\\d)";
 
    /**
     * 校验数字
     * 
     * @param username
     * @return 校验通过返回true,否则返回false
     */
    public static boolean isNumber(String number) {
        if(Strings.isEmpty(number))return true;
        return Pattern.matches(REGEX_NUMBER, number);
    }
    /**
     * 校验用户名
     * 
     * @param username
     * @return 校验通过返回true,否则返回false
     */
    public static boolean isUsername(String username) {
        return Pattern.matches(REGEX_USERNAME, username);
    }
 
    /**
     * 校验密码
     * 
     * @param password
     * @return 校验通过返回true,否则返回false
     */
    public static boolean isPassword(String password) {
        return Pattern.matches(REGEX_PASSWORD, password);
    }
 
    /**
     * 校验手机号
     * 
     * @param mobile
     * @return 校验通过返回true,否则返回false
     */
    public static boolean isMobile(String mobile) {
        return Pattern.matches(REGEX_MOBILE, mobile);
    }
    /**
     * 校验是否符合号码格式
     * 
     * @param mobile
     * @return 校验通过返回true,否则返回false
     */
    public static boolean isMobileForm(String mobile) {
        return mobile.startsWith("1")&&mobile.length()==11&&isNumber(mobile);
    }
    /**
     * 校验电信手机号码
     * 
     * @param mobile
     * @return 校验通过返回true,否则返回false
     */
    public static boolean isTelPhone(String phone) {
        return Pattern.matches(REGEX_TEL_PHONE, phone);
    }
    /**
     * 校验联通手机号码
     * 
     * @param mobile
     * @return 校验通过返回true,否则返回false
     */
    public static boolean isUnicomPhone(String phone) {
        return Pattern.matches(REGEX_UNICOM_PHONE, phone);
    }
    /**
     * 校验移动手机号码
     * 
     * @param mobile
     * @return 校验通过返回true,否则返回false
     */
    public static boolean isMobilePhone(String phone) {
        return Pattern.matches(REGEX_MOBILE_PHONE, phone);
    }
    /**
     * 校验邮箱
     * 
     * @param email
     * @return 校验通过返回true,否则返回false
     */
    public static boolean isEmail(String email) {
        return Pattern.matches(REGEX_EMAIL, email);
    }
 
    /**
     * 校验汉字
     * 
     * @param chinese
     * @return 校验通过返回true,否则返回false
     */
    public static boolean isChinese(String chinese) {
        return Pattern.matches(REGEX_CHINESE, chinese);
    }
 
    /**
     * 校验身份证
     * 
     * @param idCard
     * @return 校验通过返回true,否则返回false
     */
    public static boolean isIDCard(String idCard) {
        return Pattern.matches(REGEX_ID_CARD, idCard);
    }
 
    /**
     * 校验URL
     * 
     * @param url
     * @return 校验通过返回true,否则返回false
     */
    public static boolean isUrl(String url) {
        return Pattern.matches(REGEX_URL, url);
    }
 
    /**
     * 校验IP地址
     * 
     * @param ipAddr
     * @return
     */
    public static boolean isIPAddr(String ipAddr) {
        return Pattern.matches(REGEX_IP_ADDR, ipAddr);
    }
    
    /**
     * 验证时间格式
     * true 正确 flase 错误
     */
    public static boolean isDate(String dateStr,String pattern){
         try{
            
            SimpleDateFormat sdf = new SimpleDateFormat ( pattern ) ;
            sdf.parse ( dateStr ) ;
            return true;
        } catch (ParseException e) {
            // TODO Auto-generated catch block
            return false;
            
        }
    }
 
    //根据文件的后缀来显示对应的图标
    public static String getFileIcon(String filePath){
        if(filePath.indexOf("doc")!=-1){
            return "fa-file-word-o";
        }else if(filePath.indexOf("xls")!=-1){
            return "fa-file-excel-o";
        }else if(filePath.indexOf("pdf")!=-1){
            return "fa-file-pdf-o";
        }else if(filePath.indexOf("ppt")!=-1){
            return "fa-file-powerpoint-o";
        }else if(filePath.indexOf("txt")!=-1){
            return "fa-file-text-o";
        }
        return "";
    }
}