今天自己出了一道题差点把自己难住……
题目是这样的:
已知有三条查询的sql语句,小明不小心写一块了,你帮他拆分开。sql语句为:"select * from books where bid = 1 select * from cate where name = ‘java’ select title from books where id =2002 "
下面是实现代码:
public static void testUpS() { String sql = "select * from books" + "where bid = 1 select * from cate" + " where name = 'java' select title" + " from books where id =2002 "; // 先查找第二个select的位置 int i1 = sql.indexOf("select", 1); int i2 = sql.lastIndexOf("select"); // 给select前面插入一个-,便于后面截取 StringBuffer sb = new StringBuffer(sql); sb.insert(i1, "-"); sb.insert(i2,"-"); String [] str = (sb.toString()).split("-"); for (String ss : str) { System.out.println(ss); } }
细细一看,不是很难的。