JSTL Functions with Examples

In this article, we will discuss all JSTL functions with examples.
The JSTL function provides a number of standard functions, most of these functions are common string manipulation functions. The syntax used for including JSTL function library in your JSP is:
<%@ taglib uri="http://java.sun.com/jsp/jstl/functions" prefix="fn" %>  

JSTL Function Tags List

Let's list out all the JSTL function with it's meaning.
  1. fn:contains function: This function checks whether the given string is present in the input as sub-string. It does a case sensitive check.
  2. fn:containsIgnoreCase(): It does a case-insensitive check to see whether the provided string is a sub-string of input.
  3. fn:indexOf(): It is used for finding out the start position of a string in the provided string. Function returns -1 when string is not found in the input.
  4. fn:escapeXML(): It is used for HTML/XML character escaping which means it treats html/xml tags as a string. Similar to the escapeXml attribute of <c:out> tag.
  5. fn:join() and fn:split() functions: JSTL functions: fn:join() concatenates the strings with a given separator and returns the output string. fn:split() splits a given string into an array of substrings.
  6. fn:length(): The JSTL function fn:length() is used for computing the length of a string or to find out the number of elements in a collection. It returns the length of the object.
  7. fn:startsWith(): It checks the specified string is a prefix of a given string.
  8. fn:endsWith(): fn:endsWith() JSTL function is used for checking the suffix of a string. It checks whether the given string ends with a particular string.
  9. fn:substring(): This JSTL function is used for getting a substring from the provided string.
  10. fn:substringAfter(): It is used for getting a substring which is present in the input string before a specified string.
  11. fn:substringBefore(): It gets a substring from the input which comes after a specified string.
  12. fn:trim(): JSTL Function fn:trim() removes spaces from beginning and end of a string and function.
  13. fn:toUpperCase(): It is just opposite of fn:toLowerCase() function. It converts input string to an uppercase string.
  14. fn:toLowerCase(): This function is used for converting an input string to a lower case string.
  15. fn:replace(): fn:replace() function search for a string in the input and replace it with the provided string. It does case-sensitive processing.
Let's discuss each JSTL function with an example.

1. fn:contains function

This function checks whether the given string is present in the input as sub-string. It does a case sensitive check.

Syntax:

boolean fn:contains(String inputstring, String checkstring)

Example:

        <h1>JSTL fn:contains() function example</h1>
 <c:set var="str1" value="Java Guides" />
 <c:set var="str2" value="Guides" />
 <c:if test="${fn:contains(str1, str2)}">

  <c:out value="'Guides' substring present in 'Java Guides' string" />
 </c:if>
 <hr>

2. fn:containsIgnoreCase()

It does a case-insensitive check to see whether the provided string is a sub-string of input.

Syntex:

boolean fn:containsIgnoreCase(String input, String checkstring)

Example:

<h1>JSTL fn:containsIgnoreCase() function example</h1>
<c:set var="str1" value="Java Guides" />
<c:set var="str2" value="java guides" />
<c:if test="${fn:containsIgnoreCase(str1, str2)}">

<c:out value="'java guides' substring present in 'Java Guides' string" />
</c:if>
<hr>

fn:indexOf()

This JSTL function is used for finding out the start position of a string in the provided string. Function returns -1 when a string is not found in the input.

Syntex:

