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;

Thymeleaf对象的使用:字符串对象的更多相关文章

  1. Java基础97 json插件的使用(java对象和json字符串对象之间的转换)

    1.需要用到的包 2.实例 实体类 people package com.shore.entity; /** * @author DSHORE/2019-4-19 * */ public class ...

  2. json对象与json字符串对象格式

    var cStr = "{\"c\":\"{\\\"b\\\":\\\"000\\\",\\\"b2\\\&q ...

  3. Java中JNI的使用详解第四篇:C/C++中创建Java对象和String字符串对象及对字符串的操作方法

    首先来看一下C/C++中怎么创建Java对象:在JNIEnv中有两种方法是用来创建Java对象的: 第一种方法: jobject  NewObject(jclass clazz  , jmethodI ...

  4. javascript中的字符串对象和数组对象

    1.javascript的对象的概念 在javascript中,除了null和undefined以处,其他的数据类型都被定义成了对象 也可以用创建对象的方法定义变量,string,math,array ...

  5. JS 字符串对象 数组对象 函数对象 函数作用域

    一.内置对象 object对象:ECMAScript 中的所有对象都由这个对象继承而来:Object 对象中的所有属性和方法都会出现在其他对象中 ToString() : 返回对象的原始字符串表示.V ...

  6. javascript中的字符串对象

    1.javascript的对象的概念 在javascript中,除了null和undefined以处,其他的数据类型都被定义成了对象 也可以用创建对象的方法定义变量,string,math,array ...

  7. JavaScript对象、JSON对象、JSON字符串的区别

    一.首先看下什么是JSON JSON:JavaScript Object Natation,JavaScript对象的表现形式,已经发展成一种轻量级的数据交换格式. JavaScript对象的表现形式 ...

  8. jQuery中json对象与json字符串互换

    json字符串转json对象:jQuery.parseJSON(jsonStr); json对象转json字符串:JSON.stringify(jsonObj); 根据“|”把字符串变成数组.spli ...

  9. 序列化对象为xml字符串

    /// <summary>    /// 序列化对象为xml字符串    /// </summary>    /// <param name="obj" ...

随机推荐

  1. 微信小程序—支付宝小程序与微信小程序的不同点对比

    支付宝小程序语法 http://caibaojian.com/aliapp-wxapp.html https://www.cnblogs.com/bgwhite/p/9447639.html http ...

  2. 未来已来:云原生 Cloud Native

    作者:天知,原文链接 前言 自 2013 年容器(虚拟)技术(Docker)成熟后,后端的架构方式进入快速迭代的阶段,出现了很多新兴概念: 微服务 k8s Serverless IaaS:基础设施服务 ...

  3. Context知识详解

    Context知识详解 建议配合context知识架构图食用. 一.什么是Context 贴一个官方解释: Interface to global information about an appli ...

  4. 老版本nginx存在安全漏洞,不停服务热升级

    1.场景描述 安全部通知:nginx存在"整数溢出漏洞",经测试2017年4月21日之后的版本无问题,将openresty升级到最新版本,Nginx升级到1.13.2之后的版本. ...

  5. 关联mysql失败_Server returns invalid timezone. Go to 'Advanced' tab and set 'serverTimezon'

    关联mysql失败_Server returns invalid timezone. Go to ‘Advanced’ tab and set ‘serverTimezon’ 时区错误,MySQL默认 ...

  6. 文件系统之parted 分区

    parted分区命令 1.分区表区别 我们 Linux 系统中有两种常见的分区表 MBR 分区表(主引导记录分区表)和 GPT 分区表(GUID 分 区表) MBR 分区表:支持的最大分区是 2TB( ...

  7. MongoDB(三):数据库操作、集合操作

    1. 数据库操作 看完前面的文章,大家应该把环境搭建好了,下面我们就开始学习MongoDB的一些基本操作了. 首先我们要了解的一些要点: MongoDB将数据存储为一个文档,数据结构由键值对(key= ...

  8. 【JS档案揭秘】第三集 深入最底层探秘原型链

    关于这部分我看过大量的文章,数不胜数,包括阮一峰的继承三部曲,还有各种慕课的视频教程,网上无数继承方法的对比.也对很多概念存在长期错误的理解.今天做一个正确的总结,用来给原型链和继承这块知识画上句号, ...

  9. alluxio 信息索引

    最近要使用到 alluxio,发现网上还是有一些文档很是不错,现在通过这篇文章进行索引一下,进行备忘: https://edgedef.com/2017/08/17/alluxio-%E5%B0%8F ...

  10. tensor的加减乘和矩阵乘法

    对应的代码如下 import torch """如下是tensor乘法与加减法,对应位相乘或相加减,可以一对多""" def add_and ...