dineshonjava

JSTL fn:trim() Function

JSTL Trim function is used to remove white space from the start and end of the String. The fn:trim() function removes white space from both ends of a string.

Syntax:
The fn:trim() function has following syntax:
java.lang.String trim(java.lang.String)

Example:

Following is the example to explain the functionality of this function:
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/functions" prefix="fn" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>JSTL Trim Function Example</title>
</head>
<body>
 <c:out value="${fn:trim(' Hello ')}"></c:out>
</body>
</html>

Output: Hello

As you can see above, JSTL Trim Function takes ' Hello ' string as an input parameter and remove the white space from it and will return 'Hello' as a result.




<<Previous <<   || Index ||   >>Next >>






JSTL fn:toUpperCase() Function

The fn:toUpperCase() function converts all the characters of a string to uppercase. JSTL toLowerCase Function returns String that contains all characters in lower case.

Syntax:
The fn:toUpperCase() function has following syntax:
java.lang.String toLowerCase(java.lang.String)

Example:

Following is the example to explain the functionality of this function:
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" 
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>JSTL ToUpperCase Function Example</title>
</head>
<body>
  <c:out value="${fn:toUpperCase('Dinesh Rajput')}"/>
</body>
</html>

Output: DINESH RAJPUT

As you can see above, fn:toUpperCase() takes a String as input parameter. It converts all characters in that String to upper case and returns it with all upper case characters.





<<Previous <<   || Index ||   >>Next >>






JSTL fn:toLowerCase() Function

JSTL toLowerCase Function returns String that contains all characters in lower case. The fn:toLowerCase() function converts all the characters of a string to lowercase.

Syntax:
The fn:toLowerCase() function has following syntax:
java.lang.String toLowerCase(java.lang.String)

Example:

Following is the example to explain the functionality of this function:
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" 
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>JSTL ToLowerCase Function Example</title>
</head>
<body>
  <c:out value="${fn:toLowerCase('Dinesh Rajput')}"/>
</body>
</html>

Output: dinesh rajput

As you can see above, fn:toLowerCase() takes a String as input parameter. It converts all characters in that String to lower case and returns it with all lower case characters.




<<Previous <<   || Index ||   >>Next >>






JSTL fn:substringBefore() Function

JSTL SubstringBefore function is used to get the substring of target string. This function takes 2 parameters.

First parameter in JSTL SubstringBefore function is the target String from which you want to derive the substring.

Second parameter in JSTL SubstringBefore function is the substring which breaks the string in to 2 parts and function returns the substring before it.

The fn:substringBefore() function returns the part of a string before a specified substring.

Syntax:
The fn:substringBefore() function has following syntax:
java.lang.String substringBefore(java.lang.String, 
                                 java.lang.String)

Example:

Following is the example to explain the functionality of this function:
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/functions" prefix="fn" %>
<html>
<head>
<title>Using JSTL Functions</title>
</head>
<body>

<c:set var="string1" value="This is first String."/>
<c:set var="string2" value="${fn:substringBefore(string1, 
                                            'first')}" />

<p>Final sub string : ${string2}</p>

</body>
</html>

This would produce following result:

Final sub string : This is





<<Previous <<   || Index ||   >>Next >>






JSTL fn:substringAfter() Function

The fn:substringAfter() function returns the part of a string after a specified substring.

JSTL SubstringAfter function is used to get the substring of target string. This function takes 2 parameters.

First parameter in JSTL SubstringAfter function is the target String from which you want to derive the substring.

Second parameter in JSTL SubstringAfter function is the substring which breaks the string in to 2 parts and function returns the substring after it.

Syntax:
The fn:substringAfter() function has following syntax:
java.lang.String substringAfter(java.lang.String, java.lang.String)

Example:
Following is the example to explain the functionality of this function:

<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/functions" prefix="fn" %>
<html>
<head>
<title>Using JSTL Functions</title>
</head>
<body>

<c:set var="string1" value="This is first String."/>
<c:set var="string2" value="${fn:substringAfter(string1, 'is')}" />

<p>Final sub string : ${string2}</p>

</body>
</html>

This would produce following result:

