博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
JSTL函数
阅读量:4617 次
发布时间:2019-06-09

本文共 7805 字,大约阅读时间需要 26 分钟。

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" %>
Insert title here

//结果输出为: 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" %>
Insert title here

//结果输出为: 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" %>
Insert title here

原字符串:

判断是否以I开头:

//结果输出为:

  原字符串: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" %>
Insert title here

原字符串:

判断是否以 girl 结尾:

//结果输出为:

  原字符串: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" %>
Insert title here

原字符串效果:${text}

忽略Xml格式后:${fn:escapeXml(text)}

//结果输出为:

  原字符串效果: 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" %>
Insert title here

原字符串:

am 在其中的位置为:

//结果输出为:

  原字符串: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" %>
Insert title here

原字符串:

最后为:

//结果输出为:

  原字符串: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" %>
Insert title here

原字符串:

该字符串的长度为:

//结果输出为:

  原字符串: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" %>
Insert title here

原字符串:

将 girl 替换为 boy :

//结果输出为:

  原字符串: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" %>
Insert title here

原字符串:

将其转为字符串数组,分隔符为空格:

//结果输出为:

 原字符串: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" %>
Insert title here

原字符串:

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

//结果输出为:

  原字符串: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" %>
Insert title here

原字符串:

截取 a 后面字符串:

//结果输出为:

  原字符串: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" %>
Insert title here

原字符串:

截取 a 前面字符串:

//结果输出为:

  原字符串: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" %>
Insert title here

原字符串:

转换为大写:

//结果输出为:

  原字符串: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" %>
Insert title here

原字符串:

转换为大写:

//结果输出为:

  原字符串: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" %>
Insert title here

原字符串长度:

去除首尾空格后长度:

//结果输出为:

  原字符串长度:16

  去除首尾空格后长度:10
 

 

转载于:https://www.cnblogs.com/blog-yuesheng521/p/5530711.html

你可能感兴趣的文章
在Spring下集成ActiveMQ
查看>>
OTS parsing error: invalid version tag woff和ttf文件被Filter拦截
查看>>
使用Kazoo操作ZooKeeper服务治理
查看>>
linux系统安装配置exim4(源码安装)
查看>>
EF 两种删除方式的比较
查看>>
bzoj 2152: 聪聪可可
查看>>
推迟幸福感
查看>>
Java框架之spring 项目 附加之noteresult(status msg data uuid MD5)
查看>>
图->定义
查看>>
animate.css
查看>>
《机电传动控制》学习笔记04
查看>>
数据库系统概念:数据库的修改
查看>>
python 多继承(新式类) 四
查看>>
JS写入Json到CSV并下载
查看>>
Python2.3-原理之语句和语法
查看>>
RecyclerView实现底部载入很多其它功能
查看>>
【大话设计模式】——简单工厂模式
查看>>
如何安全地关闭MySQL实例
查看>>
Redis Cluster 3.0搭建与使用
查看>>
python基础学习第一波
查看>>