最新遇到的问题:前端传输过来一条建表SQL语句,规范为
CREATE TABLE 表名 (字段 字段类型)
由这条建表sql解析出它的表名,由于建表语句可能是有多个空格,所以直接用split不现实,所以想出了两种解决方法
1、
/*String s="CREATE TABLE tb (id int)";
String s="CREATE TABLE tb (id int)";*/
String s="CREATE TABLE tb(id int)";
//先把左括号替换为空格
String s1 = s.replace("(", " ");
//用“ ”+ 把所有的空格都替换成一个空格
String s2 = s1.replaceAll(" +", " ");
//按照空格进行分割,第三个就是表名
String[] s3 = s2.split(" ");
System.out.println("表名为:"+s3[2]);
2、
String s="CREATE TABLE tb(id int)";
//首先替换掉所有的空格
String s1 = s.replaceAll(" ", "");
//从第11个开始直到“(”结束一定是表名
String substring = s1.substring(11, s1.indexOf("("));
System.out.println("表名为:"+substring);
表名为:tb