JSTL Function Tags


JSTL functions provide a number of standard function, most of these functions are common string manipulation functions.

Syntax for including JSTL function library is:

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


The JSTL functions are as follows:

1. fn:contains(): It is used for testing if the string contains the specified substring in case sensitive way. If the specified substring is found in the string then it returns true otherwise it returns false.

Syntax:

    boolean contains(java.lang.String, java.lang.String)


Example: The following example will only execute the first if block but not the second if block because the string is case sensitive.

    <%@ taglib prefix=”c” uri=”http://java.sun.com/jsp/jstl/core” %>
    <%@ taglib prefix=”fn” uri=”http://java.sun.com/jsp/jstl/functions” %>
    <html>
    <body>
        <c:set var=”str” value=”Welcome to tutorialsduniya” />
        <c:if test=”${fn:contains(str, ‘tutorialsduniya’)} />
            <p>Found tutorialsduniya string</p>
        </c:if>
        <c:if test=”${fn:contains(str, ‘TUTORIALSDUNIYA’)} />
            <p>Found TUTORIALSDUNIYA string</p>
        </c:if>
    </body>
    </html>


2. fn:containsIgnoreCase(): It is used for testing if the string contains the specified substring in case insensitive way. If the specified substring is found in the string then it returns true otherwise it returns false.

Syntax:

    boolean containsIgnoreCase(java.lang.String, java.lang.String)


Example: The following example will execute both if blocks because the string is case insensitive.

    <%@ taglib prefix=”c” uri=”http://java.sun.com/jsp/jstl/core” %>
    <%@ taglib prefix=”fn” uri=”http://java.sun.com/jsp/jstl/functions” %>
    
    
        <c:set var=”str” value=”Welcome to tutorialsduniya” />
        <c:if test=”${fn:contains(str, ‘tutorialsduniya’)} />
            <p>Found tutorialsduniya string</p>
        </c:if>
        <c:if test=”${fn:contains(str, ‘TUTORIALSDUNIYA’)} />
            <p>Found TUTORIALSDUNIYA string</p>
        </c:if>
    </body>
    </html>


3. fn:startsWith(): It is used to test if an input string starts with a specified prefix.

Syntax:

    boolean startsWith(java.lang.String, java.lang.String)


Example: The following example will only execute the first if block but not the second if block because the string does not starts with ‘Hello’.

    <%@ taglib prefix=”c” uri=”http://java.sun.com/jsp/jstl/core” %>
    <%@ taglib prefix=”fn” uri=”http://java.sun.com/jsp/jstl/functions” %>
    
    
        <c:set var=”str” value=”Welcome to tutorialsduniya” />
        <c:if test=”${fn:startsWith(str, ‘Welcome’)} />
            <p>string starts with Welcome</p>
        </c:if>
        <c:if test=”${fn:startsWith(str, ‘Hello’)} />
            <p>string starts with Hello</p>
        </c:if>
    </body>
    </html>


4. fn:endsWith(): It is used to test if an input string ends with a specified suffix.

Syntax:

    boolean endsWith(java.lang.String, java.lang.String)


Example: The following example will only execute the first if block but not the second if block because the string does not ends with ‘ApniDuniya’.

    <%@ taglib prefix=”c” uri=”http://java.sun.com/jsp/jstl/core” %>
    <%@ taglib prefix=”fn” uri=”http://java.sun.com/jsp/jstl/functions” %>
    
    
        <c:set var=”str” value=”Welcome to tutorialsduniya” />
        <c:if test=”${fn:endsWith(str, ‘tutorialsduniya’)} />
            <p>string ends with tutorialsduniya</p>
        </c:if>
        <c:if test=”${fn:endsWith(str, ‘ApniDuniya’)} />
            <p>string ends with ApniDuniya</p>
        </c:if>
    </body>
    </html>



5. fn:indexOf(): It is used to return the index of first occurrence of a specified string within a string.

Syntax:

    int indexOf(java.lang.String, java.lang.String)


Example: The following example will return the index of first occurrence of tutorialsduniya i.e.11 in the string.

    <%@ taglib prefix=”c” uri=”http://java.sun.com/jsp/jstl/core” %>
    <%@ taglib prefix=”fn” uri=”http://java.sun.com/jsp/jstl/functions” %>
    <html>
    <body>
        <c:set var=”str” value=”Welcome to tutorialsduniya and tutorialsduniya” />
        <p>Index of substring is : ${fn:indexOf(str, ‘tutorialsduniya’)}</p>
    </body>
    </html>


6. fn:trim(): It is used to remove the blank spaces from both ends of a string.

Syntax:

    java.lang.String trim(java.lang.String)


Example:

    <%@ taglib prefix=”c” uri=”http://java.sun.com/jsp/jstl/core” %>
    <%@ taglib prefix=”fn” uri=”http://java.sun.com/jsp/jstl/functions” %>
    <html>
    <body>
        <c:set var=”str” value=”	Welcome to 	  tutorialsduniya   ” />
        <p>String after trimming : ${fn:trim(str)}</p>
    </body>
    </html>


7. fn:split(): It is used to split a string into an array based on a delimiter.

Syntax:

    java.lang.String[] split(java.lang.String, java.lang.String)


Example: The following example will split the string with ‘-‘ to an array and then joins the array into a new string with whitespace.

    <%@ taglib prefix=”c” uri=”http://java.sun.com/jsp/jstl/core” %>
    <%@ taglib prefix=”fn” uri=”http://java.sun.com/jsp/jstl/functions” %>
    <html>
    <body>
        <c:set var=”str” value=”Welcome-to-tutorialsduniya” />
        <c:set var=”str1” value=”${fn:split(str, ‘-‘)}” />
        <c:set var=”str2” value=”${fn:join(str1, ‘‘)}” />
        <p>New String : ${str2}</p>
    </body>
    </html>


