Thymeleaf字符串對(duì)象怎么使用

本篇內(nèi)容主要講解“Thymeleaf字符串對(duì)象怎么使用”,感興趣的朋友不妨來看看。本文介紹的方法操作簡(jiǎn)單快捷,實(shí)用性強(qiáng)。下面就讓小編來帶大家學(xué)習(xí)“Thymeleaf字符串對(duì)象怎么使用”吧!

成都創(chuàng)新互聯(lián)公司是一家集網(wǎng)站建設(shè),新邵企業(yè)網(wǎng)站建設(shè),新邵品牌網(wǎng)站建設(shè),網(wǎng)站定制,新邵網(wǎng)站建設(shè)報(bào)價(jià),網(wǎng)絡(luò)營(yíng)銷,網(wǎng)絡(luò)優(yōu)化,新邵網(wǎng)站推廣為一體的創(chuàng)新建站企業(yè),幫助傳統(tǒng)企業(yè)提升企業(yè)形象加強(qiáng)企業(yè)競(jìng)爭(zhēng)力??沙浞譂M足這一群體相比中小企業(yè)更為豐富、高端、多元的互聯(lián)網(wǎng)需求。同時(shí)我們時(shí)刻保持專業(yè)、時(shí)尚、前沿,時(shí)刻以成就客戶成長(zhǎng)自我,堅(jiān)持不斷學(xué)習(xí)、思考、沉淀、凈化自己,讓我們?yōu)楦嗟钠髽I(yè)打造出實(shí)用型網(wǎng)站。

Thymeleaf主要使用 org.thymeleaf.expression.Strings 類處理字符串,在模板中使用 #strings 對(duì)象來處理字符串。

開發(fā)環(huán)境:IntelliJ IDEA 2019.2.2
Spring Boot版本:2.1.8

新建一個(gè)名稱為demo的Spring Boot項(xiàng)目。

1、pom.xml
加入Thymeleaf依賴

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

2、src/main/resources/application.yml 
設(shè)置模板緩存為false,這樣修改html頁(yè)面后刷新瀏覽器能馬上看到結(jié)果

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

調(diào)用參數(shù)的toString方法返回字符串
<div th:text="${#strings.toString('hello')}"></div>
返回字符串的長(zhǎng)度
<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時(shí)設(shè)置默認(rèn)值
<div th:text="${#strings.defaultString('hello','a')}"></div>
<div th:text="${#strings.defaultString('','b')}"></div>
<div th:text="${#strings.defaultString(null,'c')}"></div>
判斷是否包含(區(qū)分大小寫)
<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>
判斷開頭和結(jié)尾是否包含(區(qū)分大小寫)
<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>
指定開始和結(jié)束索引,截取字符串(如果索引超過字符串長(zhǎng)度,則拋出異常)
<div th:text="${#strings.substring('hello',1,3)}"></div>
指定從某個(gè)字符串后面截取字符串(如果不包含則返回空字符串)
<div th:text="${#strings.substringAfter('hello','e')}"></div>
<div th:text="${#strings.substringAfter('hello','ee')}"></div>
指定從某個(gè)字符串前面截取字符串(如果不包含則返回空字符串)
<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>
轉(zhuǎn)換為大寫
<div th:text="${#strings.toUpperCase('hello')}"></div>
轉(zhuǎn)換為小寫
<div th:text="${#strings.toLowerCase('HELLO')}"></div>
首字母轉(zhuǎn)換為大寫
<div th:text="${#strings.capitalize('hello')}"></div>
首字母轉(zhuǎn)換為小寫
<div th:text="${#strings.unCapitalize('heLLo')}"></div>
每個(gè)單詞的首字母轉(zhuǎn)為大寫
<div th:text="${#strings.capitalizeWords('hello world')}"></div>
根據(jù)分隔符將每個(gè)單詞的首字母轉(zhuǎn)換為大寫
<div th:text="${#strings.capitalizeWords('hello-world','-')}"></div>
字符串前面追加
<div th:text="${#strings.prepend('world','hello ')}"></div>
字符串后面追加
<div th:text="${#strings.append('hello',' world')}"></div>
拼接字符串(參數(shù)個(gè)數(shù)不限)
<div th:text="${#strings.concat('hello',' world',' !')}"></div>
從第二個(gè)參數(shù)之后拼接字符串,如果參數(shù)為null,則用第一個(gè)參數(shù)替代
<div th:text="${#strings.concatReplaceNulls('*','hello',null,'world')}"></div>
刪除空白
<div th:text="${#strings.trim(' hello ')}"></div>
字符串截取指定長(zhǎng)度(最小為3),后面加...
<div th:text="${#strings.abbreviate('hello,world', 8)}"></div>
產(chǎn)生指定位數(shù)的隨機(jī)字母數(shù)字,范圍為大寫英文字母加0-9數(shù)字
<div th:text="${#strings.randomAlphanumeric(4)}"></div>
調(diào)用HtmlEscape類的escapeHtml4Xml方法對(duì)參數(shù)進(jìn)行編碼
<div th:text="${#strings.escapeXml('<span>hello</span>')}"></div>

