freemaker学习笔记--字符串内置方法

一:字符串内置方法
      substring(from, toExclusive)
  
   cap_first

   uncap_first

   capitalize

   chop_linebreak

   date, time, datetime:
   <#assign test1 = "10/25/1995"?date("MM/dd/yyyy")>
   <#assign test2 = "15:05:30"?time("HH:mm:ss")>
   <#assign test3 = "1995-10-25 03:05 PM"?datetime("yyyy-MM-dd hh:mm a")>

   ends_with

   html
   < replaced with &lt; 
   > replaced with &gt; 
   & replaced with &amp; 
   " replaced with &quot; 

   groups用于matches的结果。

   index_of

   j_string

   js_string

   last_index_of

   length
(文章来源 www.iocblog.net)
   lower_case

   left_pad

   right_pad

   contains

   matchs
   返回boolean或者sequence。
   <#if "fxo"?matches("f.?o")>Matches.<#else>Does not match.</#if>
   <#assign res = "foo bar fyo"?matches("f.?o")>
   <#if res>Matches.<#else>Does not match.</#if>
   Matching sub-strings:
   <#list res as m>
   - ${m}
   </#list>
   res就是典型的多值类型。

   如果正则表达式包含组(圆括号),可以通过groups访问。
   <#assign res = "aa/rx; ab/r;"?matches("(\\w[^/]+)/([^;]+);")>
   <#list res as m>
   - ${m} is ${m?groups[1]} per ${m?groups[2]}
   </#list> 

   number

   replace

   rtf
   \ replaced with \\
   { replaced with \{
   } replaced with \}


   url
   将所有url的保留字符进行转码。
   url_escaping_charset
 
  split 

  starts_with

   trim,upper_case。word_list,xml

   字符串内置方法可以接受的通用标记:
   i: 大小写不敏感。
 f: 只用于第一个,常用于替换,查找等,取第一次出现
 r: 子串是个正则表达式。
 m: 正则表达式的多行模式。
   s: 单行模式。
 c: 允许空白或注释在正则表达式中。
<#assign s = 'foo bAr baar'>
${s?replace('ba', 'XY')}
i: ${s?replace('ba', 'XY', 'i')}
if: ${s?replace('ba', 'XY', 'if')}
r: ${s?replace('ba*', 'XY', 'r')}
ri: ${s?replace('ba*', 'XY', 'ri')}
rif: ${s?replace('ba*', 'XY', 'rif')}


Built-in

i

r

m

s

c

f

replace

Yes

Yes

Only with r

Only with r

Only with r

Yes

split

Yes(文章来源 www.iocblog.net)

Yes

Only with r

Only with r

Only with r

No

match

Yes

No

Yes

Yes

Yes

No




文章整理:iocblog
版权申明:本站文章均来自网络,如有侵权,请联系我们,我们收到后立即删除,谢谢!
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有。