StringUtils 字符串工具

一、工具类代码展示

继承了org.apache.commons.lang3.StringUtils 的类。

工具类已经上传附件自行下载:

java 复制代码
/**
 * 字符串工具类
 *
 * @author Lion Li
 */
@NoArgsConstructor(access = AccessLevel.PRIVATE)
public class StringUtils extends org.apache.commons.lang3.StringUtils {

    public static final String SEPARATOR = ",";

    /**
     * 获取参数不为空值
     *
     * @param str defaultValue 要判断的value
     * @return value 返回值
     */
    public static String blankToDefault(String str, String defaultValue) {
        return StrUtil.blankToDefault(str, defaultValue);
    }

    /**
     * * 判断一个字符串是否为空串
     *
     * @param str String
     * @return true:为空 false:非空
     */
    public static boolean isEmpty(String str) {
        return StrUtil.isEmpty(str);
    }

    /**
     * * 判断一个字符串是否为非空串
     *
     * @param str String
     * @return true:非空串 false:空串
     */
    public static boolean isNotEmpty(String str) {
        return !isEmpty(str);
    }

    /**
     * 去空格
     */
    public static String trim(String str) {
        return StrUtil.trim(str);
    }

    /**
     * 截取字符串
     *
     * @param str   字符串
     * @param start 开始
     * @return 结果
     */
    public static String substring(final String str, int start) {
        return substring(str, start, str.length());
    }

    /**
     * 截取字符串
     *
     * @param str   字符串
     * @param start 开始
     * @param end   结束
     * @return 结果
     */
    public static String substring(final String str, int start, int end) {
        return StrUtil.sub(str, start, end);
    }

    /**
     * 格式化文本, {} 表示占位符<br>
     * 此方法只是简单将占位符 {} 按照顺序替换为参数<br>
     * 如果想输出 {} 使用 \\转义 { 即可,如果想输出 {} 之前的 \ 使用双转义符 \\\\ 即可<br>
     * 例:<br>
     * 通常使用:format("this is {} for {}", "a", "b") -> this is a for b<br>
     * 转义{}: format("this is \\{} for {}", "a", "b") -> this is {} for a<br>
     * 转义\: format("this is \\\\{} for {}", "a", "b") -> this is \a for b<br>
     *
     * @param template 文本模板,被替换的部分用 {} 表示
     * @param params   参数值
     * @return 格式化后的文本
     */
    public static String format(String template, Object... params) {
        return StrUtil.format(template, params);
    }

   ........省略代码

二、测试代码

  • 判断空
  • 截取字符
  • 格式化
  • 转换大小写
  • 是否匹配字符
  • 拼接字符

具体测试详见注释:

java 复制代码
package com.ruoyi.demo;

import cn.hutool.core.lang.Console;
import com.ruoyi.common.utils.StringUtils;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.function.Function;

/**
 * <简述>
 * <详细描述>
 *
 * @author syf
 * @date 2024年08月14日 10:26
 */
public class TestStringUtils {
    public static void main(String[] args) {
        test();

    }

   static void test(){
        // 判断是否为空
       String hello = StringUtils.blankToDefault("1", "hello" );
       Console.log("blankToDefault->{}", hello);
       String hell1o = StringUtils.blankToDefault(null, "hello" );
       Console.log("blankToDefault->{}", hell1o);
       // 判断是否为空
       Console.log("isNotEmpty->{}", StringUtils.isEmpty("1"));
       Console.log("isNotEmpty->{}", StringUtils.isEmpty(""));
       Console.log("isNotEmpty->{}", StringUtils.isEmpty(null));
       //去除前后空格
       Console.log("trim->{}", StringUtils.trim("  hello  "));

       //截取字符串 前闭后开规则
       Console.log("substring->{}", StringUtils.substring("hello", 1, 2));

       //格式化文本
       Console.log("format->{}", StringUtils.format("hello {}", "world"));
       Console.log("format->{}", StringUtils.format("hello \\{}", "world"));
       //是否http
       Console.log("ishttp->{}", StringUtils.ishttp("http://www.baidu.com"));


       //转驼峰
       Console.log("toCamelCase->{}", StringUtils.toCamelCase("hello_world"));
       //转下划线
       Console.log("toUnderScoreCase->{}", StringUtils.toUnderScoreCase("helloWorld"));


       //分割 返回数组
       Console.log("splitList->{}", StringUtils.splitList("1,2,3"));
       //转换数组
       Console.log("str2List->{}", StringUtils.str2List("1,2,3", ",", true, true));
       //转换集合
       Console.log("str2Set->{}", StringUtils.str2Set("1,2,3", ","));

       //匹配字符
       Console.log("isMatch->{}", StringUtils.isMatch("query/**", "query/allList"));
       //匹配
       Console.log("matches->{}", StringUtils.matches("hello", Arrays.asList("hello", "world")));
       //是否包含 第一个参数是否在第二个参数里面
       Console.log("splitTo->{}", StringUtils.inStringIgnoreCase("hello", "hello", "world"));
       //是否包含
       Console.log("containsAnyIgnoreCase->{}", StringUtils.containsAnyIgnoreCase("hello", "hello", "world"));

       //填充  左边填充
       Console.log("padl->{}", StringUtils.padl(1, 2));
       //填充  左边填充 自定义字符
       Console.log("padl->{}", StringUtils.padl("1", 2, '*'));

       //分割  返回集合
       Console.log("padl->{}", StringUtils.splitTo("1,2,3,", ",", new Function<Object, Object>() {
           @Override
           public Object apply(Object o) {
               return o + "拼接字符";
           }
       }));



   }
}

返回结果:

java 复制代码
blankToDefault->1
blankToDefault->hello
isNotEmpty->false
isNotEmpty->true
isNotEmpty->true
trim->hello
substring->e
format->hello world
format->hello {}
ishttp->true
toCamelCase->helloWorld
toUnderScoreCase->hello_world
splitList->[1, 2, 3]
str2List->[1, 2, 3]
str2Set->[1, 2, 3]
isMatch->true
matches->true
splitTo->true
containsAnyIgnoreCase->true
padl->01
padl->*1
padl->[1拼接字符, 2拼接字符, 3拼接字符, 拼接字符]
相关推荐
JH30731 小时前
SpringBoot 优雅处理金额格式化:拦截器+自定义注解方案
java·spring boot·spring
Coder_Boy_2 小时前
技术让开发更轻松的底层矛盾
java·大数据·数据库·人工智能·深度学习
invicinble2 小时前
对tomcat的提供的功能与底层拓扑结构与实现机制的理解
java·tomcat
较真的菜鸟3 小时前
使用ASM和agent监控属性变化
java
黎雁·泠崖3 小时前
【魔法森林冒险】5/14 Allen类(三):任务进度与状态管理
java·开发语言
qq_12498707534 小时前
基于SSM的动物保护系统的设计与实现(源码+论文+部署+安装)
java·数据库·spring boot·毕业设计·ssm·计算机毕业设计
Coder_Boy_4 小时前
基于SpringAI的在线考试系统-考试系统开发流程案例
java·数据库·人工智能·spring boot·后端
Mr_sun.4 小时前
Day06——权限认证-项目集成
java
瑶山4 小时前
Spring Cloud微服务搭建四、集成RocketMQ消息队列
java·spring cloud·微服务·rocketmq·dashboard
abluckyboy4 小时前
Java 实现求 n 的 n^n 次方的最后一位数字
java·python·算法