JSTL fn:substring() Function
In JSTL, the fn:substring() function is used to extract or retrieve the part of a given input or specified string, by starting from the start index to the end index [optional]. This function is mainly used for string manipulation tasks by many developers.
In this article we will explore the Syntax along with the Parameters of the JSTL fn:substring() function, also we will see the practical implementation of this function in terms of examples.
Syntax of JSTL fn:substring() Function
${fn:substring(string, startIndex ,endIndex)}
Where,
- string: input string from which the part of the substring will be extracted.
- startIndex: initial position or the starting index (inclusive) for the substring. This means that the extracting will start from this index.
- endIndex: ending index (exclusive) which is optional for the substring.
Note: If this index is not included, then the substring continues to the end of the input string
Example of fn:substring()
In this example, we will discuss the use of fn:substring(), to generate different substrings.
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:substring() Example</ title > </ head > < body > < c:set var = "mainString" value = "w3wiki" /> < h3 >Main String: ${mainString}</ h3 > < p >Substring from index 5 to the end: ${fn:substring(mainString, 5)}</ p > < p >Substring from index 0 to 5: ${fn:substring(mainString, 0, 5)}</ p > </ body > </ html > |
Output:
Explanation of the above Program:
- mainString is initialized with the value “w3wiki“.
- The first <p> tag mainly extracts the substring from index 5 to the end of the main string, result string is forBeginner.
- In the second <p> we are extracting a substring from the index 0 to 5 (excluding index 5) of the main string, the result printed is Beginner.
Contact Us