JSTL function fn 用法

Now, let’s have a look at the JSTL Function tag. Create a new JSP. Let’s name it Jstl_Functions_Tags.jsp. Then create a new link to direct to this new JSP.

 

<table border="1">

<tr>

<td>

<a href="Jstl_Functions_Tags.jsp">JSTL Functions Tags</a>

</td>

<td>

String Manipulations

</td>

</tr>

</table>

 

Do not forget to include JSTL taglib directive for prefix fn in our JSP. Here is how to import the fn JSTL taglib directive.

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

 

The JSTL functions tag will mainly deal with string manipulation. Let’s try some of them.

 

<c:set var="sentence" value=" Try This String for Manipulations " />

<c:out value="${sentence}" />

 

Above codes are used to set a fixed string to variable “sentence” and print it out for display. Next, please pay attention on how we are going to combine JSTL core <c> tag with JSTL function <fn> tag.

 

<c:out value="${fn:length(sentence)}" />

 

Above codes will print out the length of variable “sentence”. It will produce 35 since all characters are counted.

 

<c:out value="${fn:toUpperCase(sentence)}" />

<c:out value="${fn:toLowerCase(sentence)}" />

 

Above codes are used to convert our “sentence” to UPPER or LOWER case.

 

<c:out value="${fn:trim(sentence)}" />

<c:out value="${fn:length(fn:trim(sentence))}" />

<c:out value="${fn:replace(sentence,' ','_')}" />

 

Above codes are used to trim the “sentence” and count the length of trimmed “sentence”. Trimming the sentence means that we are removing the extra spaces (if any) in the string either in front or in back. You need to know that the spaces within the string will NOT be trimmed. It should produce 33 since the spaces at the beginning and at the end will be discarded. While the sample above of fn:replace will replace all occurrences of spaces with underscores.

 

<c:out value="${fn:substring(sentence,0,8)}" />

<c:out value="${fn:substringAfter(sentence,'for')}" />

<c:out value="${fn:substringBefore(sentence,'for')}" />

<c:out value="${fn:indexOf(sentence,'g')}" />

 

The fn:substring will take 3 parameters. 1st parameter is our “sentence” variable itself. 2nd parameter is for the starting point and 3rd parameter is where it must stop to return result of substring. More advanced substring functions are fn:substringAfter and fn:substringBefore. See at the screenshot to see how they work.

 

And the fn:indexOf will return the position of defined character in our “sentence”.

 

<c:out value="${fn:contains(sentence,'G')}" />

<c:out value="${fn:containsIgnoreCase(sentence,'G')}" />

<c:out value="${fn:startsWith(sentence,' Try')}" />

<c:out value="${fn:endsWith(sentence,'pulations ')}" />

 

fn:contains will return boolean value true if correct and false otherwise. However, it is case sensitive and will sensitively check for the lower case and upper case of the string. fn:containsIgnoreCase work in the same way as fn:contains but it is not case sensitive which means that it will not care whether it is in lower case or in upper case. fn:startsWith and fn:endsWith will also return boolean to check whether the “sentence” starts or ends with corresponding given value.

 

Again, clean build the project and run the project. The index.jsp will appear as usual and click the “Jstl_Functions_Tags” to see the result.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值