如何检查字符串是否可转换为数字

时间:2016-11-03 23:50:48

标签: java freemarker

在freemarker中,我有一个宏PrimeNG执行以下操作:

myfunc

所以,基本上,如果<#macro myfunc x> <#attempt> <#assign x=(x?number)> <#recover> </#attempt> ${x!''} </#macro> (以字符串开头)并且可以转换为数字,那么我想将其包装在x中,否则只显示该值。

除了随附的丑陋(${x})错误记录外,我对此解决方案没问题。

似乎没有一种简单的方法来检查字符串是否可以转换为不使用<#attempt>的数字,或者我的研究步履蹒跚 - 我也不想在Java方面这样做,因为我看到这是视图代码,不属于我的模型代码。有人知道吗?

1 个答案:

答案 0 :(得分:1)

您可以使用正则表达式:

<#if x?matches("\\d+")>
    // do something with x?number
<#else>
    // do something with x
</#if>
相关问题