Java jstl function substring
The fn:substring
function is a JSTL function that returns a substring of a specified length from a given string, starting at a specified index. The function takes three arguments: the input string, the starting index, and the length of the substring.
The syntax of the fn:substring
function is as follows:
${fn:substring(string, start, length)}
where string
is the input string, start
is the starting index (0-based) of the substring, and length
is the length of the substring.
For example, the following expression returns a substring of length 3 starting from the 2nd character of the input string "hello":
${fn:substring("hello", 1, 3)}
This expression returns the string "ell".
You can also use variables instead of literals for the arguments. For example, if you have a variable text
containing a string, a variable start
containing the starting index, and a variable length
containing the length of the substring, you can get the substring using the following expression:
${fn:substring(text, start, length)}
This expression returns a substring of length length
starting from the start
index of the string stored in the text
variable.
Note that the fn
tag library must be imported at the beginning of the JSP page using the following tag:
<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions" %>