【问题标题】:Determine If a String Is Present in a List or Map?确定字符串是否存在于列表或映射中?
【发布时间】:2019-08-16 15:11:42
【问题描述】:

如何确定列表或地图是否包含特定字符串?例如(伪代码):

<#if listofItems.contains("random-string") >
   the map contains a key called random-string
</#if>

【问题讨论】:

    标签: freemarker


    【解决方案1】:

    列表

    如果是列表:

    <#if listOfItems?seq_contains("random-string")>
      ...
    </#if>
    

    地图

    如果是地图:

    <#if someMap["random-string"]??>
      ...
    </#if>
    

    如果是地图且键不包含特殊字符:

    <#if someMap.randomString??>
      ...
    </#if>
    

    如果它是一个映射并且你正在寻找一个键值对的值:

    <#if someMap?values?seq_contains("random-string")>
      ...
    </#if>
    

    【讨论】:

      【解决方案2】:

      我认为这种方法应该可行

      <#if listofItems['random-string']?? >
         you are inside if block
      </#if>
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2011-01-26
        • 2014-03-10
        • 1970-01-01
        • 2017-05-21
        • 2017-02-28
        • 2013-07-28
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多