Final sub string : is first String.



<<Previous <<   || Index ||   >>Next >>






JSTL fn:substring() Function

JSTL Substring function is used to get the substring of target string. This function takes 3 parameters.

First parameter in JSTL Substring function is the target String from which you want to derive the substring.

Second parameter in JSTL Substring function is the start index. Character at this index is included in the substring.

Third parameter in JSTL Substring function is the end index. Character at this index is excluded from the substring. Substring will end at the previous index.

The fn:substring() function returns a subset of a string specified by start and end indices.

Syntax:
The fn:substring () function has following syntax:
java.lang.String substring(java.lang.String, int, int)

Example:

Following is the example to explain the functionality of this function:

<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/functions" prefix="fn" %>
<html>
<head>
<title>Using JSTL Functions</title>
</head>
<body>

<c:set var="string1" value="This is first String."/>
<c:set var="string2" value="${fn:substring(string1, 5, 15)}" />

<p>Final sub string : ${string2}</p>

</body>
</html>

This would produce following result:

Final sub string : is first S




<<Previous <<   || Index ||   >>Next >>






JSTL fn:startsWith() Function

JSTL startsWith Function returns true or false based on condition evaluation. The fn:startsWith() function determines whether an input string starts with a specified substring.

Syntax:
The fn:startsWith() function has following syntax:
boolean startsWith(java.lang.String, java.lang.String)

JST StartsWith Function Example-
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" 
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>JSTL StartsWith Function Example</title>
</head>
<body>
  <c:set var="array1" value="${fn:startsWith('Hi My name is Dinesh', 'Hi ')}">
</body>
</html>

Output: true

As you can see above, fn:startsWith() takes two Strings as input parameters.

First parameter in JSTL startsWith function is the target String which needs to be validated.

Second parameter in JSTL startsWith function is the specified string that needs to be matched with starting character sequence of the target string.

If target string starts with same character sequence like the specified string than JSTL startsWith function returns true otherwise returns false.





<<Previous <<   || Index ||   >>Next >>






JSTL fn:split() Function

In this tutorial you will learn about the JSTL fn split function.

JSTL split Function returns the array of String after splitting the target string with specified separator.

Sometimes it is required to split the string into a various part. For this JSTL function Library provides a function named split() that splits the string using a delimiter. This function splits a string into an array of string.

Syntax:
The fn:split() function has following syntax:
java.lang.String[] split(java.lang.String, java.lang.String)

JST Split Function Example-
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" 
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>JSTL Split Function Example</title>
</head>
<body>
  <c:set var="array1" value="${fn:split('Hi My name is Dinesh', ' ')}">
</body>
</html>

Output: [Hi,My,name,is,Dinesh]

As you can see above, fn:split() takes a String as input parametes and returns the array of String after splitting it based on separator specified in 2nd parameter.





<<Previous <<   || Index ||   >>Next >>






JSTL fn:replace() Function

In this tutorial you will learn about the JSTL replace function.
JSTL Replace function is used to replace the string with another string.
This function replaces a string with the other string. It replaces all occurrences of a string.

Syntax:
The fn:replace () function has following syntax:
boolean replace(java.lang.String, java.lang.String, java.lang.String)
JSTL Replace Function Example-
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" 
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>JSTL Replace Function Example</title>
</head>
<body>
  <c:out value="${fn:replace('Hello Dinesh','Dinesh','Rajput')}"></c:out>
</body>
</html>
Output: Hello Rajput

As you can see above, JSTL Replace function takes 3 parameters.

First parameter in JSTL replace function is the string which needs to be modified.

Second parameter in JSTL replace function is the target substring which needs to be replaced by .

Third parameter in JSTL replace function is the string which needs to be inserted inplace of second parameter.



<<Previous <<   || Index ||   >>Next >>






JSTL fn:length() Function

In this tutorial you will learn about the JSTL fn length function.

JSTL Length function is used to get the size of a collection or length of a String.

length function, however the size() method is defined by the java.util.Collection interface can not be accessed through the JSP EL because the JavaBeans component design pattern is not confirmed by it for the properties, is used for finding out the how many characters (including the white space) are there in a string or how many items are there in a collection.

