编程随笔 编程随笔
  • 前端
  • 后端
  • 星球项目
  • 开源项目
  • 海康AGV
  • 四向车
  • 工具类
  • 项目仓库

    • 部署仓库 (opens new window)
    • 代码仓库 (opens new window)
  • vuepress插件

    • 自动生成导航栏与侧边栏 (opens new window)
    • 评论系统 (opens new window)
    • 全文搜索 (opens new window)
    • 选项卡 (opens new window)
    • 自动生成sitemap (opens new window)
  • 自主开发插件

    • 批量操作frontmatter (opens new window)
    • 链接美化 (opens new window)
    • 折叠代码块 (opens new window)
    • 复制代码块 (opens new window)

liyao52033

走运时,要想到倒霉,不要得意得过了头;倒霉时,要想到走运,不必垂头丧气。心态始终保持平衡,情绪始终保持稳定,此亦长寿之道
  • 前端
  • 后端
  • 星球项目
  • 开源项目
  • 海康AGV
  • 四向车
  • 工具类
  • 项目仓库

    • 部署仓库 (opens new window)
    • 代码仓库 (opens new window)
  • vuepress插件

    • 自动生成导航栏与侧边栏 (opens new window)
    • 评论系统 (opens new window)
    • 全文搜索 (opens new window)
    • 选项卡 (opens new window)
    • 自动生成sitemap (opens new window)
  • 自主开发插件

    • 批量操作frontmatter (opens new window)
    • 链接美化 (opens new window)
    • 折叠代码块 (opens new window)
    • 复制代码块 (opens new window)
  • springboot

    • MyBatis Plus使用
    • springboot2引入swagger3
    • EasyCaptcha验证码存入redis的使用
    • 常用方法
    • Elasticsearch全文搜索
    • canal同步mysql数据到es中
    • SpringSecurity使用
    • StringUtils 工具类使用
      • 概览
      • 静态常量
      • 花式截取字符串
      • 各种判断空与非空
      • 判断是否相等
      • 补齐
      • 缩短省略
      • 去掉控制字符和指定字符
    • HTTP各种参数发送
    • EasyExcel之Excel导入导出
    • EasyExcel具体使用
    • FreeMarker 模板引擎入门
    • FreeMarker生成文件及WEB使用
    • TrueLicense 创建及安装证书
  • 服务器相关

  • 腾讯云cos对象操作

  • 后端
  • springboot
华总
2023-10-03
0
0
目录

StringUtils 工具类使用原创

# 概览

大部分方法都提供了很多重载方法,比如不区分大小写xxIgnoreCase、匹配任何xxAny、还有参数类型不同、参数个数不同的重载等,能适应绝大多数业务场景的需求。

# 静态常量

StringUtils 提供了最常用的常量如下,使用定义的常量能让我们的代码更清晰。

1532358237

# 花式截取字符串

