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拼接字符, 拼接字符]
相关推荐
꧁Q༒ོγ꧂5 分钟前
算法详解(二)--算法思想基础
java·数据结构·算法
次元工程师!7 分钟前
Sa-Token完成路由鉴权
java·服务器·前端
华如锦13 分钟前
一.2部署——大模型服务快速部署vLLM GPU 安装教程 (Linux)
java·linux·运维·人工智能·后端·python·vllm
小鸡脚来咯18 分钟前
设计模式,单例和工厂模式
java
Qiuner23 分钟前
Spring Boot 全局异常处理策略设计(三):@ExceptionHandler 与 @ControllerAdvice 生效原理源码解析
java·spring boot·后端
零度@25 分钟前
Java 消息中间件 - RabbitMQ 全解(保姆级 2026)
java·rabbitmq·java-rabbitmq
u01040583625 分钟前
企业微信自建应用权限模型与 RBAC 在 Spring Security 中的映射
java·spring·企业微信
墨雨晨曦8826 分钟前
通过调用deepseek的api来实现智能客服
java
予枫的编程笔记28 分钟前
Elasticsearch核心架构与基础原理:解密其极速性能的底层逻辑
java·大数据·人工智能·elasticsearch·搜索引擎·架构·全文检索