Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> การเขียนโปรแกรม

วิธีตรวจสอบ Substring ใน String ด้วย JSTL fn:contains


ฟังก์ชัน fn:contains() ตรวจสอบว่า String มี Substring ที่ระบุหรือไม่ โดยสนใจตัวพิมพ์เล็ก/ใหญ่ (Case-sensitive)

Syntax

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

ตัวอย่างการใช้งาน

<%@ taglib uri="https://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ taglib uri="https://java.sun.com/jsp/jstl/functions" prefix="fn" %>
<html>
<head>
<title>Using JSTL Functions</title>
</head>
<body>
<c:set var="theString" value="I am a test String"/>
<c:if test="${fn:contains(theString, 'test')}">
    <p>พบ test string</p>
</c:if>
<c:if test="${fn:contains(theString, 'TEST')}">
    <p>พบ TEST string</p>
</c:if>
</body>
</html>

ผลลัพธ์

พบ test string

เงื่อนไขแรก true เพราะ "test" ตรงกับ "test" ใน String เงื่อนไขที่สอง false เพราะ "TEST" ไม่ตรงกับ "test" (Case-sensitive)