BeginnersBook

  • Home
  • Java
    • Java OOPs
    • Java Collections
    • Java Examples
  • C
    • C Examples
  • C++
    • C++ Examples
  • DBMS
  • Computer Network
  • Python
    • Python Examples
  • More…
    • jQuery
    • Kotlin
    • WordPress
    • SEO
    • JSON
    • JSP
    • JSTL
    • Servlet
    • MongoDB
    • XML
    • Perl

fn:trim() and fn:startsWith() JSTL Functions

By Chaitanya Singh | Filed Under: JSTL

In this post we are discussing two functions which operates on strings. These functions are fn:trim() and fn:startsWith(). Function fn:trim() removes spaces from beginning and end of a string and fn:startsWith() checks whether the specified string is a prefix of given string.

JSTL fn:trim() Function

It removes the space characters from start and end of the provided string.

Syntax:

String fn:trim(String input)

The function returns the string after removing the white spaces from start and end of the input String.

Example

In this example we have a string which has few space characters appended at the start and end of the string “mymsg” and we are truncating those spaces using the function.

<%@ 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>fn:trim() example in JSTL</title>
</head>
<body>
<c:set var="mymsg" value=" This is the test String       "/>
${fn:trim(mymsg)}
</body>
</html>

Output Screenshot:

trim-string-example

fn:startsWith() function in JSTL

It checks whether the given string starts with a particular string value.

Syntax:

boolean fn:startsWith(String input, String prefix)

This function returns a boolean value. It gives true when the string starts with the given prefix else it returns false.

Example

Here we have one long string and two substrings of it and we are checking whether the string starts with any of those substrings.

<%@ 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>fn:startsWith example</title>
</head>
<body>
<c:set var="mymsg" value="Example of JSTL function"/>
The string starts with "Example": ${fn:startsWith(mymsg, 'Example')}
<br>The string starts with "JSTL": ${fn:startsWith(mymsg, 'JSTL')}
</body>
</html>

Output: PFB the output screenshot for above example.

check-string-starting-from-given-string

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

JSTL Core Tags

  • c:out
  • c:set
  • c:remove
  • c:if
  • c:choose
  • c:when
  • c:otherwise
  • c:catch
  • c:import
  • c:forEach
  • c:forTokens
  • c:param
  • c:url
  • c:redirect
JSTL Functions
  • fn:contains()
  • fn:containsIgnoreCase()
  • fn:indexOf()
  • fn:escapeXML()
  • fn:join() & fn:split()
  • fn:length()
  • fn:startsWith()
  • fn:endsWith()
  • fn:substring()
  • fn:substringAfter()
  • fn:substringBefore()
  • fn:trim()
  • fn:toUpperCase()
  • fn:toLowerCase()
  • fn:replace()

Copyright © 2012 – 2022 BeginnersBook . Privacy Policy . Sitemap