groovy array remove the brackets

戏子无情 提交于 2019-12-19 12:21:46

问题


  def colarray=[]
                    def num
                    def newRole = rolecol.split(',')
                    def len = newRole.size()
                    println "$newRole,$len"
                    for (num = 0; num < len; num++) {
                        def col = "col"
                        col="$col"+newRole[num]
                        colarray.add(col)

                    }
                    println colarray
                    sql.eachRow("select col01,$colarray from read_csv where col01=? and col${usercol}!=? ", [file.name,""])

i want save col1..col11 into array and call it from select statement, but the problem is that $colarray has the brackets with it (like [col03, col04, col05, col06, col07, col08, col09, col10, col11, col12, col13, col14, col15, col16, col17, col18, col19, col20, col21, col22, col23, col24, col25, col26, col27, col28, col29, col30, col31, col32, col33] ), so now i want to remove them, anyone can help with it???thx


回答1:


the [] are there, because you are doing an implicit toString on the list. use colarray.join(',')




回答2:


def colarray = rolecol.tokenize(',').collect { "col$it" }.join(', ')

Then you will also need to escape your sql:

sql.eachRow("select col01, ${Sql.expand(colarray)} from read_csv where col01 = ? and ${Sql.expand('col' + usercol)} != ? ", [file.name,""])


来源:https://stackoverflow.com/questions/28064184/groovy-array-remove-the-brackets

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!