8. fn:join(): It is used to concatenate all the elements of an array into a string with a specified separator.

Syntax:

    String join(java.lang.String[], java.lang.String)


Example: The following example will split the string with ‘-‘ to an array and then joins the array into a new string with whitespace.

    <%@ taglib prefix=”c” uri=”http://java.sun.com/jsp/jstl/core” %>
    <%@ taglib prefix=”fn” uri=”http://java.sun.com/jsp/jstl/functions” %>
    <html>
    <body>
        <c:set var=”str” value=”Welcome-to-tutorialsduniya” />
        <c:set var=”str1” value=”${fn:split(str, ‘-‘)}” />
        <c:set var=”str2” value=”${fn:join(str1, ‘‘)}” />
        <p>New String : ${str2}</p>
    </body>
    </html>



9. fn:length(): It is used to return the number of characters in a string or number of items in a collection.

Syntax:

    int length(java.lang.object)


Example: The following example will split the string with ‘-‘ to an array and then joins the array into a new string with whitespace.

    <%@ taglib prefix=”c” uri=”http://java.sun.com/jsp/jstl/core” %>
    <%@ taglib prefix=”fn” uri=”http://java.sun.com/jsp/jstl/functions” %>
    <html>
    <body>
        <c:set var=”str” value=”Welcome-to-tutorialsduniya” />
        <p>Length of the string : ${fn:length(str)}</p>
    </body>
    </html>


10. fn:replace(): It is used to replace all the occurrences of a string with another provided string sequence.

Syntax:

    String fn:replace(String input, String search_for_string, String replace_with_string)


Example: The following example will replace all the occurrences of ‘first’ in the string ‘str’ with ‘second’.

    <%@ taglib prefix=”c” uri=”http://java.sun.com/jsp/jstl/core” %>
    <%@ taglib prefix=”fn” uri=”http://java.sun.com/jsp/jstl/functions” %>
    <html>
    <body>
        <c:set var=”str” value=”This is the first string in my first paragraph.” />
        <c:set var=”str1” value=”${fn:replace(str, ‘first’, ‘second’)}” />
        <p>New string : ${str1}</p>
    </body>
    </html>


11. fn:toLowerCase(): It is used to convert all the characters of a string to lowercase.

Syntax:

    String fn:toLowerCase(String input_string)


Example: The following example will convert all the characters of the given string into lowercase characters.

    <%@ taglib prefix=”c” uri=”http://java.sun.com/jsp/jstl/core” %>
    <%@ taglib prefix=”fn” uri=”http://java.sun.com/jsp/jstl/functions” %>
    <html>
    <body>
        <c:set var=”str” value=”Welcome to TutorialsDuniya” />
        <p>String in lowercase : ${fn:toLowerCase(str)}</p>
    </body>
    </html>


12. fn:toUpperCase(): It is used to convert all the characters of a string to uppercase.

Syntax:

    String fn:toUpperCase(String input_string)


Example: The following example will convert all the characters of the given string into uppercase characters.

    <%@ taglib prefix=”c” uri=”http://java.sun.com/jsp/jstl/core” %>
    <%@ taglib prefix=”fn” uri=”http://java.sun.com/jsp/jstl/functions” %>
    <html>
    <body>
        <c:set var=”str” value=”Welcome to TutorialsDuniya” />
        <p>String in uppercase : ${fn:toUpperCase(str)}</p>
    </body>
    </html>



13. fn:substring(): It is used to return the substring of the given string according to specified start and end indices.

Syntax:

    String fn:substring(String given_string, int start_index, int end_index)


Example: The following example will take the substring from the given string with index 8 to index 20 i.e. ”to Tutorials”.

    <%@ taglib prefix=”c” uri=”http://java.sun.com/jsp/jstl/core” %>
    <%@ taglib prefix=”fn” uri=”http://java.sun.com/jsp/jstl/functions” %>
    <html>
    <body>
    <c:set var=”str” value=”Welcome to TutorialsDuniya” />
    <p>substring is : ${fn:substring(str, 8, 20)}</p>
    </body>
    </html>


14. fn:substringAfter(): It is used to return the part of a string which lies after the provided substring.

Syntax:

    String fn:substringAfter(String input_string, String sub_string)


Example: The following example will return the part of string i.e. “TutorialsDuniya” after the substring “to”.

    <%@ taglib prefix=”c” uri=”http://java.sun.com/jsp/jstl/core” %>
    <%@ taglib prefix=”fn” uri=”http://java.sun.com/jsp/jstl/functions” %>
    <html>
    <body>
        <c:set var=”str” value=”Welcome to TutorialsDuniya” />
        <p>New string after substring is : ${fn:substringAfter(str, “to”)}</p>
    </body>
    </html>


15. fn:substringBefore(): It is used to return the part of a string which lies before the provided substring.

Syntax:

    String fn:substringBefore(String input_string, String sub_string)


Example: The following example will return the part of string i.e. “Welcome” before the substring “to”.

    <%@ taglib prefix=”c” uri=”http://java.sun.com/jsp/jstl/core” %>
    <%@ taglib prefix=”fn” uri=”http://java.sun.com/jsp/jstl/functions” %>
    <html>
        <body>
            <c:set var=”str” value=”Welcome to TutorialsDuniya” />
            <p>New string before substring is : ${fn:substringBefore(str, “to”)}</p>
        </body>
    </html>