【沫沫金】Java逗号拼接字符串增加单引号

背景

页面提供逗号拼接的字符串,可作为数据库查询in的条件。

a,b

问题

数据库针对字符串的in条件,要求增加单引号

xx in ('a','b')

需求

页面的逗号拼接字符串直接转换成数据库要求格式(不使用for循环)

技术点

join

org.apache.commons.lang.StringUtils
StringUtils.join(split, "','")

实现源码

    public static void main(String[] args) {
        String[] split = ("阎军梅,李乾毅".replaceAll(",", ",")).split(",");
        System.out.println("'"+StringUtils.join(split, "','")+"'");
    }

回顾

页面逗号拼接字符串,转换为数据库要求的每个元素带单引号的格式。
不使用for循环处理,以上方法即可轻松实现。感谢apache、感谢commons包

猜你喜欢

转载自blog.51cto.com/zl0828/2300799