Use String class in java to manipulate complex strings

Today, I came out with a question and almost stumped myself ... The
question is this:
There are three sql statements that are known, Xiao Ming accidentally wrote a piece, you help him split it. The sql statement is:"select * from books where bid = 1 select * from cate where name = ‘java’ select title from books where id =2002 "

Here is the implementation code:

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);
		}
		
	}

A closer look is not difficult.
Welcome to pay attention to the public number:Xiongxiong's small classroom, Hahaha ~

Published 691 original articles · praised 649 · 1.11 million views

Guess you like

Origin blog.csdn.net/qq_34137397/article/details/105081468