JSTL fn:indexOf 函数类似于 Java 中的 indexOf() 方法,用于返回字符串在指定字符串中的开始位置(第一次出现处的索引)。
JSP fn:indexOf 函数的语法如下:
- int fn:indexOf(String sourceStr, String specialStr)
其中,sourceStr 表示源字符串,specialStr 为指定的字符串。
下面为 fn:indexOf 函数的简单实例。
- <%@ 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>
- <html>
- <head>
- <title>城东书院(www.cdsy.xyz)</title>
- </head>
- <body>
- <c:set var="str" value="This is first String." />
- first首次出现索引:${fn:indexOf(str, "first")}
- </body>
- </html>
页面输出内容如下: