工具类(自定义常用工具类)

 1 import java.util.Random;
 2 import java.util.UUID;
 3 
 4 public class DemoClass4Util {
 5     public static void main(String[] args) {
 6         String str24 = null;
 7         String str25 = "";
 8         String str26 = "  ";
 9         String str27 = "Frank";
10 
11         System.out.println(StringUtil.isEmpty(str24)); //true
12         System.out.println(StringUtil.isEmpty(str25)); //true
13         System.out.println(StringUtil.isEmpty(str26)); //true
14         System.out.println(StringUtil.isEmpty(str27)); //false
15 
16         System.out.println("--------------");
17 
18         System.out.println(StringUtil.StringRandom());
19         System.out.println(StringUtil.StringRandom("1234dkuewifjsk&*&^&", 50));
20     }
21 }
22 
23 //字符串工具类
24 class StringUtil{
25     //判断是否为空
26     public static boolean isEmpty(String str){
27        /* if(str == null)
28             return true;
29         if("".equals(str))
30             return true;
31         if("".equals(str.trim()))
32             return true;*/
33 
34         //整理代码
35         if(str == null || "".equals(str.trim())){
36             return true;
37         }
38         return false;
39     }
40 
41     //生成随机字符串
42     public static String StringRandom(){
43         return UUID.randomUUID().toString().replace("-", "");
44     }
45 
46     public static  String StringRandom(String fromStr, int len){
47         if (len < 1) {
48             return "";
49         }else {
50             char[] charArray = fromStr.toCharArray();
51             StringBuilder sb = new StringBuilder();
52             for (int i = 0; i < len; i++) {
53                 Random random = new Random();
54                 //nextInt 随机获取下标
55                 int index = random.nextInt(charArray.length);
56                 char c = charArray[index];
57                 sb.append(c);
58             }
59             return sb.toString();
60         }
61     }
62 }

 

posted @ 2025-10-15 16:23  字节虫  阅读(11)  评论(0)    收藏  举报