Freemarker built-in functions, numbers, strings, and date formatting

Source: Internet
Author: User
Tags date1

I. Sequence built-in functions

1. sequence? First returns the first value of sequence.

2. sequence? Last returns the last value of sequence.

3. sequence? Reverse reverses the existing sequence of sequence, that is, reverse order.

4. sequence? Size returns the sequence size.

5. sequence? Sort converts the objects in the sequence into strings and sorts them sequentially.

6. sequence? Sort_by (value) is sorted by the attribute value of the object in sequence.

Ii. built-in Hash Functions

1. Hash? Keys returns all keys in the hash, and the returned result is sequence.

2. Hash? Values returns all values in the hash. The returned result is sequence.

For example:

<# Assign user = {"name": "hailang", "sex": "man"}>

<# Assign keys = user? Keys>

<# List keys as key>

$ {Key }=$ {user [Key]}

</# List>

Iii. Operate string functions

1. substring (START, end) captures substrings from a string

Start: The index starting from the substring. Start must be greater than or equal to 0 and less than or equal to end.

End: Specifies the length of a substring. The end value must be greater than or equal to 0 and smaller than or equal to the string length. If this parameter is omitted, the default value is the string length.

Example:

$ {'Str '? Substring (0)}, returns Str

$ {'Str '? Substring (1)}, returns tr

$ {'Str '? Substring (2)}. The result is R.

$ {'Str '? Substring (3)}, and the result is

$ {'Str '? Substring (0, 0)}, and the result is

$ {'Str '? Substring ()}, the result is S

$ {'Str '? Substring ()}, the result is St

$ {'Str '? Substring ()}, returns Str

2. cap_first converts the first letter of the word in the string to uppercase.

$ {'Str '? Cap_first}. The result is str.

3. uncap_first converts the first letter of the word in the string to lowercase.

$ {'Str '? Cap_first}. The result is str.

4. capitalize converts the first letter of all words in the string to uppercase.

$ {'Str '? Capitalize}. The result is str.

5. Date, time, datetime convert string to date

For example:

<# Assign date1 = "2009-10-12 "? Date ("yyyy-mm-dd")>

<# Assign date2 = "9:28:20 "? Time ("HH: mm: SS")>

<# Assign date3 = "9:28:20 "? Time ("HH: mm: SS")>

$ {Date1}. The result is.

$ {Date2}. The result is 9:28:20.

$ {Date3}, the result is 9:28:20

Note: If the specified string format is incorrect, an error is thrown.

6. ends_with: determines whether a string ends with a substring and returns a Boolean value.

$ {"String "? Ends_with ("ing ")? String}, returns true

Note: boolean values must be converted to strings before output.

7. html is used to replace <,>, &, and "with the corresponding & lt; & gt; & quot: & amp

8. index_of (substring, start) searches for a substring in the string and returns the index of the first character of the substring. If no substring is found,-1 is returned.

The start parameter is used to specify the index where the string is searched. The start parameter is a numerical value.

If start is greater than the string length, the start value is equal to the string length. If start is less than 0, the start value is 0.

$ {"String "? Index_of ("in"), the result is 3

$ {"String "? Index_of ("AB"), the result is-1

9. Length returns the string length $ {"string "? Length}, the result is 6

10. lower_case convert string to lowercase

$ {"String "? Lower_case}, the result is string

11. upper_case convert string to uppercase

$ {"String "? Upper_case}, the result is string

12. Contains determines whether a character contains a substring. Returns a Boolean value.

$ {"String "? Contains ("ing ")? String}, returns true

Note: boolean values must be converted to strings before output.

13. number converts a string to a number.

${111.11 "? Number}. The result is 111.11.

14. Replace replaces a portion of the string with another string from left to right.

$ {"Strabg "? Replace ("AB", "in")}, the result is string

15. Split Splits a string into a group of strings using the specified delimiter.

<# List "This | is | split "? Split ("|") as S>

$ {S}

</# List>

Result:

This

Is

Split

16. Trim deletes spaces at the beginning and end of a string $ {"string "? Trim}, the result is string

Iv. Operation numbers

1. c is used to convert numbers into strings

$ {123? C}, Result 123

2. String is used to convert numbers into strings.

Freemarker defines three numeric formats: number, currency (currency), and percent (Percentage). number indicates the default digital format conversion.

For example:

<# Assign tempnum = 20>

$ {Tempnum}

$ {Tempnum? String. Number} or $ {tempnum? String ("Number")}, the result is 20

$ {Tempnum? String. Currency} or $ {tempnum? String ("currency")}, with the result of ¥20.00

$ {Tempnum? String. percent} or $ {tempnum? String ("percent")}, the result is 2,000%

5. Operate boolean values

String is used to convert a Boolean value to a string for output.

Convert true to true, and convert false to false"

Foo? String ("yes", "no"). If the Boolean value is true, "yes" is returned; otherwise, no is returned.

Freemarker built-in functions, numbers, strings, and date formatting

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.