[ Team LiB ] Previous Section Next Section

Functions

Version 2.0 of the JSP specification introduced the notion of functions to the JSP expression language. JSTL includes a number of standard functions, most of which are common string manipulation functions.

The fn:contains Function

The fn:contains function returns true if a string contains a specified substring. The format of the function is


fn:contains(string_to_search, string_to_find)

For example, fn:contains("Abracadabra", "brac") returns true, and fn:contains("Abracadabra", "kazam") returns false. If either argument is null, it is treated as an empty string.

The fn:containsIgnoreCase Function

Like the fn:contains function, the fn:containsIgnoreCase function returns true if a string contains a specified substring. The fn:containsIgnoreCase function, however, ignores the case of the strings when performing the search. The format of the function is


fn:containsIgnoreCase(string_to_search, string_to_find)

For example, fn:containsIgnoreCase("Abracadabra", "BRAC") returns true, and fn:containsIgnoreCase("Abracadabra", "kAzAm") returns false. If either argument is null, it is treated as an empty string.

The fn:endsWith Function

The fn:endsWith function returns true if a string ends with a specified substring. The format of the function is


fn:endsWith(string_to_search, string_to_find)

For example, fn:endsWith("Abracadabra", "abra") returns true, whereas fn:endsWith("Abracadabra", "Abra") returns false. If either argument is null, it is treated as an empty string.

The fn:escapeXml Function

The fn:escapeXml function returns an XML-safe equivalent of another string. The function performs the same operations as the escapeXml attribute in the <c:out> tag and replaces the characters <, >, &, ' (apostrophe/single quote), and " (double quote) with their XML equivalents (&lt;, &gt;, &amp;, &#039;, and &#034;, respectively). The format of the function is


fn:escapeXml(string)

For example, fn:escapeXml("<Hello>") returns "&lt;Hello&gt;".

The fn:indexOf Function

The fn:indexOf function returns the first location of a substring within the specified string (similar to the indexOf function in java.lang.String. The format of the function is


fn:indexOf(string_to_search, string_to_find)

For example, fn:indexOf("Abracadabra", "brac") returns 1, and fn:contains("Abracadabra", "kazam") returns -1. If either argument is null, it is treated as an empty string. Unlike the indexOf function in the String class, fn:indexOf does not let you specify the start location for the search—the function always starts at position 0.

The fn:join Function

The fn:join function joins each string in an array of strings together into a single string, separated by a given specifier. The format is


fn:join(strings[], separator)

For example, if the array stooges contains "Moe", "Larry", "Curly", and "Shemp", fn:join(stooges, ";") returns "Moe;Larry;Curly;Shemp". If the separator is null or an empty string, there is no separator. For example, fn:join(stooges, "") returns "MoeLarryCurlyShemp".

The fn:length Function

The fn:length function returns the length of a collection or a string. The format is


fn:length(collection_or_string)

Invoking fn:length on a null value results in a length of 0.

The fn:replace Function

The fn:replace function replaces all occurrences of one substring in a specified string with a replacement string and returns the result. The format is


fn:replace(original_string, string_to_find, replacement_string)

For example, fn:replace("Abracadabra", "bra", "ZZZ") returns "AZZZcadaZZZ". If any of the arguments are null, they are treated as empty strings.

The fn:split Function

The fn:split function splits a string into an array of substrings based on a set of separators. The function works like the StringTokenizer class, only it returns an array of token strings rather than an iterator. The format is


fn:split(string, token_string)

For example, fn:split("Moe,Larry,Curly", ",") returns a three-element array containing "Moe", "Larry", and "Curly".

The fn:startsWith Function

The fn:startsWith function returns true if a string starts with a specified substring. The format of the function is


fn:startsWith(string_to_search, string_to_find)

For example, fn:startsWith("Abracadabra", "Abra") returns true, and fn: startsWith ("Abracadabra", "dabra") returns false. If either argument is null, it is treated as an empty string.

The fn:substring Function

The fn:substring function returns the portion of a string from a given starting position up to, but not including, a given ending position. The format is


fn:substring(string, starting_pos, ending_pos)

For example, fn:substring("Abracadabra", 2, 5) returns "rac". If the end index is less than 0 or greater than the length of the string, it is treated as the length of the string. If the starting position is less than 0, it is treated as 0.

The fn:substringAfter Function

The fn:substringAfter function searches a string for a specific substring and returns the portion of the string immediately after the substring. The format is


fn:substringAfter(string_to_search, string_to_find)

For example, fn:substringAfter("http://www.wutka.com", "http://") returns "www.wutka.com".

The fn:substringBefore Function

The fn:substringBefore function searches a string for a specific substring and returns the portion of the string immediately before the substring. The format is


fn:substringBefore(string_to_search, string_to_find)

For example, fn:substringBefore("http://www.wutka.com", ":/") returns "http".

The fn:toLowerCase Function

The fn:toLowerCase function returns the lowercase equivalent of a string. The format is


fn:toLowerCase(string)

For example, fn:toLowerCase("AbRaCaDaBrA") returns "abracadabra".

The fn:toUpperCase Function

The fn:toUpperCase function returns the uppercase equivalent of a string. The format is


fn:toUpperCase(string)

For example, fn:toUpperCase("AbRaCaDaBrA") returns "ABRACADABRA".

The fn:trim Function

The fn:trim function removes any leading or trailing whitespace from a string and returns the result. The format is


fn:trim(string)

For example, fn:trim(" Hello ") returns "Hello".

    [ Team LiB ] Previous Section Next Section