瀏覽器訪問:http://localhost:8080
頁(yè)面輸出:

調(diào)用參數(shù)的toString方法返回字符串
hello
返回字符串的長(zhǎng)度
5
判斷是否為空或null
false
true
true
為空或null時(shí)設(shè)置默認(rèn)值
hello
b
c
判斷是否包含(區(qū)分大小寫)
true
false
判斷是否包含(忽略大小寫)
true
true
判斷開頭和結(jié)尾是否包含(區(qū)分大小寫)
true
false
false
true
獲取字符串的索引(如果不存在返回-1)
1
-1
指定開始和結(jié)束索引,截取字符串(如果索引超過字符串長(zhǎng)度,則拋出異常)
el
指定從某個(gè)字符串后面截取字符串(如果不包含則返回空字符串)
llo
指定從某個(gè)字符串前面截取字符串(如果不包含則返回空字符串)
h
替換字符串
hallo
轉(zhuǎn)換為大寫
HELLO
轉(zhuǎn)換為小寫
hello
首字母轉(zhuǎn)換為大寫
Hello
首字母轉(zhuǎn)換為小寫
heLLo
每個(gè)單詞的首字母轉(zhuǎn)為大寫
Hello World
根據(jù)分隔符將每個(gè)單詞的首字母轉(zhuǎn)換為大寫
Hello-World
字符串前面追加
hello world
字符串后面追加
hello world
拼接字符串(參數(shù)個(gè)數(shù)不限)
hello world !
從第二個(gè)參數(shù)之后拼接字符串,如果參數(shù)為null,則用第一個(gè)參數(shù)替代
hello*world
刪除空白
hello
字符串截取指定長(zhǎng)度(最小為3),后面加...
hello...
產(chǎn)生指定位數(shù)的隨機(jī)字母數(shù)字,范圍為大寫英文字母加0-9數(shù)字
PBAT
調(diào)用HtmlEscape類的escapeHtml4Xml方法對(duì)參數(shù)進(jìn)行編碼
&lt;span&gt;hello&lt;/span&gt;

到此,相信大家對(duì)“Thymeleaf字符串對(duì)象怎么使用”有了更深的了解,不妨來實(shí)際操作一番吧!這里是創(chuàng)新互聯(lián)網(wǎng)站,更多相關(guān)內(nèi)容可以進(jìn)入相關(guān)頻道進(jìn)行查詢,關(guān)注我們,繼續(xù)學(xué)習(xí)!

當(dāng)前題目:Thymeleaf字符串對(duì)象怎么使用
地址分享:http://www.muchs.cn/article4/ghpoie.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供搜索引擎優(yōu)化域名注冊(cè)、網(wǎng)站收錄微信小程序、做網(wǎng)站企業(yè)網(wǎng)站制作

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)

手機(jī)網(wǎng)站建設(shè)