JSTL Functions Tag LibraryS2C Home « JSTL Functions Tag Library

In our final lesson on JSTL tag libraries we look at the Functions tag library. The Functions tag library consists of sixteen actions. The fn:length tag is used for returning the length of a collection or for returning the number of characters within a string. The other fifteen Functions tags are all used for string manipulation.

Array Function ActionsTop

There are two array Function actions, fn:join which joins all elements of an array into a string and fn:split which splits a string into an array of substrings.

Action /
Attribute Name
Object Type Description
<fn:join>
arrayString[]The array of strings to be joined.
separatorStringString to separate each element of the array in the resulting string.
resultStringAll array elements joined into one string.
<fn:split>
stringStringInput string to be split into an array of strings.
delimitersStringDelimiter characters used to split the string.
resultString[]The resultant array of strings.

fn:joinTop

The fn:join tag joins all elements of an array into a string.

If the array attribute is null, an emptry string is returned.

If the separator attribute is null, it is processed as an emptry string.

If the separator attribute is an empty string, the elements are joined together without any separator.


fn:join(array, separator) -> String

fn:splitTop

The fn:split tag splits a string into an array of substrings.

If the string attribute is null, it is processed as an emptry string.

If the string attribute is an empty string, a one element array is returned consisting of an emptry string.

If the delimiters attribute is null, it is processed as an emptry string.

If the delimiters attribute is an empty string, a one element array is returned consisting of the original string.


fn:split(string, delimiters) -> String

Array ExamplesTop

The following code shows examples of using the fn:join and fn:split tags.


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

<p>Function Library fn:join and fn:split Examples</p>

<c:set var="split" value='${fn:split("A Stitch in TIME saves NINE", " ")}'>

<table>
   <c:forEach var="substring" items="${split}">
      <tr><td>${substring}</tr></td>
   </c:forEach>
</table>

<p>Join array into String: ${fn:join(split, "*")}<br></p>

The following screnshot shows the results of running the above JSP code.

Caps Tags

Capitalization Function ActionsTop

There are two capitalization Function actions, fn:toLowerCase and fn:toUpperCase which convert all characters of a string to lowercase and uppercase respectively.

Action /
Attribute Name
Object Type Description
<fn:toLowerCase>
stringStringInput string to transform to lower case.
resultintInput string transformed to lower case.
<fn:toUpperCase>
stringStringInput string to transform to upper case.
resultintInput string transformed to upper case.

fn:toLowerCaseTop

The fn:toLowerCase tag transforms the contents of a string to lower case.

If the string attribute is null it is treated as an emptry string and returned as such.


fn:toLowerCase(input) -> integer

fn:toUpperCaseTop

The fn:toUpperCase tag transforms the contents of a string to upper case.

If the string attribute is null it is treated as an emptry string and returned as such.


fn:toUpperCase(input) -> integer

Capitalization ExamplesTop

The following code shows examples of using the fn:toLowerCase and fn:toUpperCase tags.


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

<p>Function Library fn:toLowerCase and fn:toUpperCase Examples</p>
<p>String Transformed To Lower Case: ${fn:toLowerCase("A Stitch in TIME saves NINE")}<br></p>
<p>String Transformed To Upper Case: ${fn:toUpperCase("A Stitch in TIME saves NINE")}<br></p>

The following screnshot shows the results of running the above JSP code.

Caps Tags

Contains Function ActionsTop

There are five contains Function actions these being fn:indexOf which returns the index within a string of the first occurrence of a specified substring, fn:startsWith which tests if a string starts with the specified prefix, fn:endsWith which tests if a string ends with the specified suffix, fn:contains which tests if a string contains the specified substring and fn:containsIgnoreCase which tests if a string contains the specified substring in a case insensitive way.

Action /
Attribute Name
Object Type Description
<fn:indexOf>
stringStringInput string to which the substring function is applied.
substringStringSubstring to search for within the input string.
resultintIf the value of the substring attribute is a substring of the input string returns the index of the first character of the first such substring occurrence
If the value of the substring attribute does not occur as a substring then -1 is returned.
<fn:startsWith>
stringStringInput string to which the function is applied.
prefixStringPrefix to be matched against.
resultbooleanReturns true if the value of the prefix attribute is the prefix of the character sequence represented by the string attribute, otherwise returns false.
<fn:endsWith>
stringStringInput string to which the function is applied.
suffixStringSuffix to be matched against.
resultbooleanReturns true if the value of the prefix attribute is the suffix of the character sequence represented by the string attribute, otherwise returns false.
<fn:contains>
stringStringInput string to which the substring function is applied.
substringStringSubstring to be etsted for.
resultbooleanReturns true if character sequence of substring attribute exists in character sequence represented by the string attribute, otherwise returns false.
<fn:containsIgnoreCase>
stringStringInput string to which the substring function is applied.
substringStringSubstring to be etsted for.
resultbooleanReturns true if character sequence of substring attribute exists in character sequence represented by the string attribute ignoring case differences, otherwise returns false.

