Skip to content

Latest commit

 

History

History
41 lines (29 loc) · 1.41 KB

32.md

File metadata and controls

41 lines (29 loc) · 1.41 KB

fn:replace() - JSTL 函数

原文: https://beginnersbook.com/2013/12/fn-replace-jstl-function/

它在输入中搜索一个字符串,并用提供的字符串替换它。以下是fn:replace()函数的基本语法。

语法

String fn:replace(String input, String search_for, String replace_with)

三个字符串参数和返回类型也是String。它搜索input中的search_for字符串,并将其替换为replace_with字符串。如果未找到该字符串,则返回实际输入。

:它区分大小写处理。

在这个例子中,我们在两个输入字符串上使用fn:replace()函数。

<%@ 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:replace() example</title>
</head>
<body>
<c:set var="author" value="Chaitanya Singh"/>
<c:set var="randomstring" value="abc def abc ghi ABC"/>
${fn:replace(author, "Chaitanya", "Rahul")} 
${fn:replace(randomstring, "abc", "hello")}
</body>
</html>

输出:

观察输出,你会发现randomstring变量中的"ABC"保持不变,而其他"abc"子串被"hello"替换。它发生的原因是区分大小写,我们在上面的例子中替换了小写的"abc"

fn-replace-example