在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