您好,欢迎来到三六零分类信息网!老站,搜索引擎当天收录,欢迎发信息

JSP中JSTL提供的函数标签EL表达式操作字符串的方法

2024/3/19 21:29:00发布17次查看
首先在jsp页面导入标签
<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions"%>
分类:
(1)转换字符串大小写: 
 tolowercase("要截取的字符串")----转换成小写----例: ${fn:tolowercase("hello,word!")}  touppercase("要截取的字符串")----转换成大写----例: ${fn:touppercase("hello,word!")}
(2)截取字符串:
substring("原字符串",开始的索引,结束的索引)----截取字符串----例: ${fn:substring("hello,word!",0,5)}
substringafter("原字符串","索引串")----取索引串之后的内容----例: ${fn:substringafter("hello,word!","hello,") }
substringbefore("原子付出","索引串")----取索引串之前的内容----例:${fn:substringbefore("hello,word!","word!") }
注:如果字符串中未包含索引串,返回空字符串.
(3)去空格:
trim("原字符串")----去掉字符串前后的空格----例:${fn:trim(" hello,word! ")}
(4)替换字符:
replace("原字符串","要替换的字符","替换后的字符")----替换字符----例:${fn:replace("hello,word!","word","你好")}
(5)字符串匹配查找:
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") }
(6)拆分、合并字符串
split("原字符串","拆分的分界符")----把字符串拆分成数组----例: ${fn:split("hello,word!",",")} join("字符串数组","组合后的分隔符")----把字符串数组组合成字符串----例:${fn:join(strarray,"|")}
(7)将字符串中的xml符号转换成实体符号:
escapexml("要转换的字符串")----不解析xml标签,直接输出----例:${fn:escapexml("<b>hello,word!</b>")}
(8)计算字符串长度
length("字符串")返回int----求字符串的长度----例:${fn:length("hello,word!")}
范例如下:
/***********截取一定长度字符串*****************/
在应用程序开发中,如果内容过长,想截取一定长度字符,然后补充"....."
jstl1.1引入了一个fn.tld的标签,用于处理字符,如获得字符length,substring,indexof,endwith,lowcase
实现截取字符串
如:
<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> /*****************截取一定长度字符串***************/
jstl使用表达式来简化页面的代码,这对一些标准的方法,例如bean的getter/setter方法,请求参数或者context以及 session中的数据的访问非常方便,但是我们在实际应用中经常需要在页面调用对象的某些方法,例如我需要调用字符串的length方法来获取字符串的长度时,在以往的开发过程中我们必须把对象先转为string类,然后在调用其length方法,这样的代码繁琐而且容易出错。
因此jstl内置了几个用于字符串操作的方法,可以直接在表达式中使用,大大的简化了代码,提供代码的可读性。在jstl的表达是中要使用一个函数,其格式如下
${fn:methodname(args....)}
在使用这些函数之前必须在jsp中引入标准函数的声明
<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions" %>
下面是jstl中自带的方法列表以及其描述
函数名 函数说明 使用举例 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)}
以上就是jsp中jstl提供的函数标签el表达式操作字符串的方法 的详细内容。
该用户其它信息

VIP推荐

免费发布信息,免费发布B2B信息网站平台 - 三六零分类信息网 沪ICP备09012988号-2
企业名录 Product