Home Web Front-end JS Tutorial Methods for manipulating strings using function labels EL expressions provided by JSTL in JSP

Methods for manipulating strings using function labels EL expressions provided by JSTL in JSP

Sep 26, 2017 pm 02:08 PM
javascript expression

首先在jsp页面导入标签

1

<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions"%>

Copy after login

分类:
(1)转换字符串大小写: 

1

2

toLowerCase("要截取的字符串")----转换成小写----例: ${fn:toLowerCase("Hello,Word!")}

toUpperCase("要截取的字符串")----转换成大写----例: ${fn:toUpperCase("Hello,Word!")}

Copy after login

(2)截取字符串:
  substring("原字符串",开始的索引,结束的索引)----截取字符串----例: ${fn:substring("Hello,Word!",0,5)}
  substringAfter("原字符串","索引串")----取索引串之后的内容----例: ${fn:substringAfter("Hello,Word!","Hello,") }
  substringBefore("原子付出","索引串")----取索引串之前的内容----例:${fn:substringBefore("Hello,Word!","Word!") }
   注:如果字符串中未包含索引串,返回空字符串.
(3)去空格:

1

trim("原字符串")----去掉字符串前后的空格----例:${fn:trim("   Hello,Word!   ")}

Copy after login

(4)替换字符:

1

replace("原字符串","要替换的字符","替换后的字符")----替换字符----例:${fn:replace("Hello,Word!","Word","你好")}

Copy after login

(5)字符串匹配查找:

1

2

3

4

5

6

7

8

9

indexOf("字符串","子字符串")返回int----求字符串第一次出现的下标----例:${fn:indexOf("Hello,Word!",",")}

   startsWith("字符串1","字符串2")返回boolean----判断一个字符串是否已另一个字符串开头

    ----${fn:startsWith("Hello,Word","H") }

  endsWith("字符串1","字符串2")返回boolean----判断一个字符串是否已另一个字符串结尾

    ----${fn:endsWith("Hello,Word","h") }

  contains("字符串1","字符串2")返回boolean----判断一个字符串是否包含另一个字符串

    ----${fn:contains("Hello,Word","Hello") }

  containsIgnoreCase("字符串1","字符串2")返回boolean----判断一个字符串是否包含另一个字符串,不区分大小写

    ----${fn:containsIgnoreCase("Hello,Word","w") }

Copy after login

(6)拆分、合并字符串

1

2

split("原字符串","拆分的分界符")----把字符串拆分成数组----例: ${fn:split("Hello,Word!",",")}

join("字符串数组","组合后的分隔符")----把字符串数组组合成字符串----例:${fn:join(strArray,"|")}

Copy after login

(7)将字符串中的XML符号转换成实体符号:

1

escapeXml("要转换的字符串")----不解析xml标签,直接输出----例:${fn:escapeXml("<b>Hello,Word!</b>")}

Copy after login

(8)计算字符串长度

1

length("字符串")返回int----求字符串的长度----例:${fn:length("Hello,Word!")}

Copy after login


范例如下:

/***********截取一定长度字符串*****************/
在应用程序开发中,如果内容过长,想截取一定长度字符,然后补充"....."
jstl1.1引入了一个fn.tld的标签,用于处理字符,如获得字符length,substring,indexof,endWith,lowcase
实现截取字符串
如:

1

2

3

4

5

6

7

8

9

10

<c:set var="log.logTitle" value="做一个截取字符串长度的测试"

<c:choose>

    <c:when test="${fn:length(log.logTitle) > 10}">

     <c:out value="${fn:substring(log.logTitle, 0, 10)}......" />

    </c:when>

    <c:otherwise>

     <c:out value="${log.logTitle}" />

    </c:otherwise>

   </c:choose>

/*****************截取一定长度字符串***************/

Copy after login

JSTL使用表达式来简化页面的代码,这对一些标准的方法,例如bean的getter/setter方法,请求参数或者context以及 session中的数据的访问非常方便,但是我们在实际应用中经常需要在页面调用对象的某些方法,例如我需要调用字符串的length方法来获取字符串的长度时,在以往的开发过程中我们必须把对象先转为String类,然后在调用其length方法,这样的代码繁琐而且容易出错。
因此JSTL内置了几个用于字符串操作的方法,可以直接在表达式中使用,大大的简化了代码,提供代码的可读性。在JSTL的表达是中要使用一个函数,其格式如下

1

${fn:methodName(args....)}

Copy after login

在使用这些函数之前必须在JSP中引入标准函数的声明

1

<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions" %>

Copy after login

下面是JSTL中自带的方法列表以及其描述

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

函数名

函数说明

使用举例

contains

判断字符串是否包含另外一个字符串

<c:if test="${fn:contains(name, searchString)}">

containsIgnoreCase

判断字符串是否包含另外一个字符串( 大小写无关)

<c:if test="${fn:containsIgnoreCase(name, searchString)}">

endsWith

判断字符串是否以另外字符串结束

<c:if test="${fn:endsWith(filename, ".txt")}">

escapeXml

把一些字符转成XML 表示,例如< 字符应该转为&lt;

${fn:escapeXml(param:info)}

indexOf

子字符串在母字符串中出现的位置

${fn:indexOf(name, "-")}

join

将数组中的数据联合成一个新字符串,并使用指定字符格开

