在JSTL中也提供了一些标准的函数,但是几乎都是和操作字符串相关的函数,如果需要使用这类函数的话,应该引入下面的taglib:
<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions" %>
下面就对这些函数做个大概的介绍吧:
1. fn:contains()
boolean contains(String, String);
判断输入的字符串中是否包含指定的字符串。
Example:
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/functions" prefix="fn" %>
<html>
<head>
<title>Using JSTL Functions</title>
</head>
<body> <c:set var="theString" value="I am a test String"/>
<c:if test="${fn:contains(theString, 'test')}">
<p>Found test string<p>
</c:if>
<c:if test="${fn:contains(theString, 'TEST')}">
<p>Found TEST string<p>
</c:if>
</body>
</html>
Result:
Found test string
2. fn:containsIgnoreCase()
boolean containsIgnoreCase(String, String);
判断输入的字符串中是否包含指定的字符串,在匹配过程中不区分大小写。
Example:
同上。
Result:
Found test string
Found TEST string
3. fn:startsWith()
boolean startsWith(String, String);
判断输入的字符串中是否包含指定的前缀。
Example:
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/functions" prefix="fn" %>
<html>
<head>
<title>Using JSTL Functions</title>
</head>
<body>
<c:set var="theString" value="I am a test String 123"/>
<c:if test="${fn:startsWith(theString, ‘I am')}">
<p>String starts with I am<p>
</c:if>
<c:if test="${fn:startsWith(theString, 'TEST')}">
<p>String starts with TEST<p>
</c:if>
</body>
</html>
Result:
String starts withI am
4. fn:endsWith()
boolean endsWith(String, String);
判断输入的字符串中是否包含指定的后缀。
Example:
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/functions" prefix="fn" %>
<html>
<head>
<title>Using JSTL Functions</title>
</head>
<body>
<c:set var="theString" value="I am a test String 123"/>
<c:if test="${fn:endsWith(theString, '123')}">
<p>String ends with 123<p>
</c:if>
<c:if test="${fn:endsWith(theString, 'TEST')}">
<p>String ends with TEST<p>
</c:if>
</body>
</html>
Result:
String ends with123
5. fn:escapeXML()
String escapeXML(String);
忽略输入字符串中的xml标签,以纯文本对待。
Example:
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/functions" prefix="fn" %>
<html>
<head>
<title>Using JSTL Functions</title>
</head>
<body>
<c:set var="string1" value="This is first String."/>
<c:set var="string2" value="This <abc>is second String.</abc>"/>
<p>With escapeXml() Function:</p>
<p>string (1) : ${fn:escapeXml(string1)}</p>
<p>string (2) : ${fn:escapeXml(string2)}</p>
<p>Without escapeXml() Function:</p>
<p>string (1) : ${string1}</p>
<p>string (2) : ${string2}</p>
</body>
</html>
Result:
With escapeXml()Function:
string(1):This is first String.
string(2):This<abc>is second String.</abc>
Without escapeXml()Function:
string(1):This is first String.
string(2):This is second String.
6. fn:indexOf()
int indexOf(String, String);
返回在输入字符串中指定字符串第一次出现的下标,如果不存在则返回-1。
Example:
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/functions" prefix="fn" %>
<html>
<head>
<title>Using JSTL Functions</title>
</head>
<body>
<c:set var="string1" value="This is first String."/>
<c:set var="string2" value="This <abc>is second String.</abc>"/>
<p>Index (1) : ${fn:indexOf(string1, "first")}</p>
<p>Index (2) : ${fn:indexOf(string2, "second")}</p>
</body>
</html>
Result:
Index(1):8
Index(2):13
7. fn:join()
String join(String[ ], String);
将数组中的所有元素用指定的分隔符连接成一个字符串。
Example:
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/functions" prefix="fn" %>
<html>
<head>
<title>Using JSTL Functions</title>
</head>
<body>
<c:set var="string1" value="This is first String."/>
<c:set var="string2" value="${fn:split(string1, ' ')}"/>
<c:set var="string3" value="${fn:join(string2, '-')}"/>
<p>Final String : ${string3}</p>
</body>
</html>
Result:
Final String:This-is-first-String.
8. fn:split()
String[ ] split(String, String);
用指定的分隔符将输入的字符创分割成一个字符串数组。
Example:
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/functions" prefix="fn" %>
<html>
<head>
<title>Using JSTL Functions</title>
</head>
<body>
<c:set var="string1" value="This is first String."/>
<c:set var="string2" value="${fn:split(string1, ' ')}"/>
<c:set var="string3" value="${fn:join(string2, '-')}"/>
<p>String (3) : ${string3}</p>
<c:set var="string4" value="${fn:split(string3, '-')}"/>
<c:set var="string5" value="${fn:join(string4, ' ')}"/>
<p>String (5) : ${string5}</p>
</body>
</html>
Result:
String(3):This-is-first-String.
String(5):This is first String.
9. fn:length()
int length(Object);
返回一个字符串的长度或者一个集合中元素的个数。
Example:
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/functions" prefix="fn" %>
<html>
<head>
<title>Using JSTL Functions</title>
</head>
<body>
<c:set var="string1" value="This is first String."/>
<c:set var="string2" value="This is second String."/>
<p>Length of String (1) : ${fn:length(string1)}</p>
<p>Length of String (2) : ${fn:length(string2)}</p>
</body>
</html>
Result:
Length of String(1):21
Length of String(2):22
10. fn:replace()
boolean replace(String, String, String);
将一个字符串中的指定字符串替换成为其他字符串。
Example:
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/functions" prefix="fn" %>
<html>
<head>
<title>Using JSTL Functions</title>
</head>
<body>
<c:set var="string1" value="This is first String."/>
<c:set var="string2" value="${fn:replace(string1, 'first', 'second')}"/>
<p>Final String : ${string2}</p>
</body>
</html>
Result:
Final String:This is second String.
11. fn:substring()
String substring(String, int, int);
返回一个字符串中指定起始位置和终止位置的字串。
Example:
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/functions" prefix="fn" %>
<html>
<head>
<title>Using JSTL Functions</title>
</head>
<body>
<c:set var="string1" value="This is first String."/>
<c:set var="string2" value="${fn:substring(string1, 5, 15)}"/>
<p>Final substring : ${string2}</p>
</body>
</html>
Result:
Final substring:is first S
12. fn:substringBefore()
String substringBefore(String, String);
返回输入字符串中指定字符串之前的字串。
Example:
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/functions" prefix="fn" %>
<html>
<head>
<title>Using JSTL Functions</title>
</head>
<body>
<c:set var="string1" value="This is first String."/>
<c:set var="string2" value="${fn:substringBefore(string1, 'is')}"/>
<p>Final substring : ${string2}</p>
</body>
</html>
Result:
Final substring: This
13. fn:substringAfter()
String substringAfter(String, String);
返回输入字符串中指定字符串之后的字符串。
Example:
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/functions" prefix="fn" %>
<html>
<head>
<title>Using JSTL Functions</title>
</head>
<body>
<c:set var="string1" value="This is first String."/>
<c:set var="string2" value="${fn:substringAfter(string1, 'is')}"/>
<p>Final substring : ${string2}</p>
</body>
</html>
Result:
Final substring:is first String.
14. fn:toLowerCase()
String toLowerCase(String);
将输入字符串转换成小写字母。
Example:
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/functions" prefix="fn" %>
<html>
<head>
<title>Using JSTL Functions</title>
</head>
<body>
<c:set var="string1" value="This is first String."/>
<c:set var="string2" value="${fn:toLowerCase(string1)}"/>
<p>Final string : ${string2}</p>
</body>
</html>
Result:
Final string:this is first string.
15. fn:toUpperCase()
String toUpperCase(String);
将输入的字符串转换成大写字母。
Example:
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/functions" prefix="fn" %>
<html>
<head>
<title>Using JSTL Functions</title>
</head>
<body>
<c:set var="string1" value="This is first String."/>
<c:set var="string2" value="${fn:toUpperCase(string1)}"/>
<p>Final string : ${string2}</p>
</body>
</html>
Result:
Final string: THIS IS FIRST STRING.
16. trim()
String trim(String);
去除输入字符串中开始和末尾的空格。
Example:
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/functions" prefix="fn" %>
<html>
<head>
<title>Using JSTL Functions</title>
</head>
<body>
<c:set var="string1" value="This is first String"/>
<p>String (1) Length : ${fn:length(string1)}</p>
<c:set var="string2" value="${fn:trim(string1)}"/>
<p>String (2) Length : ${fn:length(string2)}</p>
<p>Final string : ${string2}</p>
</body>
</html>
Result:
String(1) Length:29
String(2) Length:20
Final string:This is first String