Latest Tutorials| Questions and Answers|Ask Questions?|Site Map



Home Jsp Use of Tag of JSTL

Related Tutorials


 
 

Share on Google+Share on Google+

Use of Tag of JSTL

Advertisement
In this section, you will learn how to use Tag of JSTL.

Use of <fn:indexOf(String, String)> Tag of JSTL

     

In this section we will learn how to use <fn:indexOf> Tag of JSTL. This tag returns index of first occurrence of specified sub string in the given string string. This takes string type as arguments and returns int type.

 

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








indexOf_fnJstlTag.jsp

<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions" %>
<html>
    <head>
        <title>Example of fn:indexof tag of JSTL</title>
    </head>
    <body>
        <form method="POST">
            This will calculate the first occurance of given 
            character/string in the <br>specified String.
            <table>
                <tr>
                    <td>Enter Text</td>
                    <td><input type="text" name="text"></td>
                </tr>
                
                <tr>
                    <td>Search</td>
                    <td><input type="text" name="str"></td>
                </tr>
                
                <tr>
                    <td></td>
                    <td><input type="submit" value="search"></td>
                </tr>
            </table>  
        </form>
        <c:if test="${pageContext.request.method=='POST'}">
            <c:set var="text" value="${param.text}"/>
            <c:set var="str" value="${param.str}"/>
            
            <font size="5" color="#1B2A0A">
                Index of first occurance : 
            </font>
            <font size="5" color="green">

                <c:out value="${fn:indexOf(text,str)}"/>  
              
            </font>
        </c:if>
    </body>
</html>

 

This jsp code shows the following output :

 

Suppose in 'mahendra singh' string user want to find index of the first occurrence of 'e'....




When user clicks on search button, output will be.....




Download Source Code

 

Advertisements

If you enjoyed this post then why not add us on Google+? Add us to your Circles



Liked it!  Share this Tutorial


Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Posted on: August 24, 2008

Related Tutorials

Discuss: Use of Tag of JSTL  

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments:1
asdasd
February 15, 2012
lastindexOf

get lastIndexOf????
DMCA.com