當前位置: 首頁>>編程示例 >>用法及示例精選 >>正文


JSTL fn:substringBefore()用法及代碼示例

在 JSTL 中,fn:substringBefore()函數主要用於返回字符串中特定子字符串之前的子集。此函數提取給定輸入字符串中指定分隔符之前的子字符串。在本文中,我們將討論語法、參數和示例fn:substringBefore()函數。

  • fn:substringBefore() 函數是 JSTL 核心標記庫的一部分。
  • 該函數簡化了 JSP 應用程序中的字符串操作。

語法為fn:substringBefore()函數:

${fn:substringBefore(string, delimiter)}

  • ${fn:substringBefore(..)}:這是用於提取字符串子集的 JSTL 表達式。
  • string: 這是我們需要從中提取子字符串的輸入字符串參數。
  • delimiter: 這是定義要提取的子字符串結尾的分隔符屬性。

JSTL fn:substringBefore() 函數示例

在此示例中,我們將了解如何使用 JSTL fn:substringBefore() 函數查找字符串的長度。

HTML


<%@ page contentType="text/html;charset=UTF-8" language="java" %> 
<%@ 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:substringBefore() Example</title> 
</head> 
<body> 
<c:set var="inputString" value="GeeksforGeeks is Good"/> 
<p>Original String: ${inputString}</p> 
<p>Substring before "is": ${fn:substringBefore(inputString, 'is')}</p> 
</body> 
</html> 

輸出:

Original String: GeeksforGeeks is Good
Substring before "is": GeeksforGeeks

上述程序的輸出屏幕:

Output of Example of JSTL fn:substringBefore() Function

上述程序的解釋:

  • 我們已經用字符串值“初始化了 inputString 變量”GeeksforGeeks很好”.
  • 然後,通過使用fn:substringBefore()函數中,我們提取子字符串出現之前的子字符串為“”。
  • 所以在子字符串之前,輸出是GeeksforGeeks我們使用 HTML 的 <p> 標簽將其打印在屏幕上。

相關用法


注:本文由純淨天空篩選整理自anjalibo6rb0大神的英文原創作品 JSTL fn:substringBefore() Function。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。