Example of JSTL fn:substringBefore() Function
In this example, we will see how to find the length of a string using the JSTL fn:substringBefore() Function.
HTML
<%@ page contentType="text/html;charset=UTF-8" language="java" %> <%@ 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 >JSTL fn:substringBefore() Example</ title > </ head > < body > < c:set var = "inputString" value = "w3wiki is Good" /> < p >Original String: ${inputString}</ p > < p >Substring before "is": ${fn:substringBefore(inputString, 'is')}</ p > </ body > </ html > |
Output:
Original String: w3wiki is Good
Substring before "is": w3wiki
Output Screen of the above Program:
Explanation of the above Program:
- We have initialized the inputString variable with the string value “w3wiki is Good”.
- Then, by using the fn:substringBefore() function, we extract the substring before the occurrence of the substring as “is“.
- So before the substring, the output is w3wiki which we are printing on screen by using the <p> tag of HTML.
JSTL fn:substringBefore() Function
In JSTL, the fn:substringBefore() function is mainly used to return the subset of the string before a specific substring. This function extracts the substring before a specified delimiter in the given input string. In this article, we will discuss about the syntax, parameters, and example of the fn:substringBefore() function.
- The fn:substringBefore() function is part of the JSTL core tag library.
- The function simplifies the string manipulation in JSP applications.
Syntax of fn:substringBefore() function:
${fn:substringBefore(string, delimiter)}
- ${fn:substringBefore(..)}: This is the JSTL expression for extracting a subset of a string.
- string: This is the input string parameter from which we need to extract the substring.
- delimiter: This is the delimiter attribute that defines the end of the substring to be extracted.