• Java 字符串工具类


    package com.lihaozhe.util.string;
    
    import java.lang.reflect.Field;
    import java.util.UUID;
    import java.util.concurrent.ThreadLocalRandom;
    
    /**
     * 字符串工具类
     *
     * @author 李昊哲
     * @version 1.0
     * @create 2023/10/17
     */
    public class StringUtils {
        /**
         * 去除字符串左边的的空格
         *
         * @param string 原始字符串
         * @return 去除左边空格后的字符串
         */
        public static String ltrim(String string) {
            if (string == null) {
                throw new NullPointerException();
            } else {
                return string.replaceAll("^\\s+", "");
            }
        }
    
        /**
         * 去除字符串右边的的空格
         *
         * @param string 原始字符串
         * @return 去除右边空格后的字符串
         */
        public static String rtrim(String string) {
            if (string == null) {
                throw new NullPointerException();
            } else {
                return string.replaceAll("\\s+$", "");
            }
        }
    
        /**
         * 生成的UUID是带-的字符串,类似于:a5c8a5e8-df2b-4706-bea4-08d0939410e3
         *
         * @return 带-的字符串
         */
        public static String randomUUID() {
            return UUID.randomUUID().toString();
        }
    
        /**
         * 生成的是不带-的字符串,类似于:b17f24ff026d40949c85a24f4f375d42
         *
         * @return 不带-的字符串
         */
        public static String simpleUUID() {
            return randomUUID().replace("-", "");
        }
    
        /**
         * 随机字符串
         *
         * @param count 随机字符串长度
         * @return 随机字符串
         */
        public static String random(int count) {
            String codePool = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
            StringBuilder sb = new StringBuilder();
            for (int i = 0; i < count; i++) {
                sb.append(codePool.charAt(ThreadLocalRandom.current().nextInt(codePool.length())));
            }
            return sb.toString();
        }
    
        /**
         * 随机数字
         *
         * @param count 随机数字长度
         * @return 随机数字
         */
        public static String randomNumeric(int count) {
            String codePool = "0123456789";
            StringBuilder sb = new StringBuilder();
            for (int i = 0; i < count; i++) {
                sb.append(codePool.charAt(ThreadLocalRandom.current().nextInt(codePool.length())));
            }
            return sb.toString();
        }
    
        /**
         * 判断字符串是否为空
         *
         * @param string 判断参数 string 是否为空
         * @return 参数为 null 或者 参数的长度为零 返回 true 其余情况返回 false
         */
        public static boolean isEmpty(String string) {
            if (string == null || string.length() == 0) {
                return true;
            }
            return false;
        }
    
        /**
         * 判断字符串是否为空
         *
         * @param string 判断参数 string 是否为空
         * @return 参数为 null 或者 参数收尾两端去掉空格后的长度为零 返回 true 其余情况返回 false
         */
        public static boolean isBlank(String string) {
            if (string == null || string.trim().length() == 0) {
                return true;
            }
            return false;
        }
    
        /**
         * 获取文件后缀名
         *
         * @param fileName 原始文件名
         * @return 文件后缀名
         */
        public static String getExtension(String fileName) {
            int index = fileName.lastIndexOf(".");
            if (index == -1) {
                return "";
            }
            return fileName.substring(index + 1);
        }
    
        /**
         * 对象转 csv文件格式字符串
         *
         * @param obj 被转换的对象
         * @return csv 文件格式字符串
         * @throws IllegalAccessException IllegalAccessException
         */
        public static String format2CSV(Object obj) throws IllegalAccessException {
            if (obj == null) {
                return null;
            }
            // 利用反射获取该对象的 类对象
            Class<?> aClass = obj.getClass();
            // 获取该类对象的是所有属性
            Field[] fields = aClass.getDeclaredFields();
            // 保存最终结果
            StringBuilder buffer = new StringBuilder();
            // 遍历属性
            for (Field field : fields) {
                // 设置私有属性可以访问权限
                field.setAccessible(true);
                // 将属性值全部转为字符串
                String string = String.valueOf(field.get(obj));
                // 将读取到的字符串内容 按照 csv 文件格式封装到 StringBuilder 对象 中
                buffer.append(string).append(",");
            }
            // 返回 最终结果 返回前 独钓字符串中最后一个逗号
            return buffer.substring(0, buffer.length() - 1);
        }
    }
    
    
    • 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
  • 相关阅读:
    Go 初识微信订阅号(测试号)
    基于单片机的多功能电子万年历系统
    WeNet更新:喜马拉雅团队在 WeNet 中支持 Squeezeformer
    力扣刷题目录
    2024/4/3 IOday5
    实例解析Java反射
    基于JAVA校园表白墙服务平台计算机毕业设计源码+系统+mysql数据库+lw文档+部署
    openfeign整合sentinel出现异常
    Spring Boot 注解详解:一步一步了解Spring Boot的核心注解
    广西螺蛳粉:“超级农货”养成记
  • 原文地址:https://blog.csdn.net/qq_24330181/article/details/133888878