如何在无Apache标记中比较两个字符串

问题描述 投票:2回答:1

我在apache free marker.中有以下情况

str1= Shruti

str2可能是( "Shruti" ,"shruti", shruti,'Shruti' or SHRUTI)

如果字符串2是double quotesingle quot e或纯文本,我们需要将其返回true。也应该不区分大小写。如果包含和比较字符串,如何忽略字符串中的单引号和双引号?

基本上,我想比较2个字符串。如果包含“单引号或双引号”,则应忽略。我尝试使用str1=str2?remove_beginning(""")?remove_end("""),但显示错误。

regex freemarker data-processing
1个回答
0
投票
您可以使用replace()并通过用反斜杠\"\'转义引号来匹配引号。我还使用trim()删除前导和尾随空格,并使用lower_case()忽略大小写。我在Online FreeMarker Tester中尝试了以下内容

str1 = "\"Hi\"" str2 = "'hi'"

<#if str1?trim?replace("\'","")?replace("\"","")?lower_case == str2?trim?replace("\'","")?replace("\"","")?lower_case>
  The two strings are equal
  ${str1?trim?replace("\'","")?replace("\"","")?lower_case} = ${str2?trim?replace("\'","")?replace("\"","")?lower_case}

</#if>
两个字符串相等

hi =嗨


这里是可用于比较两个字符串的函数。

<#function is_equal x y> <#return x?trim?replace("\'","")?replace("\"","")?lower_case == y?trim?replace("\'","")?replace("\"","")?lower_case> </#function> <#if is_equal(str1,str2)> The two strings are equal </#if>

© www.soinside.com 2019 - 2024. All rights reserved.