JSTL Function fn:startsWith Example
- Details
- Written by Nam Ha Minh
- Last Updated on 30 August 2019   |   Print Email
This post helps you understand how to use the <fn:startsWith> tag in the JSTL function tags library with code example.
The fn:startsWith function tests whether a given string starts with a given prefix. This function returns boolean value.
JSTL <fn:startsWith> Syntax:
<c:if test="${fn:startsWith(<var containing source string>, <prefix to search>)}">
...
</c:if>
JSTL <fn:startsWith> Example:
The following code example searches for a prefix www in the string www.codejava.net.
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/functions" prefix="fn"%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>fn:startsWith Demo</title>
</head>
<body>
<h1>fn:startsWith Demo</h1>
<c:set var="stringToSearch"
value="www.CodeJava.net" />
<c:out value="${stringToSearch}"/>
<c:if test="${fn:startsWith(stringToSearch, 'www')}">
<p>www.CodeJava.net starts with www</p>
</c:if>
</body>
</html>
Output:

Other JSTL Function Tags:
contains | containsIgnoreCase | endsWith | escapeXml | indexOf | join | length | replace | split | substring | substringAfter | substringBefore | toLowercase | toUpperCase | trim
About the Author:
Nam Ha Minh is certified Java programmer (SCJP and SCWCD). He began programming with Java back in the days of Java 1.4 and has been passionate about it ever since. You can connect with him on Facebook and watch his Java videos on YouTube.
Comments