Recently, when using jstl1.0, you often need to modify the string on the page. I found a well-written string on the Internet. Let's learn from it:
Jstl uses expressions to simplify Page code. This is very convenient for some standard methods, such as the getter/setter method of bean, to access request parameters, context, and session data, however, in practical applications, we often need to call some methods of objects on the page. For example, when I need to call the length method of a string to obtain the length of a string, in the previous development process, we had to convert the object to the string class first, and then call its length method. Such code is cumbersome and error-prone.
Therefore, jstl has several built-in methods for string operations, which can be used directly in expressions, greatly simplifying the code and providing code readability. A function must be used in the expression of jstl. The format is as follows:
$ {Ns: methodname (ARGs ....)}
Before using these functions, you must introduce the standard function declaration in JSP.
<% @ Taglib prefix = "FN" uri = "http://java.sun.com/jsp/jstl/functions" %>
The following is a list of methods self-contained in jstl and their descriptions.
Function Name |
Function Description |
Example |
Contains |
Determines whether a string contains another string. |
<C: If test = "$ {fn: Contains (name, searchstring)}"> |
Containsignorecase |
Determines whether the string contains another string (case-insensitive) |
<C: If test = "$ {fn: containsignorecase (name, searchstring)}"> |
Endswith |
Judge whether the string ends with another string |
<C: If test = "$ {fn: endswith (filename,". txt ")}"> |
Escapexml |
Convert some characters to XML Representation. For example, <character should be converted to <; |
$ {Fn: escapexml (Param: INFO )} |
Indexof |
Position of the substring in the parent string |
$ {Fn: indexof (name ,"-")} |
Join |
Combines the data in the array into a new string and uses the specified character lattice |
$ {Fn: Join (array ,";")} |
Length |
Returns the length of a string or the size of an array. |
$ {Fn: length (shoppingcart. Products )} |
Replace |
Replace the specified character in the string |
$ {Fn: Replace (text, "-", "& #149 ;")} |
Split |
Splits a string by a specified character. |
$ {Fn: Split (customernames ,";")} |
Startswith |
Determines whether a string starts with a substring. |
<C: If test = "$ {fn: startswith (products. ID," 100-")}"> |
Substring |
Obtain substrings |
$ {Fn: substring (ZIP, 6,-1 )} |
Substringafter |
Obtains the substring starting from the position of a character. |
$ {Fn: substringafter (ZIP ,"-")} |
Substringbefore |
Obtains the substring from the start to the position of a character. |
$ {Fn: substringbefore (ZIP ,"-")} |
Tolowercase |
Lowercase |
$ {Fn. tolowercase (product. Name )} |
Touppercase |
Convert to uppercase characters |
$ {Fn. uppercase (product. Name )} |
Trim |
Removes spaces before and after a string. |
$ {Fn. Trim (name )} |
Reference: http://blog.163.com/guixl_001