fn:indexOfTop

The fn:indexOf tag returns the index (0-based) first occurrence of a specified substring within a string.

If the string attribute is null, the input is treated as an empty string and is returned as such.

If the substring attribute is is null, it is processed as an empty string.

If the substring attribute is empty, this matches the beginning of the string and 0 is returned.


fn:indexOf(string, substring) -> INT

fn:startsWithTop

The fn:startsWith tag tests if a string starts with the specified prefix.

If the string attribute is null, it is processed as an empty string.

If the prefix attribute is null, it is processed as an empty string.


fn:startsWith(string, prefix) -> boolean

fn:endsWithTop

The fn:endsWith tag tests if a string ends with the specified suffix.

If the string attribute is null, it is processed as an empty string.

If the suffix attribute is null, it is processed as an empty string.

If the suffix attribute is empty this matches the end of the string and true is returned.


fn:endsWith(string, suffix) -> boolean

fn:containsTop

The fn:contains tag tests if a string contains the specified substring.

If the string attribute is null, it is processed as an empty string.

If the substring attribute is is null, it is processed as an empty string.

If the substring attribute is empty this matches the beginning of the string and true is returned.


fn:contains(string, substring) -> string

fn:containsIgnoreCaseTop

The fn:containsIgnoreCase tag tests if a string contains the specified substring in a case insensitive way.

If the string attribute is null, it is processed as an empty string.

If the substring attribute is is null, it is processed as an empty string.

If the substring attribute is empty this matches the beginning of the string and true is returned.


fn:containsIgnoreCase(string, substring) -> string

Contains ExamplesTop

The following code shows examples of using the fn:indexOf, fn:startsWith, fn:endsWith, fn:containsand fn:containsIgnoreCase tags.


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

<p>fn:indexOf, fn:startsWith, fn:endsWith, fn:contains and fn:containsIgnoreCase Examples<br></p>
<p>fn:indexOf - Index position: ${fn:indexOf("123456789", "56")}<br></p>
<p>fn:indexOf - Substring empty: ${fn:indexOf("123456789", "")}<br></p>
<p>fn:startsWith - True returned: ${fn:startsWith("123456789", "123")}<br></p>
<p>fn:startsWith - False returned: ${fn:startsWith("123456789", "234")}<br></p>
<p>fn:endsWith - True returned: ${fn:endsWith("123456789", "9")}<br></p>
<p>fn:contains - False returned: ${fn:contains("abcdef", "CDE")}<br></p>
<p>fn:containsIgnoreCase - True returned: ${fn:containsIgnoreCase("abcdef", "CDE")}<br></p>

The following screnshot shows the results of running the above JSP code.

Contains Tag

Escape XML Function ActionTop

There is one escape XML Function action fn:escapeXml which can be used to escape characters that could be interpreted as XML markup.

Action /
Attribute Name
Object Type Description
<fn:escapeXml>
stringStringInput string on which the conversion is applied.
resultStringConverted string.

fn:escapeXmlTop

The fn:escapeXml tag is used to escape characters that could be interpreted as XML markup.

If the string attribute is null, it is processed as an emptry string.


fn:escapeXml(string) -> string

Escape XML ExamplesTop

The following code shows examples of using the fn:escapeXml tag.


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

<p>Function Library fn:escapeXml Examples</p>
<p>Convert HTML markup: ${fn:escapeXml("<table><tr><td>Some data</tr></td></table>")}<br></p>
<p>Convert characters: ${fn:escapeXml("<>")}<br></p>

Length Function ActionTop

There is one length Function action fn:length which can be used for returning the length of a collection or for returning the number of characters within a string.

Action /
Attribute Name
Object Type Description
<fn:length>
inputSupported type for items attribute in <c:forEach> action, or StringInput collection or string on which the length is to be computed.
resultintLength of the collection or the string.

fn:lengthTop

The fn:length tag returns the length of a collection or returns the number of characters within a string.

If the input attribute is null the input is treated as an empty collection and the value returned is 0.

If the input attribute is an empty string, the value returned is 0.


fn:length(input) -> integer

Length ExamplesTop

The following code shows examples of using the fn:length tag.


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

<p>Function Library fn:length Examples</p>
<p>Size of collection: ${fn:length(headerValues)}<br></p>
<p>Characters in string: ${fn:length("1 2 3 4 5 6 7")}<br></p>

The following screnshot shows the results of running the above JSP code.

Length Tag

Replace Function ActionTop

There is one replace Function action fn:replace which returns a string resulting from replacing all occurrences of a before substring within an input string, into occurrences of an after substring.

Action /
Attribute Name
Object Type Description
<fn:replace>
inputStringStringInput string on which the replace function is applied.
beforeSubstringStringThe before substring to be replaced.
afterSubstringStringThe after substring used to replace the before substring.
resultStringThe string resulting from replacing the string value held within the beforeSubstring attribute with the string value held within the afterSubstring attribute.

fn:replaceTop

The fn:replace tag returns a string resulting from replacing all occurrences of a before substring within an input string, into occurrences of an after substring.