int indexOf(String,  String 

Example:

<h1>JSTL fn:indexOf() function example</h1>
<c:set var="str1" value="Java Guides" />
<c:set var="str2" value="Guides" />
<p>Index of "Guides" in "Java Guides" : ${fn:indexOf(str1, str2)}</p>
<hr>

fn:escapeXML()

This function is used for HTML/XML character escaping which means it treats html/xml tags as a string. Similar to the escapeXml attribute of <c:out> tag.

System:

String escapeXml(String input_string)

Example

Message1: <b>Hi This is just a message</b>
<br>Message2: <i>This is an example</i>
<br>Message1 and fn:escapeXml(): ${fn:escapeXml("<b>Hi This is just a message</b>")}
<br>Message2 and fn:escapeXml(): ${fn:escapeXml("<i>This is an example</i>")}

fn:join() fn:join()

It concatenates the strings with a given separator and returns the output string.

Syntax:

String fn:join(String arrayofstrings, String separator)

Example:

<h1>JSTL fn:join() function example</h1>
<%
String arr[] = { "Java", "Programming", "Language" };
session.setAttribute("names", arr);
%>
${fn:join(names, " & ")}
<hr>

fn:split()

It splits a given string into an array of substrings. Splitting process considers the delimiter string which we provide during a function call. I.e. we provide the string and delimiter as arguments to the function and it returns the array of strings after splitting the input based on the delimiter string.

Syntax

String[] fn:split(String inputstring, String delimiterstring)

Example:

<h1>JSTL fn:split() function example</h1>
<c:set var="msg" value="This is an example of JSTL function" />
<c:set var="arrayofmsg" value="${fn:split(msg,' ')}" />
<c:forEach var="i" begin="0" end="6">
arrayofmsg[${i}]: ${arrayofmsg[i]}<br>
</c:forEach>

fn:length()

The JSTL function fn:length() is used for computing the length of a string or to find out the number of elements in a collection. It returns the length of the object.

Syntax:

int length(Object)
The return type of this function is int. It returns the length of the object provided in it as an argument.

Example:

<hr>
<h1>JSTL fn:length() function example</h1>
<c:set var="str" value="Java Guides" />
Length of str is: ${fn:length(str)}
<br>

fn:startsWith()

It checks the specified string is a prefix of a given string.

Syntax:

boolean fn:startsWith(String input, String prefix)
This function returns a boolean value. It gives true when the string starts with the given prefix else it returns false.

Example:

<hr>
<h1>JSTL fn:startsWith() function example</h1>
<c:set var="str" value="Java Guides" />
Java Guides starts with Java : ${fn:startsWith(str, 'Java')}
<br>

fn:endsWith()

The fn:endsWith() JSTL function is used for checking the suffix of a string. It checks whether the given string ends with a particular string.

Syntax:

boolean fn:endsWith(input_string, suffix_string)
It validates whether the input_string ends with suffix_string. If the test is successful then the function returns true else it gives false. The return type of the function is boolean and it receives both the strings as arguments.

Example:

<hr>
<h1>JSTL fn:endsWith() function example</h1>
<c:set var="str" value="Java Guides" />
Java Guides ends with Guides : ${fn:endsWith(str, 'Guides')}
<br>

fn:substring()

This JSTL function is used for getting a substring from the provided string.

Syntax:

String fn:substring(String inputstring, int start, int end)
  • return type of function: String
  • input string: The string from which a substring needs to be taken
  • start: Starting position of the substring
  • end: end position of the substring

Example:

<hr>
<h1>JSTL fn:substring() function example</h1>
<c:set var="str" value="Java Guides" />
${fn:substring(msg, 5, 10)}
<br>

fn:substringAfter()

This method is used for getting a substring which is present in the input string before a specified string.

Syntax:

String fn:substring(String inputstring, int start, int end)
  • return type of function: String
  • input string: The string from which a substring needs to be taken
  • start: Starting position of the substring
  • end: end position of the substring

Example:

<hr>
<h1>JSTL fn:substringAfter() function example</h1>
<c:set var="str" value="Java Guides" />
${fn:substringAfter(str, "Gu")}
<br>

fn:substringBefore()

This method gets a substring from the input which comes after a specified string.

Syntax

String fn:substring(String inputstring, int start, int end)
  • The return type of function: String
  • input string: The string from which a substring needs to be taken
  • start: Starting position of the substring
  • end: end position of the substring

Example:

<hr>
<h1>JSTL fn:substringBefore() function example</h1>
<c:set var="str" value="Java Guides" />
${fn:substringBefore(str, "Gu")}
<br>

fn:trim()

This fn:trim() function removes spaces from the beginning and end of a string and function.

Syntax:

String fn:trim(String input)
The function returns the string after removing the white spaces from the start and end of the input String.

Example:

<hr>
<h1>JSTL fn:trim() function example</h1>
<c:set var="str" value="  Java Guides  " />
Trim of " Java Guides " is: ${fn:trim(str)}
<br>

fn:toUpperCase()

It is just opposite of fn:toLowerCase() function. It converts input string to an uppercase string.

Syntax:

String fn:toUpperCase(String input)
It returns the String after converting the input string to uppercase.

Example:

<hr>
<h1>JSTL fn:toUpperCase() function example</h1>
<c:set var="str" value="java guides" />
Upper case of "java guides" is: ${fn:toUpperCase(str)}
<br>

fn:toLowerCase()

This function is used for converting an input string to a lower case string.

Syntax

String fn:toLowerCase(String  input)

Example:

<hr>
<h1>JSTL fn:toLowerCase() function example</h1>
<c:set var="str" value="JAVA GUIDES" />
Lower case of "JAVA GUIDES" is: ${fn:toLowerCase(str)}
<br>

fn:replace()

This fn:replace() function search for a string in the input and replace it with the provided string. It does case-sensitive processing.

Syntax:

String fn:replace(String input, String search_for, String replace_with)
Three string arguments and return type are also String. It searches the search_for string in input and replaces it with replace_with string. If the string is not found it returns the actual input as it is.

Example:

<c:set var="author" value="Ramesh Fadatare" />
<c:set var="randomstring" value="abc def abc ghi ABC" />
${fn:replace(author, "Ramesh", "Umesh")} 
${fn:replace(randomstring, "abc", "hello")}


Comments