StringUtils 字符串工具

一、工具类代码展示

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

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

/**
 * 字符串工具类
 *
 * @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);
    }

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

二、测试代码

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

 具体测试详见注释:

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 + "拼接字符";
           }
       }));



   }
}

返回结果:

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拼接字符, 拼接字符]

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

syfjava

请博主喝杯蜜雪冰城

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值