This post helps you understand how to use the <fn:substringBefore> tag in the JSTL function tags library with code example.

The fn:substringBefore function returns a string before the specified substring.

 

JSTL <fn:substringBefore> Syntax:

${fn:substringBefore(<string>, <substring>)}

 

JSTL <fn:substringBefore> Example:

The following JSP example outputs a string before the substring .net

<%@ 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"%>
<html>
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
    <title>fn:substringBefore Demo</title>
  </head>
  <body>
   <h1>fn:substringBefore Demo</h1>
   <c:set var="text" value="www.CodeJava.net is great source of information."/>
   <c:set var="website" value="${fn:substringBefore(text,'.net')}" />
   Full Text: <strong><c:out value="${text}"/></strong><br/><br/>
   Text Before Substring .net <strong><c:out value="${website}"/></strong><br/><br/>	
  </body>
</html>

 

Output:

JSTL function fn-substringBefore

 

Other JSTL Function Tags:

contains  |  containsIgnoreCase  |  endsWith |  escapeXml  |  indexOf  |  join  |  length  |  replace  |  split  |  startsWith  |  substring  |  substringAfter  |  toLowercase  |  toUpperCase  |  trim


About the Author:

is certified Java programmer (SCJP and SCWCD). He started programming with Java in the time of Java 1.4 and has been falling in love with Java since then. Make friend with him on Facebook and watch his Java videos you YouTube.



Add comment