• JSTL函数


    JSTL包含了一系列标准函数。

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

    函数有:

    函数描述
    fn:contains() 测试输入的字符串是否包含指定的子串
    fn:containsIgnoreCase() 测试输入的字符串是否包含指定的子串,大小写不敏感
    fn:endsWith() 测试输入的字符串是否以指定的后缀结尾
    fn:escapeXml() 跳过可以作为XML标记的字符
    fn:indexOf() 返回指定字符串在输入字符串中出现的位置
    fn:join() 将数组中的元素合成一个字符串然后输出
    fn:length() 返回字符串长度
    fn:replace() 将输入字符串中指定的位置替换为指定的字符串然后返回
    fn:split() 将字符串用指定的分隔符分隔然后组成一个子字符串数组并返回
    fn:startsWith() 测试输入字符串是否以指定的前缀开始
    fn:substring() 返回字符串的子集
    fn:substringAfter() 返回字符串在指定子串之后的子集
    fn:substringBefore() 返回字符串在指定子串之前的子集
    fn:toLowerCase() 将字符串中的字符转为小写
    fn:toUpperCase() 将字符串中的字符转为大写
    fn:trim() 移除首位的空白符

    fn:contains() 测试输入的字符串中是否包含指定的字符串

    语法:

    boolean contains(String,String)

    fn:contains(受测试字符串,指定字符串)

    eg:

    <%@ page language="java" contentType="text/html; charset=utf-8"  pageEncoding="utf-8"%>
    <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
    <%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions" %>
    
    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
    <title>Insert title here</title>
    </head>
    <body>
        <c:set var="text" value="I am a boy" />
        <c:out value="${text}" /><br>
        <c:if test="${fn:contains(text,'girl')}">
            <p><c:out value="has 'girl' in it." /></p>
        </c:if>
        <c:if test="${fn:contains(text,'boy')}">
            <p><c:out value="has 'boy' in it." /></p>
        </c:if>
    </body>
    </html>


    //结果输出为:
    I am a boy

      has 'boy' in it.

    fn:containsIgnoreCase()  在忽略大小写情况下,测试输入的字符串中是否包含指定的字符串

    语法:

    boolean containsIgnoreCase(String,String)

    fn.containsIgnoreCase(受测试字符串,指定字符串)

    eg:

    <%@ page language="java" contentType="text/html; charset=utf-8"  pageEncoding="utf-8"%>
    <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
    <%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions" %>
    
    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
    <title>Insert title here</title>
    </head>
    <body>
        <c:set var="text" value="I am a boy" />
        <c:out value="${text}" /><br>
        <c:if test="${fn:containsIgnoreCase(text,'GIRL')}">
            <p><c:out value="has 'GIRL' in it." /></p>
        </c:if>
        <c:if test="${fn:containsIgnoreCase(text,'BOY')}">
            <p><c:out value="has 'BOY' in it." /></p>
        </c:if>
    </body>
    </html>

    //结果输出为:
    I am a boy

      has 'BOY' in it.

    
    

    fn:startsWith() 确定字符串是否以指定字符串开始

    语法:

    boolean startWith(String,String)

    fn:startWith(受测试字符串,指定字符串)

    eg:

    <%@ page language="java" contentType="text/html; charset=utf-8"  pageEncoding="utf-8"%>
    <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
    <%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions" %>
    
    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    <title>Insert title here</title>
    </head>
    <body>
        <c:set var="text" value="I am a girl " />
        <p>原字符串:<c:out value="${text}" /></p>
        <p>判断是否以I开头:<c:out value="${fn:startsWith(text,'I')}" /></p>
    </body>
    </html>

    //结果输出为:

      原字符串:I am a girl

      判断是否以I开头:true

     

    fn:endsWith() 测试字符串是否以指定字符串结尾

    语法:

    boolean endsWith(String,String)

    fn:endsWith(受测试字符串,指定字符串)

    eg:

    <%@ page language="java" contentType="text/html; charset=utf-8"  pageEncoding="utf-8"%>
    <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
    <%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions" %>
    
    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    <title>Insert title here</title>
    </head>
    <body>
        <c:set var="text" value="I am a girl" />
        <p>原字符串:<c:out value="${text}" /></p>
        <p>判断是否以 girl 结尾:<c:out value="${fn:endsWith(text,'girl')}" /></p>
    </body>
    </html>

    //结果输出为:

      原字符串:I am a girl

      判断是否以 girl 结尾:true
    
    

    fn:escapeXml() 忽略XML标记,同样在<c:out escapeXml="true" />

    语法:

    java.lang.String escapeXml(String)

    fn:escapeXml(字符串)

    eg:

    <%@ page language="java" contentType="text/html; charset=utf-8"  pageEncoding="utf-8"%>
    <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
    <%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions" %>
    
    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; ">
    <title>Insert title here</title>
    </head>
    <body>
        <c:set var="text" value="<po> I am a girl </po>" />
        <p>原字符串效果:${text}</p>
        <p>忽略Xml格式后:${fn:escapeXml(text)}</p>
        
    </body>
    </html>

    //结果输出为:

      原字符串效果: I am a girl

      忽略Xml格式后:<po> I am a girl </po>

    fn:indexOf() 返回指定字符串在该字符串中所在的位置

    语法:

    int indexOf(String,String)

    fn:indexOf(字符串,指定字符串)

    eg:

    <%@ page language="java" contentType="text/html; charset=utf-8"  pageEncoding="utf-8"%>
    <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
    <%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions" %>
    
    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    <title>Insert title here</title>
    </head>
    <body>
        <c:set var="text" value="I am a girl" />
        <p>原字符串:<c:out value="${text}" /></p>
        <p>am 在其中的位置为:<c:out  value="${fn:indexOf(text,'am')}" /></p>
    </body>
    </html>

    //结果输出为:

      原字符串:I am a girl

      am 在其中的位置为:2

    fn:join() 利用指定分隔符连接数组中所有元素

    语法:

    String join(String[],String)

    fn:join(连接数组,分隔符)

    eg:

    <%@ page language="java" contentType="text/html; charset=utf-8"  pageEncoding="utf-8"%>
    <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
    <%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions" %>
    
    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    <title>Insert title here</title>
    </head>
    <body>
        <c:set var="text" value="I am a girl" />
        <p>原字符串:<c:out value="${text}" /></p>
        <c:set var="text1" value="${fn:split(text,' ')}" />
        <p>最后为:<c:out  value="${fn:join(text1,'-')}" /></p>
    </body>
    </html>

    //结果输出为:

      原字符串:I am a girl

      最后为:I-am-a-girl

    fn:length() 返回字符串长度,或集合个数

    语法:

    int length(Object)

    fn:length(所求长度对象)

    eg:

    <%@ page language="java" contentType="text/html; charset=utf-8"  pageEncoding="utf-8"%>
    <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
    <%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions" %>
    
    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    <title>Insert title here</title>
    </head>
    <body>
        <c:set var="text" value="I am a girl" />
        <p>原字符串:<c:out value="${text}" /></p>
        <p>该字符串的长度为:<c:out value="${fn:length(text)}" /></p>
    </body>
    </html>

    //结果输出为:

      原字符串:I am a girl

      该字符串的长度为:11

    fn:replace() 替换字符串

    语法:

    boolean replace(String,String,String)

    fn:replace(母字符串,替换源,替换目标)

    eg:

    <%@ page language="java" contentType="text/html; charset=utf-8"  pageEncoding="utf-8"%>
    <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
    <%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions" %>
    
    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    <title>Insert title here</title>
    </head>
    <body>
        <c:set var="text" value="I am a girl" />
        <p>原字符串:<c:out value="${text}" /></p>
        <p>将 girl 替换为 boy :<c:out value="${fn:replace(text,'girl','boy')}" /></p>
    </body>
    </html>

    //结果输出为:

      原字符串:I am a girl

      将 girl 替换为 boy :I am a boy

    fn:split() 利用分隔符分割字符串成为数组

    语法:

    String[] split(String,String)

    fn:split(字符串,分隔符)

    eg:

    <%@ page language="java" contentType="text/html; charset=utf-8"  pageEncoding="utf-8"%>
    <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
    <%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions" %>
    
    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
    <title>Insert title here</title>
    </head>
    <body>
        <c:set var="text" value="I am a boy" />
        <p>原字符串:<c:out value="${text}" /></p>
        <p>将其转为字符串数组,分隔符为空格:<br>
            <c:set var="texts" value="${fn:split(text,' ')}" />
            <c:forEach var="item"  items="${texts}">
                <c:out value="${item}"/><br>
            </c:forEach>
        </p>
    </body>
    </html>

    //结果输出为:

     原字符串:I am a boy

    将其转为字符串数组,分隔符为空格:
    I
    am
    a
    boy

    fn:substring() 截取字符串

    语法:

    String substring(String,int,int)

    fn:substring(字符串,开始位置,结束位置)

    eg:

    <%@ page language="java" contentType="text/html; charset=utf-8"  pageEncoding="utf-8"%>
    <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
    <%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions" %>
    
    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
    <title>Insert title here</title>
    </head>
    <body>
        <c:set var="text" value="I am a boy" />
        <p>原字符串:<c:out value="${text}" /></p>
        <p>截取 2与8之间的字符串:<c:out value="${fn:substring(text,2,8)}" /></p>
    </body>
    </html>

    //结果输出为:

      原字符串:I am a boy

      截取 2与8之间的字符串:am a b
    
    

    fn:substringAfter() 截取指定字符串之后的字符串

    语法:

    String substringAfter(String,String)

    fn:substringAfter(字符串,指定字符串)

    eg:

    <%@ page language="java" contentType="text/html; charset=utf-8"  pageEncoding="utf-8"%>
    <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
    <%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions" %>
    
    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
    <title>Insert title here</title>
    </head>
    <body>
        <c:set var="text" value="I am a boy" />
        <p>原字符串:<c:out value="${text}" /></p>
        <p>截取 a 后面字符串:<c:out value="${fn:substringAfter(text,'a')}" /></p>
    </body>
    </html>

    //结果输出为:

      原字符串:I am a boy

      截取 a 后面字符串:m a boy

     

    fn:substringBefore() 截取指定字符串之前的部分

    语法:

    String substringBefore(String,String)

    fn:substringBefore(字符串,指定字符串)

    eg:

    <%@ page language="java" contentType="text/html; charset=utf-8"  pageEncoding="utf-8"%>
    <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
    <%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions" %>
    
    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
    <title>Insert title here</title>
    </head>
    <body>
        <c:set var="text" value="I am a boy" />
        <p>原字符串:<c:out value="${text}" /></p>
        <p>截取 a 前面字符串:<c:out value="${fn:substringBefore(text,'a')}" /></p>
    </body>
    </html>

    //结果输出为:

      原字符串:I am e boy

      截取 a 前面字符串:I am

     

    fn:toLowerCase() 将指定字符串转换为小写

    语法:

    String toLowerCase(String)

    fn:toLowerCase(指定字符串)

    eg:

    <%@ page language="java" contentType="text/html; charset=utf-8"  pageEncoding="utf-8"%>
    <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
    <%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions" %>
    
    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
    <title>Insert title here</title>
    </head>
    <body>
        <c:set var="text" value="I am a boy" />
        <p>原字符串:<c:out value="${text}" /></p>
        <p>转换为大写:<c:out value="${fn:toLowerCase(text)}" /></p>
    </body>
    </html>

    //结果输出为:

      原字符串:I am a boy

      转换为大写:i am a boy

    fn:toUpperCase() 将指定字符串转换为大写

    语法:

    String toUpperCase(String)

    fn:toUpperCase(指定字符串)

    eg:

    <%@ page language="java" contentType="text/html; charset=utf-8"  pageEncoding="utf-8"%>
    <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
    <%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions" %>
    
    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
    <title>Insert title here</title>
    </head>
    <body>
        <c:set var="text" value="I am a boy" />
        <p>原字符串:<c:out value="${text}" /></p>
        <p>转换为大写:<c:out value="${fn:toUpperCase(text)}" /></p>
    </body>
    </html>

    //结果输出为:

      原字符串:I am a boy

      转换为大写:I AM A BOY
    
    

    fn:trim() 清除字符串首尾空白字符串

    语法:

    String trim(String)

    fn:trim(指定字符串)

    eg:

    <%@ page language="java" contentType="text/html; charset=utf-8"  pageEncoding="utf-8"%>
    <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
    <%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions" %>
    
    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
    <title>Insert title here</title>
    </head>
    <body>
        <c:set var="text" value="   I am a boy   " />
        <p>原字符串长度:<c:out value="${fn:length(text)}" /></p>
        <p>去除首尾空格后长度:<c:out value="${fn:length(fn:trim(text))}" /></p>
    </body>
    </html>

    //结果输出为:

      原字符串长度:16

      去除首尾空格后长度:10
    
    
  • 相关阅读:
    Luogu P3372 【模板】线段树 1
    Luogu P1439 【模板】最长公共子序列
    Luogu P3374 【模板】树状数组 1
    Computer Vision_33_SIFT:Improving Bag-of-Features for Large Scale Image Search——2010
    Computer Vision_33_SIFT:Distinctive Image Features from Scale-Invariant Keypoints——2004
    Computer Vision_33_SIFT:PCA-SIFT A More Distinctive Representation for Local Image Descriptors——2004
    Computer Vision_33_SIFT:Speeded-Up Robust Features (SURF)——2006
    Computer Vision_33_SIFT:Evaluation of Interest Point Detectors——2000
    Computer Vision_33_SIFT:Object recognition from local scale-invariant features——1999
    Image Processing and Analysis_21_Scale Space:Feature Detection with Automatic Scale Selection——1998
  • 原文地址:https://www.cnblogs.com/blog-yuesheng521/p/5530711.html
Copyright © 2020-2023  润新知