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

背景

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

a,bapache

问题

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

xx in ('a','b')工具

需求

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

技术点

joinorm

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

实现源码

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

2019/11/25补充工具方法

/**
     * 工具-字符串-转换Sql查询IN中使用的格式
     * 效果:a,b==>'a','b'
     * @param str
     * @return
     */
    public String strToDbin(String str){
        return String.format("'%s'", StringUtils.join(str.split(","),"','"));
    }

回顾

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

相关文章
相关标签/搜索