使用 freemarker 将字符串中的最后 2 个字符大写?

发布于 2024-12-23 00:11:27 字数 69 浏览 3 评论 0原文

我有一个自由标记变量 ${string} ,它可以是任意长度。我如何将最后 2 个字符大写? 谢谢

I have a freemarker variable, ${string} which can be of any length. How would I capitalize just the last 2 characters?
Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

秋风の叶未落 2024-12-30 00:11:27

您可以使用 upper_case 内置对字符串进行序列切片并编写FTL 函数如

<#function foo text>
  <#local len = text?length />
  <#if (len>2)>
    <#return text[0..len-3] + text[len-2..]?upper_case> 
  <#else>
    <#return text?upper_case>
  </#if>
</#function>

然后 FTL 表达式

${foo("foobar")}

生成字符串 foobAR

You can use the upper_case builtin and sequence slicing on strings and write a FTL function like

<#function foo text>
  <#local len = text?length />
  <#if (len>2)>
    <#return text[0..len-3] + text[len-2..]?upper_case> 
  <#else>
    <#return text?upper_case>
  </#if>
</#function>

Then the FTL expression

${foo("foobar")}

produces the string foobAR.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文