| String split方法在忽略参数大小的情况下取得String[]的小技巧 |
|
| 作者是 蒋家狂潮 | |||
| 2008-05-04 07:01:17 | |||
|
??? select * from tableA union select * from tableB ????要根据关键字把两个查询语句分解出来,即得到 ?? select * from tableA 和 select * from tableB ?? 一般的做法是: ?String sql="select * from tableA union select * from tableB"; ?? if(sql.toLowerCase().indexOf(" union ")>-1){ ??? String sql2[]=sql.split(" union "); ??? for(int i=0;i<sql2.length;i++){ ????? System.out.println(sql2[i]); ??? } ? } ?但因为这个是SQL语句,union关键字不区分大小写的,所有sql语句有这些情况: ?select * from tableA?UNION select * from tableB ?select * from tableA?UnIOn select * from tableB ?select * from tableA?UNiON select * from tableB ?…… 所以单单用这样的语句:String sql2[]=sql.split(" union "); 是分解不出子语句的。 这里的碰到了特殊情况,我不知道别人是怎么做的,我的思路是应该有种机制sql.split(不区分大小写); String类没提供该方法,于是我觉得先看看SUN的源码,了解它的源码 是怎么处理的,找到了: public int indexOf(String str) { ?return indexOf(str, 0); ??? } ?public String[] split(String regex, int limit) { ?return Pattern.compile(regex).split(this, limit); ??? } 它运用的不过是正则表达式而已,幸好这几天对正则表达式有所研究。 看看正则表达式: DEELX 正则表达式匹配模式
DEELX 支持对 IGNORECASE, SINGLELINE, MULITLINE, GLOBAL 进行修改?
String sql2[]=sql.split(" union "); 修改为 String sql2[]=sql.split(" (?i)union "); 问题即可迎刃而解。 完整的: String sql="select * from tableA union select * from tableB"; ?? if(sql.toLowerCase().indexOf(" union ")>-1){ ??? String sql2[]=sql.split(" (?i)union "); ??? for(int i=0;i<sql2.length;i++){ ????? System.out.println(sql2[i]); ??? } ? } 打印结果: select * from tableA select * from tableB |
|||
| 最近更新 ( 2008-05-04 07:01:17 ) | |||

