String 如何在Groovy中为最后一项使用不同的分隔符来连接字符串列表
我想加入用户输出的字符串列表。每个字符串之间的分隔符应为“String 如何在Groovy中为最后一项使用不同的分隔符来连接字符串列表,string,list,collections,groovy,String,List,Collections,Groovy,我想加入用户输出的字符串列表。每个字符串之间的分隔符应为“”、“”,但最后一个元素的分隔符应为“”和“” 例如: def a = ['one', 'two', 'three']; println a.joinWithDifferentLast(', ', ' and '); // output: one, two and three 如何在Groovy中实现这样的连接函数?如果它可以处理一个元素(没有分隔符)、两个元素(最后一个分隔符)和多个元素的情况就好了 List.metaClass.jo
”、“
”,但最后一个元素的分隔符应为“”和“
”
例如:
def a = ['one', 'two', 'three'];
println a.joinWithDifferentLast(', ', ' and ');
// output: one, two and three
如何在Groovy中实现这样的连接函数?如果它可以处理一个元素(没有分隔符)、两个元素(最后一个分隔符)和多个元素的情况就好了
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]
}
稍微少了一点“反转”,但需要对列表的大小()进行证券化
这将是我的第一次暴力和天真(但已更正)猜测:-D
List.metaClass.joinWithDifferentLast { a, b ->
def l = delegate.size()
delegate[0..l-2].join(a) + b + delegate[l-1]
}
稍微少一点“反转”,但需要对列表的大小()进行证券化您也可以这样做:
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 ' )
您也可以这样做:
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 ' )
为了好玩,我试着让一些东西更容易阅读:
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()
}
}
为了好玩,我试着让一些东西更容易阅读:
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()
}
}
不幸的是,第一个函数在长度大于1的分隔符的情况下不起作用(例如,当您使用
,'
)。我没有测试第二个。我已经相应地进行了编辑,因为我在第一个示例中遇到了您的示例row@Grooveek如果列表的最后一个元素包含第一个分隔符,也会出现问题,即:assert['pizza'、'bacon'、'香肠、豆子和薯条].joinWithDifferentList()=='pizza,bacon,香肠、豆子和薯片“
正如您所看到的,香肠
元素已更改,并且没有和
分隔符。不幸的是,第一个功能在长度>1的分隔符的情况下不起作用(例如,当您使用”,“
)。我没有测试第二个。我已经相应地进行了编辑,因为我在第一个示例中遇到了您的示例row@Grooveek如果列表的最后一个元素包含第一个分隔符,也会出现问题,即:assert['pizza'、'bacon'、'香肠、豆子和薯条].joinWithDifferentList()=='pizza,bacon,香肠、豆类和薯条
正如您所看到的,香肠
元素已更改,并且没有和
分隔符。