Syntax :
The fn:length() function has following syntax:
int length(java.lang.Object)

JSTL Length Function Example:
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" 
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>JSTL Length Function Example</title>
</head>
<body>
  <c:out value="${fn:length('Hello')}"></c:out>
</body>
</html>

Output: 5

As you can see above, JSTL Length Function takes 'Hello' string as an input parameter and will return 5 as a result.
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/functions" prefix="fn" %>
<html>
<head>
<title>Using JSTL Functions</title>
</head>
<body>

<c:set var="string1" value="This is first String."/>
<c:set var="string2" value="This is second String." />

<p>Length of String (1) : ${fn:length(string1)}</p>
<p>Length of String (2) : ${fn:length(string2)}</p>

</body>
</html>

This would produce following result:

Length of String (1) : 21
Length of String (2) : 22


<<Previous <<   || Index ||   >>Next >>






JSTL fn:join() Function

JSTL Join function is used to concatenate the strings contained in an array with the specified string.

"fn:join" is a string function of JSP Standard Tag Library(JSTL). This function is used to return string with all the elements of an array separated by a "separator".

The fn:join() function concatenates all the elements of an array into a string with a specified separator.

Syntax:
The fn:join() function has following syntax:
String join (java.lang.String[], java.lang.String)

JST Join Function Example-

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" 
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>JSTL Join Function Example</title>
</head>
<body>
  <c:set var="array1" value="${fn:split('I am a good programmer in java', ' ')}">
  <c:out value="${fn:join(array1, '-')}"></c:out>
</body>
</html>

Output: I-am-a-good-programmer-in-java

As you can see above, fn:join() takes an array of String as input parameters and returns the result of concatenation of strings with another string specified as 2nd parameter in JSTL join function.


<<Previous <<   || Index ||   >>Next >>





JSTL fn:indexOf() Function

JSTL indexOf function returns the index of specified String in target String. The fn:indexOf() function returns the index within a string of a specified substring.

Syntax:
The fn:indexOf() function has following syntax:
int indexOf(java.lang.String, java.lang.String)

JSTL indexOf Function Example-
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" 
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>JSTL IndexOf Function Example</title>
</head>
<body>
  <c:out value="${fn:indexOf('Hello', 'llo')}"></c:out>
</body>
</html>

Output: 2

As you can see above, fn:indexOf() takes 'Hello' string as an input parameter and return the index of 'llo' as a result. If it does not find the specified String, it will return -1.

<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/functions" prefix="fn" %>
<html>
<head>
<title>Using JSTL Functions</title>
</head>
<body>

<c:set var="string1" value="This is first String."/>
<c:set var="string2" value="This <abc>is second String.</abc>"/>

<p>Index (1) : ${fn:indexOf(string1, "first")}</p>
<p>Index (2) : ${fn:indexOf(string2, "second")}</p>

</body>
</html>

This would produce following result:

Index (1) : 8
Index (2) : 13



<<Previous <<   || Index ||   >>Next >>





JSTL fn:escapeXml() Function

In this section we will learn how to use <fn:escapeXml> Tag of JSTL. This tag is used to escape characters that could be interpreted as XML markup. This takes string type as argument and also returns string type.

JSTL escapeXml function is used to escape XML markup characters. EscapeXML function takes only one parameter of type String.

Syntax :
java.lang.String escapeXml(java.lang.String)

JSTL escapeXML function Example:
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/functions" prefix="fn" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>JSTL EscapeXML Example</title>
</head>
<body>
    <c:set var="testString" value="This is <b>JSTL escapeXML example</b> in this JSTL function tutorial."></c:set>
 Without escapeXml function : ${testString}
 <br/>
 Using escapeXml function : ${fn:escapeXml(testString)}
</body>
</html>

Output:

Without escapeXml function : This is JSTL escapeXML example in this JSTL function tutorial.

Using escapeXml function : This is <b>JSTL escapeXML example</b> in this JSTL function tutorial.

As you can see above, fn:escapeXml() takes string parameter and returns string after escaping XML markup characters. You can see the difference between output with and without escapeXml function.



