freemarker 字符串函数

本文详细介绍了字符串内置方法的使用,包括substring、cap_first、uncap_first、capitalize、chop_linebreak、date、time、datetime、ends_with、html、url等,并展示了如何在实际场景中应用这些方法。

一:字符串内置方法
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

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

Yes

Only with r

Only with r

Only with r

No

match

Yes

No

Yes

Yes

Yes

No


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值