java JSP 字符串格式化截断

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6418095/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 15:48:36  来源:igfitidea点击:

JSP String formatting Truncate

javastringjsp

提问by Justin Wrobel

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

有谁知道如何使用标记库截断 JSP 中的字符串?我打算使用Jakarta Taglibs,但它说它已经退休,因为:

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.

随着 JSTL 的出现,许多库的核心特性已经标准化,对这些库的需求减少了。因此,大部分 Taglibs 代码库都进入了维护模式。

回答by Etienne Neveu

You can use the JSTL substring function:

您可以使用JSTL 子字符串函数

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

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

回答by Sid

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

您可以简单地在 JSP 中使用 Java Scriptlets。
例如

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

    String s = "This is a String";

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