<<Previous <<   || Index ||   >>Next >>





JSTL fn:endsWith() Function

In this tutorial you will learn about the JSTL fn endsWith function. JSTL endsWith Function returns true or false based on condition evaluation.

To manipulate the string that ends with a specific characters or string in JSP, JSTL provides a function endsWith(). This function helps in to work with the suffix associated with a string.

Syntax:

The fn:endsWith() function has following syntax:
boolean endsWith(java.lang.String, java.lang.String)


JSTL fn endsWith Example-
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" 
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>JSTL EndsWith Function Example</title>
</head>
<body>
  <c:set var="array1" value="${fn:endsWith('Hi My name is Dinesh', 'Dinesh')}">
</body>
</html>

Output: true

As you can see above, fn:endsWith() takes two Strings as input parameters.

First parameter in JSTL endsWith function is the target String which needs to be validated.

Second parameter in JSTL endsWith function is the specified string that needs to be matched with starting character sequence of the target string.

If target string ends with same character sequence like the specified string than JSTL endsWith function returns true otherwise returns false.

<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/functions" prefix="fn" %>
<html>
<head>
<title>Using JSTL Functions</title>
</head>
<body>

<c:set var="theString" value="I am a good programmer in java"/>

<c:if test="${fn:endsWith(theString, 'java')}">
   <p>String ends with java<p>
</c:if>

<c:if test="${fn:endsWith(theString, 'CPP')}">
   <p>String ends with CPP<p>
</c:if>
</body>
</html>

This would produce following result:

String ends with java




<<Previous <<   || Index ||   >>Next >>





JSTL fn:containsIgnoreCase() Function

JSTL containsIgnoreCase Function returns true or false based on condition evaluation. "fn:containsIgnoreCase This function is used to check if the "string" contains the specified "subtsring" no matter the case of the string and substring. The function returns true if the substring is present and false if not.

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

JST containsIgnoreCase Function Example:
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" 
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>JSTL containsIgnoreCase Function Example</title>
</head>
<body>
  <c:out value="${fn:containsIgnoreCase('dineshonjava', 'DIneS')}"/>
</body>
</html>

Output: true

As you can see above, fn:containsIgnoreCase() takes two Strings as input parameters.

First parameter in JSTL containsIgnoreCase function is the String which needs to be checked to see if it contains second String, ignoring the cases.

<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>  
    <%@ taglib uri="http://java.sun.com/jsp/jstl/functions" prefix="fn" %>  
    <html>  
    <head>  
    <title>Using JSTL Functions</title>  
    </head>  
    <body>  
      
    <c:set var="theString" value="I am a good programmer"/>  
      
    <c:if test="${fn:containsIgnoreCase(theString, 'good')}">  
       <p>Found good string<p>  
    </c:if>  
      
    <c:if test="${fn:containsIgnoreCase(theString, 'GOOD')}">  
       <p>Found GOOD string<p>  
    </c:if>  
      
    </body>  
    </html>  

Output:
Found good string
Found GOOD string



<<Previous <<   || Index ||   >>Next >>





JSTL fn:contains() Function

JSTL Contains function is used to check whether target string contains specified string or not. contains function is used for checking whether the specified substring is contained by the string or not.

Syntax :
boolean contains(String, String)
JSTL Contains Function Example:
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" 
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>JSTL Contains Function Example</title>
</head>
<body>
  <c:out value="${fn:contains('Hello', 'el')}"></c:out>
</body>
</html>

Output: true

As you can see above, fn:contains() takes 'Hello' string as an input parameter and check if it contains 'el' and will return true as a result.

JSTL Contains Function Example:
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/functions" prefix="fn" %>
<html>
<head>
<title>Using JSTL Functions</title>
</head>
<body>

<c:set var="theString" value="I am a good programmer"/>

<c:if test="${fn:contains(theString, 'good')}">
   <p>Found good string<p>
</c:if>

<c:if test="${fn:contains(theString, 'GOOD')}">
   <p>Found GOOD string<p>
</c:if>

</body>
</html>

Output: Found good string



<<Previous <<   || Index ||   >>Next >>