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 >>





No comments:

Post a Comment