@Test
public void testSubstring() {
    // 最基本的截取
    System.out.println(  StringUtils.substring("abcde", 1, 2)  ); // b
    // 截取前半部分
    System.out.println(  StringUtils.substringBefore("role/site/admin-(1.site.1)", "-(")  ); // role/site/admin
    // 截取后半部分
    System.out.println(  StringUtils.substringAfter("role/site/admin-(1.site.1)", "-")  ); // (1.site.1)
    // 截取之间的
    System.out.println(  StringUtils.substringBetween("role/site/admin-(1.site.1)", "(", ")")  ); // 1.site.1
    // 截取左边的部分
    System.out.println(  StringUtils.left("aabbcc", 4)  ); // aabb
    // 截取右边的部分
    System.out.println(  StringUtils.right("aabbcc", 4)  ); // bbcc
    // 截取中间部分
    System.out.println(  StringUtils.mid("aabbcc", 1, 2)  ); // ab
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17

# 各种判断空与非空

@Test
public void testIsEmpty() {
    // 判断是否为空 只有为 null 或 "" 的时候才会true
    System.out.println(  StringUtils.isEmpty("  ")  );  // false
    // 判断不为空
    System.out.println(  StringUtils.isNotEmpty("  ")  ); // true
    // 判断是否为空 null、""、全空格 都为true
    System.out.println(  StringUtils.isBlank("  ")  ); // true
    // 判读不为空
    System.out.println(  StringUtils.isNotBlank("  ")  ); // false
    // 任何一个为空
    System.out.println(  StringUtils.isAnyEmpty("ab", " ")  ); // false
    // 任何一个为空
    System.out.println(  StringUtils.isAnyBlank("ab", "  ")  ); // true
    // 没有为空的
    System.out.println(  StringUtils.isNoneEmpty("ab", "")  ); // false
    // 所有的都为空
    System.out.println(  StringUtils.isAllBlank("ab", "  ")  ); // false
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19

# 判断是否相等

@Test
public void testEqual() {
    // 判断是否相等 区分大小写
    System.out.println(  StringUtils.equals("ab", "Ab")  ); // false
    // 判断是否相等 不区分大小写
    System.out.println(  StringUtils.equalsIgnoreCase("ab", "Ab")  ); // true
    // 判断其中一个相等
    System.out.println(  StringUtils.equalsAny("ab", "Ab", "ab", "abc")  ); // true
    // 判断其中一个相等
    System.out.println(  StringUtils.equalsAnyIgnoreCase("ab", "Ab", "abc")  ); // true
}
1
2
3
4
5
6
7
8
9
10
11

# 补齐

@Test
public void testPad() {
    // 左边补齐
    System.out.println(  StringUtils.leftPad("abc", 5, "x")  ); // xxabc
    // 右边补齐
    System.out.println(  StringUtils.rightPad("abc", 5, "x")  ); // abcxx
    // 两边补齐
    System.out.println(  StringUtils.center("abc", 5, "x")  ); // xabcx
}
1
2
3
4
5
6
7
8
9

# 缩短省略

@Test
public void testAbb() {
    // 共计10位数 超出省略 默认 ...
    System.out.println(  StringUtils.abbreviate("abcdefghijklmn", 10)  ); // abcdefg...
    // 指定省略符
    System.out.println(  StringUtils.abbreviate("abcdefghijklmn", "***", 10)  ); // abcdefg***
    // 两边省略
    System.out.println(  StringUtils.abbreviate("abcdefghijklmn", 5, 10)  ); // ...fghi...
}
1
2
3
4
5
6
7
8
9

# 去掉控制字符和指定字符

@Test
public void testTrim() {
    // trim 主要用于去掉控制字符 ,即ASC码表小于等于32的字符,strip主要用于去掉指定字符,默认空格

    // 去掉两边空白
    System.out.println("[" + StringUtils.trim(" ab cd ") + "]"); // [ab cd]
    // trim 去掉的是ASC码表小于等于32的字符(特殊字符)
    System.out.println("[" + StringUtils.trim(" \n ab cd ") + "]"); // [ab cd]
    // trim 去掉的是ASC码表小于等于32的字符(特殊字符)
    System.out.println("[" + StringUtils.trim(" ab \n cd ") + "]"); // [ab \n cd]
    // 如果为空则返回null
    System.out.println("[" + StringUtils.trimToNull("    ") + "]"); // [null]
    // 如果为空返回空
    System.out.println("[" + StringUtils.trimToEmpty(null) + "]"); // []

    // strip 行为和trim基本一样,可以指定去掉的字符串
    char ch = 30;
    System.out.println("[" + StringUtils.strip(ch+"  ab cd ") + "]"); // [ab cd]
    // 去掉开头的字符,null默认为空格
    System.out.println("[" + StringUtils.stripStart("  ab cd  ", null) + "]"); // [ab cd  ]
    // 去掉开头的字符,null默认为空格
    System.out.println("[" + StringUtils.stripStart("ab cd  ", "a") + "]"); // [b cd  ]
    // 去掉末尾的空格
    System.out.println("[" + StringUtils.stripEnd("  ab cd  ", null) + "]"); // [  ab cd]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#后端#springboot#StringUtils
上次更新: 2023/12/09 16:19:24
SpringSecurity使用
HTTP各种参数发送

← SpringSecurity使用 HTTP各种参数发送→

最近更新
01
element-plus多文件手动上传 原创
11-03
02
TrueLicense 创建及安装证书 原创
10-25
03
手动修改迅捷配置 原创
09-03
04
安装 acme.sh 原创
08-29
05
zabbix部署 原创
08-20
更多文章>
Copyright © 2023-2024 liyao52033
  • 跟随系统
  • 浅色模式
  • 深色模式
  • 阅读模式