jstl fn:startsWith()函式

2019-10-16 22:13:01

fn:startsWith()函式確定輸入的字串是否以指定的子字串開頭。

語法

fn:startsWith()函式具有以下語法 -

boolean startsWith(java.lang.String, java.lang.String)

範例

以下範例將顯示如何使用fn:startsWith()函式,編寫一個JSP檔案:fn_startsWith.jsp 如下所示:

<%@page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%@taglib uri="http://java.sun.com/jsp/jstl/functions" prefix="fn"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>jstl fn:startsWith()函式</title>
</head>
<body>
    <div style="margin: auto; width: 90%">
        <h2>fn:startsWith()函式範例</h2>
        <hr />
        <c:set var="string" value="Title: This is the tutorials from tw511.com." />
        <p>原字串:<c:out value="${string }"/></p>
        <c:if test="${fn:startsWith(string, 'First')}">
            <p>String starts with 'First'</p>
        </c:if>
        <c:if test="${fn:startsWith(string, 'Title')}">
            <p>String starts with 'Title'</p>
        </c:if>
    </div>
</body>
</html>

執行上述專案程式碼,得到以下結果如下 -