Thymeleaf主要使用 org.thymeleaf.expression.Strings 类处理字符串,在模板中使用 #strings 对象来处理字符串。

开发环境:IntelliJ IDEA 2019.2.2
Spring Boot版本:2.1.8

新建一个名称为demo的Spring Boot项目。

1、pom.xml
加入Thymeleaf依赖

        <dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>

2、src/main/resources/application.yml
设置模板缓存为false,这样修改html页面后刷新浏览器能马上看到结果

spring:
thymeleaf:
cache: false

3、src/main/java/com/example/demo/TestController.java

package com.example.demo;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping; @Controller
public class TestController {
@RequestMapping("/")
public String test(){
return "test";
}
}

4、src/main/resources/templates/test.html

调用参数的toString方法返回字符串
<div th:text="${#strings.toString('hello')}"></div>
返回字符串的长度
<div th:text="${#strings.length('hello')}"></div>
判断是否为空或null
<div th:text="${#strings.isEmpty('hello')}"></div>
<div th:text="${#strings.isEmpty('')}"></div>
<div th:text="${#strings.isEmpty(null)}"></div>
为空或null时设置默认值
<div th:text="${#strings.defaultString('hello','a')}"></div>
<div th:text="${#strings.defaultString('','b')}"></div>
<div th:text="${#strings.defaultString(null,'c')}"></div>
判断是否包含(区分大小写)
<div th:text="${#strings.contains('hello','he')}"></div>
<div th:text="${#strings.contains('hello','HE')}"></div>
判断是否包含(忽略大小写)
<div th:text="${#strings.containsIgnoreCase('hello','he')}"></div>
<div th:text="${#strings.containsIgnoreCase('hello','HE')}"></div>
判断开头和结尾是否包含(区分大小写)
<div th:text="${#strings.startsWith('hello','he')}"></div>
<div th:text="${#strings.startsWith('hello','HE')}"></div>
<div th:text="${#strings.startsWith('hello','el')}"></div>
<div th:text="${#strings.endsWith('hello','lo')}"></div>
获取字符串的索引(如果不存在返回-1)
<div th:text="${#strings.indexOf('hello','el')}"></div>
<div th:text="${#strings.indexOf('hello','ee')}"></div>
指定开始和结束索引,截取字符串(如果索引超过字符串长度,则抛出异常)
<div th:text="${#strings.substring('hello',1,3)}"></div>
指定从某个字符串后面截取字符串(如果不包含则返回空字符串)
<div th:text="${#strings.substringAfter('hello','e')}"></div>
<div th:text="${#strings.substringAfter('hello','ee')}"></div>
指定从某个字符串前面截取字符串(如果不包含则返回空字符串)
<div th:text="${#strings.substringBefore('hello','e')}"></div>
<div th:text="${#strings.substringBefore('hello','ee')}"></div>
替换字符串
<div th:text="${#strings.replace('hello','e','a')}"></div>
转换为大写
<div th:text="${#strings.toUpperCase('hello')}"></div>
转换为小写
<div th:text="${#strings.toLowerCase('HELLO')}"></div>
首字母转换为大写
<div th:text="${#strings.capitalize('hello')}"></div>
首字母转换为小写
<div th:text="${#strings.unCapitalize('heLLo')}"></div>
每个单词的首字母转为大写
<div th:text="${#strings.capitalizeWords('hello world')}"></div>
根据分隔符将每个单词的首字母转换为大写
<div th:text="${#strings.capitalizeWords('hello-world','-')}"></div>
字符串前面追加
<div th:text="${#strings.prepend('world','hello ')}"></div>
字符串后面追加
<div th:text="${#strings.append('hello',' world')}"></div>
拼接字符串(参数个数不限)
<div th:text="${#strings.concat('hello',' world',' !')}"></div>
从第二个参数之后拼接字符串,如果参数为null,则用第一个参数替代
<div th:text="${#strings.concatReplaceNulls('*','hello',null,'world')}"></div>
删除空白
<div th:text="${#strings.trim(' hello ')}"></div>
字符串截取指定长度(最小为3),后面加...
<div th:text="${#strings.abbreviate('hello,world', 8)}"></div>
产生指定位数的随机字母数字,范围为大写英文字母加0-9数字
<div th:text="${#strings.randomAlphanumeric(4)}"></div>
调用HtmlEscape类的escapeHtml4Xml方法对参数进行编码
<div th:text="${#strings.escapeXml('<span>hello</span>')}"></div>

