【问题标题】:How can I join a string list in Groovy using a different separator for the last item如何在 Groovy 中为最后一项使用不同的分隔符加入字符串列表
【发布时间】:2013-01-31 08:24:36
【问题描述】:

我想加入一个字符串列表以供用户输出。每个字符串之间的分隔符应为',',但分隔符应为'and' 的最后一个元素除外。

例如:

def a = ['one', 'two', 'three'];
println a.joinWithDifferentLast(', ', ' and ');
// output: one, two and three

如何在 Groovy 中实现这样的连接功能?如果它可以处理一个元素(无分隔符)、两个元素(最后一个分隔符)和多个元素的情况,那就太好了。

【问题讨论】:

    标签: string list collections groovy


    【解决方案1】:

    为了好玩,我试着让一些东西更容易阅读:

    def static joinWithDifferentLast(List list, String firstJoin, String lastJoin) {
        switch (list?.size() ?: 0) {
            case 0:
                return ''
            case 1:
                return list.head() as String
            default:
                return list.init().join(firstJoin) + lastJoin + list.last()
        }
    }
    

    【讨论】:

      【解决方案2】:
      List.metaClass.joinWithDifferentLast { a, b ->
          delegate.join(a).reverse().replaceFirst(a.reverse(),b.reverse()).reverse()
      }
      
      def a = ['one', 'two', 'three'];
      println a.joinWithDifferentLast(',',' and ') //prints one,two and three
      
      assert ''           == [].joinWithDifferentLast(' , ', ' and ' )
      assert '1'          == [ 1 ].joinWithDifferentLast( ', ', ' and ' )
      assert '1 and 2'    == [ 1, 2 ].joinWithDifferentLast( ', ', ' and ' )
      assert '1, 2 and 3' == [ 1, 2, 3 ].joinWithDifferentLast(', ', ' and ' ) 
      assert '1 %ac 2 %ac 3 %ac 4 %ac 5 %bi 6' == [ 1, 2, 3, 4, 5, 6 ].joinWithDifferentLast(' %ac ', ' %bi ' )
      

      将是我的第一个蛮力和幼稚(但已纠正)的猜测:-D

      List.metaClass.joinWithDifferentLast { a, b ->
          def l = delegate.size() 
          delegate[0..l-2].join(a) + b + delegate[l-1]
      }
      

      “反转”有点少,但需要对列表的 size() 进行安全化

      【讨论】:

      • 不幸的是,第一个函数在长度 > 1 的分隔符的情况下不起作用(例如,当您使用 ', ' 时)。没有测试第二个。
      • 我已经进行了相应的编辑,因为我在第一行坚持你的例子
      • @Grooveek 如果列表的最后一个元素包含第一个分隔符,这也有问题,即:assert [ 'pizza', 'bacon', 'sausage, beans and chips'].joinWithDifferentLast() == 'pizza, bacon, sausage and beans and chips' 如您所见,sausage 元素已更改,并且没有 @987654326 @分隔符。
      【解决方案3】:

      你也可以这样做:

      def joinWithDifferentLast( List list, String others, String last ) {
        def start = list.take( list.size() - 1 ).join( others )
        def end   = list.drop( list.size() - 1 )[ 0 ]
        if( start ) {
          [ start, last, end ].join()
        }
        else {
          end as String ?: ''
        }
      }
      
      assert ''           == joinWithDifferentLast( [],          ', ', ' and ' )
      assert '1'          == joinWithDifferentLast( [ 1 ],       ', ', ' and ' )
      assert '1 and 2'    == joinWithDifferentLast( [ 1, 2 ],    ', ', ' and ' )
      assert '1, 2 and 3' == joinWithDifferentLast( [ 1, 2, 3 ], ', ', ' and ' )
      

      【讨论】:

        猜你喜欢
        • 2015-07-17
        • 2016-04-28
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-04-23
        • 2012-08-01
        相关资源
        最近更新 更多