Groovy字符串连接 [英] Groovy String Concatenation

查看:316
本文介绍了Groovy字符串连接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当前的代码:

  row.column.each(){column  - > 
println column.attributes()['name']
println column.value()
}

Column 是一个节点,它具有单个属性和单个值。我解析一个XML来输入创建插入语句进入访问。是否有一种Groovy方法来创建以下结构化语句:
$ b $ pre $

我正在存储属性和值来分开数组,然后将它们弹出到正确的订单。

解决方案

我认为在常规方面比当前接受的答案更容易。 collect和join方法是为这种类型构建的。加入会自动处理连接,并且不会在字符串中放置逗号。

  def names = row.column.collect { it.attributes()['name']} .boin(,)
def values = row.column.collect {it.values()} .join(,)
def结果=INSERT INTO tablename($ names)VALUES($ values)


Current code:

row.column.each(){column ->
    println column.attributes()['name']
    println column.value()
}

Column is a Node that has a single attribute and a single value. I am parsing an xml to input create insert statements into access. Is there a Groovy way to create the following structured statement:

Insert INTO tablename (col1, col2, col3) VALUES (1,2,3)

I am currently storing the attribute and value to separate arrays then popping them into the correct order.

解决方案

I think it can be a lot easier in groovy than the currently accepted answer. The collect and join methods are built for this kind of thing. Join automatically takes care of concatenation and also does not put the trailing comma on the string

def names = row.column.collect { it.attributes()['name'] }.join(",")
def values = row.column.collect { it.values() }.join(",")
def result = "INSERT INTO tablename($names) VALUES($values)"

这篇关于Groovy字符串连接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