浏览器访问:http://localhost:8080
页面输出:

调用参数的toString方法返回字符串
hello
返回字符串的长度
5
判断是否为空或null
false
true
true
为空或null时设置默认值
hello
b
c
判断是否包含(区分大小写)
true
false
判断是否包含(忽略大小写)
true
true
判断开头和结尾是否包含(区分大小写)
true
false
false
true
获取字符串的索引(如果不存在返回-1)
1
-1
指定开始和结束索引,截取字符串(如果索引超过字符串长度,则抛出异常)
el
指定从某个字符串后面截取字符串(如果不包含则返回空字符串)
llo
指定从某个字符串前面截取字符串(如果不包含则返回空字符串)
h
替换字符串
hallo
转换为大写
HELLO
转换为小写
hello
首字母转换为大写
Hello
首字母转换为小写
heLLo
每个单词的首字母转为大写
Hello World
根据分隔符将每个单词的首字母转换为大写
Hello-World
字符串前面追加
hello world
字符串后面追加
hello world
拼接字符串(参数个数不限)
hello world !
从第二个参数之后拼接字符串,如果参数为null,则用第一个参数替代
hello*world
删除空白
hello
字符串截取指定长度(最小为3),后面加...
hello...
产生指定位数的随机字母数字,范围为大写英文字母加0-9数字
PBAT
调用HtmlEscape类的escapeHtml4Xml方法对参数进行编码
&lt;span&gt;hello&lt;/span&gt;

最新文章

  1. 解决Android界面布局添加EditText组件后界面无法预览
  2. Sublime+Golang Plugin
  3. Find the Duplicate Number
  4. 附带详细注释的log4net的app.config文件配置例子
  5. Linux Network IO Model、Socket IO Model - select、poll、epoll
  6. Selenium2学习-023-WebUI自动化实战实例-021-获取浏览器显示区域大小,通过 WebDriver 截图功能
  7. Android JNI学习之javah命令的正确使用(找了好半天才找到的,汉,网上好多说法都没用)
  8. js jsp 时间 日期 控件 插件 简单 实用
  9. hdu5355 Cake(构造)
  10. Java基础(含思维导图)
  11. Android进阶(十二)Fragment VS Activity
  12. 面试之路(27)-链表中倒数第K个结点
  13. AngularJs的基本使用(一)
  14. grid++报表使用时注意事项
  15. N阶楼梯上楼问题
  16. sql server 转置 和实现随机分配和一串代码的含义拼在一行
  17. mysql命令(三)
  18. java web 工程更改名字
  19. springMVC源码学习地址
  20. 详解 Tomcat 的连接数与线程池(转)

热门文章

  1. JQuery 表单textarea控制字数
  2. 使用Git出现以下错误&quot;Git@github.com: Permission denied (publickey). Could not read from remote repository.&quot;解决方案
  3. Openshift 自建DDNS动态域名
  4. Prometheus学习系列(四)之Prometheus 配置说明
  5. node.js安装本地模块遇到的目录锁定问题【新手问题】
  6. jieba的使用
  7. 如何正确使用 Spring Cloud?【下】
  8. [转]smtplib.SMTPDataError: (554, b&#39;DT:SPM的异常
  9. CODING 祝大家中秋快乐!
  10. MATLAB聚类有效性评价指标(外部 成对度量)