现在的位置: 首页 > 综合 > 正文

JSTL字符串处理函数

2013年08月24日 ⁄ 综合 ⁄ 共 1797字 ⁄ 字号 评论关闭

近日处理使用到JSTL的判断字符串是否以某字符串结束,老实说,以前并没有深入接触过jstl语言,而是大部分WEB开发时间使用在了诸如struts1/2的tag上面,所以也算是重新开始研究或是recall起sun自己的tag。

1。判断某字符串是否以某字串结束

引入JSTL:

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

set比较值:

<c:set var="requestURI" value="/store/index.jsp" />
<c:set var="suppdix1" value="/store/index.jsp" />
<c:set var="suppdix2" value="/store/" />

使用<c:choose>、<fn:endsWith(param1,param2)>判断:

<c:choose>
  <c:when test="${ !empty productIdVar}">
      
  </c:when>
  <c:when test="${! empty categoryIdVar }">
    
  </c:when>
  <c:when test="${ fn:endsWith(requestURI, suppdix1) || 
      fn:endsWith(requestURI, suppdix2) }">
    
  </c:when>
  <c:otherwise>

  </c:otherwise>
</c:choose>


2。其他fn函数

函数 描述
fn:contains(string, substring) 如果参数string中包含参数substring,返回true
fn:containsIgnoreCase(string, substring) 如果参数string中包含参数substring(忽略大小写),返回true
fn:endsWith(string, suffix) 如果参数 string 以参数suffix结尾,返回true
fn:escapeXml(string) 将有特殊意义的XML (和HTML)转换为对应的XML character entity code,并返回
fn:indexOf(string, substring) 返回参数substring在参数string中第一次出现的位置
fn:join(array, separator) 将一个给定的数组array用给定的间隔符separator串在一起,组成一个新的字符串并返回。
fn:length(item) 返回参数item中包含元素的数量。参数Item类型是数组、collection或者String。如果是String类型,返回值是String中的字符数。
fn:replace(string, before, after) 返回一个String对象。用参数after字符串替换参数string中所有出现参数before字符串的地方,并返回替换后的结果
fn:split(string, separator) 返回一个数组,以参数separator 为分割符分割参数string,分割后的每一部分就是数组的一个元素
fn:startsWith(string, prefix) 如果参数string以参数prefix开头,返回true
fn:substring(string, begin, end) 返回参数string部分字符串, 从参数begin开始到参数end位置,包括end位置的字符
fn:substringAfter(string, substring) 返回参数substring在参数string中后面的那一部分字符串
fn:substringBefore(string, substring) 返回参数substring在参数string中前面的那一部分字符串
fn:toLowerCase(string) 将参数string所有的字符变为小写,并将其返回
fn:toUpperCase(string) 将参数string所有的字符变为大写,并将其返回
fn:trim(string) 去除参数string 首尾的空格,并将其返回



抱歉!评论已关闭.