背景
页面提供逗号拼接的字符串, 可作为数据库查询 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 包
来源: http://www.bubuko.com/infodetail-2810360.html