${fn:join(array, ";")}

length

获取字符串的长度,或者数组的大小

${fn:length(shoppingCart.products)}

replace

替换字符串中指定的字符

${fn:replace(text, "-", "&#149;")}

split

把字符串按照指定字符切分

${fn:split(customerNames, ";")}

startsWith

判断字符串是否以某个子串开始

<c:if test="${fn:startsWith(product.id, "100-")}">

substring

获取子串

${fn:substring(zip, 6, -1)}

substringAfter

获取从某个字符所在位置开始的子串

${fn:substringAfter(zip, "-")}

substringBefore

获取从开始到某个字符所在位置的子串

${fn:substringBefore(zip, "-")}

toLowerCase

转为小写

${fn.toLowerCase(product.name)}

toUpperCase

转为大写字符

${fn.UpperCase(product.name)}

trim

去除字符串前后的空格

${fn.trim(name)}

Copy after login

The above is the detailed content of Methods for manipulating strings using function labels EL expressions provided by JSTL in JSP. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
1 months ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to implement an online speech recognition system using WebSocket and JavaScript How to implement an online speech recognition system using WebSocket and JavaScript Dec 17, 2023 pm 02:54 PM

How to use WebSocket and JavaScript to implement an online speech recognition system Introduction: With the continuous development of technology, speech recognition technology has become an important part of the field of artificial intelligence. The online speech recognition system based on WebSocket and JavaScript has the characteristics of low latency, real-time and cross-platform, and has become a widely used solution. This article will introduce how to use WebSocket and JavaScript to implement an online speech recognition system.

WebSocket and JavaScript: key technologies for implementing real-time monitoring systems WebSocket and JavaScript: key technologies for implementing real-time monitoring systems Dec 17, 2023 pm 05:30 PM

WebSocket and JavaScript: Key technologies for realizing real-time monitoring systems Introduction: With the rapid development of Internet technology, real-time monitoring systems have been widely used in various fields. One of the key technologies to achieve real-time monitoring is the combination of WebSocket and JavaScript. This article will introduce the application of WebSocket and JavaScript in real-time monitoring systems, give code examples, and explain their implementation principles in detail. 1. WebSocket technology

How to use JavaScript and WebSocket to implement a real-time online ordering system How to use JavaScript and WebSocket to implement a real-time online ordering system Dec 17, 2023 pm 12:09 PM

Introduction to how to use JavaScript and WebSocket to implement a real-time online ordering system: With the popularity of the Internet and the advancement of technology, more and more restaurants have begun to provide online ordering services. In order to implement a real-time online ordering system, we can use JavaScript and WebSocket technology. WebSocket is a full-duplex communication protocol based on the TCP protocol, which can realize real-time two-way communication between the client and the server. In the real-time online ordering system, when the user selects dishes and places an order

How to implement an online reservation system using WebSocket and JavaScript How to implement an online reservation system using WebSocket and JavaScript Dec 17, 2023 am 09:39 AM

How to use WebSocket and JavaScript to implement an online reservation system. In today's digital era, more and more businesses and services need to provide online reservation functions. It is crucial to implement an efficient and real-time online reservation system. This article will introduce how to use WebSocket and JavaScript to implement an online reservation system, and provide specific code examples. 1. What is WebSocket? WebSocket is a full-duplex method on a single TCP connection.

JavaScript and WebSocket: Building an efficient real-time weather forecasting system JavaScript and WebSocket: Building an efficient real-time weather forecasting system Dec 17, 2023 pm 05:13 PM

JavaScript and WebSocket: Building an efficient real-time weather forecast system Introduction: Today, the accuracy of weather forecasts is of great significance to daily life and decision-making. As technology develops, we can provide more accurate and reliable weather forecasts by obtaining weather data in real time. In this article, we will learn how to use JavaScript and WebSocket technology to build an efficient real-time weather forecast system. This article will demonstrate the implementation process through specific code examples. We

How to use insertBefore in javascript How to use insertBefore in javascript Nov 24, 2023 am 11:56 AM

Usage: In JavaScript, the insertBefore() method is used to insert a new node in the DOM tree. This method requires two parameters: the new node to be inserted and the reference node (that is, the node where the new node will be inserted).

Simple JavaScript Tutorial: How to Get HTTP Status Code Simple JavaScript Tutorial: How to Get HTTP Status Code Jan 05, 2024 pm 06:08 PM

JavaScript tutorial: How to get HTTP status code, specific code examples are required. Preface: In web development, data interaction with the server is often involved. When communicating with the server, we often need to obtain the returned HTTP status code to determine whether the operation is successful, and perform corresponding processing based on different status codes. This article will teach you how to use JavaScript to obtain HTTP status codes and provide some practical code examples. Using XMLHttpRequest

How to get HTTP status code in JavaScript the easy way How to get HTTP status code in JavaScript the easy way Jan 05, 2024 pm 01:37 PM

Introduction to the method of obtaining HTTP status code in JavaScript: In front-end development, we often need to deal with the interaction with the back-end interface, and HTTP status code is a very important part of it. Understanding and obtaining HTTP status codes helps us better handle the data returned by the interface. This article will introduce how to use JavaScript to obtain HTTP status codes and provide specific code examples. 1. What is HTTP status code? HTTP status code means that when the browser initiates a request to the server, the service

See all articles