页面提供逗号拼接的字符串,可做为数据库查询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, "','")+"'"); }
/** * 工具-字符串-转换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包字符串