使用java中的String类操作复杂的字符串

今天自己出了一道题差点把自己难住……
题目是这样的:
已知有三条查询的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);
		}
		
	}

细细一看,不是很难的。
欢迎关注公众号:雄雄的小课堂,哈哈哈~

发布了691 篇原创文章 · 获赞 649 · 访问量 111万+

猜你喜欢

转载自blog.csdn.net/qq_34137397/article/details/105081468