JSP String formatting Truncate

2020-08-27 05:06发布

问题:

Does anyone know how to truncate a string in a JSP using a tag library? I was going to use Jakarta Taglibs but it says that it has been retired because:

With the advent of JSTL, the core features of many of the libraries had been standardized and the need for these libraries diminished. As such, much of the Taglibs codebase moved into maintenance mode.

回答1:

You can use the JSTL substring function:

<%@ taglib uri="http://java.sun.com/jsp/jstl/functions" prefix="fn" %>

${fn:substring(myVariable, 0, 5)}


回答2:

You can simply use the Java Scriptlets in JSP.
e.g.

<%
    int i = 5; // This is the number of characters to truncate

    String s = "This is a String";

    s = s.substring(0, i);
%>


标签: java string jsp