If the inputString attribute is null the input is treated as an empty string and is returned as such.

If the beforeSubstring attribute is an empty string, it is processed as an empty string.

If the afterSubstring attribute is an empty string, all occurrences of the beforeSubstring attribute are removed from the inputString attribute.


fn:replace(inputString, beforeSubstring, afterSubstring) -> string

Replace ExamplesTop

The following code shows examples of using the fn:replace tag.


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

<p>Function Library fn:replace Examples</p>
<p>Replacing Occurrences: ${fn:replace("123456789", "456", "789")}<br></p>
<p>Before substring empty: ${fn:replace("123456789", "", "789")}<br></p>
<p>After substring empty: ${fn:replace("123456789", "456", "")}<br></p>

The following screnshot shows the results of running the above JSP code.

Replace Tag

String Subset Function ActionsTop

There are three string subset Function actions these being fn:substring which returns a subset of a string, fn:substringAfter which returns a subset of a string following a specific substring and fn:substringBefore which returns a subset of a string before a specific substring.

Action /
Attribute Name
Object Type Description
<fn:substring>
stringStringInput string to which the substring function is applied.
beginIndexintThe beginning index (0-based), inclusive.
endIndexintThe ending index (0-based), inclusive.
resultStringSubstring of the input string.
<fn:substringAfter>
stringStringInput string to which the substring function is applied.
substringStringSubstring delimiting beginning of the subset of the input string to be returned.
resultStringSubstring of the input string.
<fn:toUpperCase>
stringStringInput string to which the substring function is applied.
substringStringSubstring delimiting beginning of the subset of the input string to be returned.
resultStringSubstring of the input string.

fn:substringTop

The fn:substring tag returns a subset of a string.

If the string attribute is null it is treated as an emptry string and returned as such.

If the beginIndex attribute is greater than the last index of the input string an emptry string is returned.

If the beginIndex attribute is less than 0 it is adjusted to 0.

If the endIndex attribute is less than 0 or greater than the length of the input string, its value is adjusted to be the length of the input string.

If the endIndex attribute is less than the beginIndex attribute an emptry string is returned.


fn:substring(string, beginIndex, endIndex) -> String

fn:substringAfterTop

The fn:substringAfter tag returns a subset of a string following a specific substring. The substring returned starts at the first character after the substring matched in the input string, and extends up to the end of the input string.

If the string attribute is null it is treated as an emptry string and returned as such.

If the substring attribute is null it is treated as an emptry string and returned as such.

If the string attribute is an emptry string it is returned as such.

If the substring attribute is an emptry string it matches the beginning of the input string and the input string is returned.

If the substring attribute does not occur in the input string then an empty stringis returned.


fn:substringAfter(string, substring) -> String

fn:substringBeforeTop

The fn:substringBefore tag returns a subset of a string before a specific substring. The substring returned starts at the first character in the input string and extends up to the character just before the substring matched in the input string.

If the string attribute is null it is treated as an emptry string and returned as such.

If the substring attribute is null it is treated as an emptry string and returned as such.

If the string attribute is an emptry string it is returned as such.

If the substring attribute is an emptry string it matches the beginning of the input string and an empty string is returned.

If the substring attribute does not occur in the input string then an empty string is returned.


fn:substringBefore(string, substring) -> String

String Subset ExamplesTop

The following code shows examples of using the fn:substring, fn:substringAfter and fn:substringBefore tags.


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

<p>Function Library fn:substring, fn:substringAfter and fn:substringBefore Examples</p>
<p>Return subset of string: ${fn:substring("A Stitch in TIME saves NINE", 5, 10)}<br></p>
<p>Return subset of string after: ${fn:substringAfter("A Stitch in TIME", "Stit")}<br></p>
<p>Return subset of string before: ${fn:substringBefore("A Stitch in TIME", "IM")}<br></p>

The following screnshot shows the results of running the above JSP code.

Subset Tags

Trimming Function ActionTop

There is one trimming Function action fn:trim which can be used to remove white space from both ends of a string.

Action /
Attribute Name
Object Type Description
<fn:trim>
stringStringInput string on which the trim is applied.
resultStringThe trimmed string.

fn:trimTop

The fn:trim tag removes white space from both ends of a string.

If the string attribute is null the input is treated as an empty string and is returned as such.

If the input attribute is an empty string, the value returned is 0.


fn:trim(string) -> string

Trimming ExamplesTop

The following code shows examples of using the fn:trim tag.


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

<p>Function Library fn:trim Examples</p>
<p>Trimming both ends: ${fn:trim("     BOOM    ")}<br></p>
<p>Trimming end: ${fn:trim("KABOOM   ")}<br></p>

The following screnshot shows the results of running the above JSP code.

Trim Tag

Lesson 6 Complete

In our final lesson on JSTL tag libraries we looked at the Functions tag library.

What's Next?

In the next lesson we look at creating our own custom tags for when the functionality we need is not present in the JSTL tag libraries.

go to home page Homepage go to top of page Top