The original idea: like a pair of parentheses, suppose to meet the select on the stack, if you meet from the stack, until the stack is empty, get the position at this time. For string interception.
Implementation method: Meet the character S and after 5 characters elect + 1, meet the character F and 3 characters in a row for ROM-1, when the counter result is 0 o'clock, return the position of the current character, and then the string interception can be:
/**** * Get intercept location * @param selectsql * @return * * * public static int Getsubindex (String selectsql) {System.out.println (Selectsql.length ()); int count = 0;for (int i=0;i<selectsql.length (); i++) {char c = Selectsql.charat (i); if (c = = ' s ') {if (Selectsql.charat (i+1) = = ' E ' &&selectsql.charat (i+2) = = ' L ' && Selectsql.charat (i+3) = = ' E ' &&selectsql.charat (i+4) = = ' C ' && selectsql.charat (i+5) = = ' t ') {count++;i=i+5;} Else{continue;}} else if (c = = ' F ') {if (Selectsql.charat (i+1) = = ' R ' &&selectsql.charat (i+2) = = ' O ' && Selectsql.charat (i+3) = = ' m ') { count--; i=i+3; System.out.println (count); if (count = = 0) { System.out.println (i); return i+1;} } Else{continue;}} else {continue;}} return-1; }
To perform a character intercept operation:
/** * string intercept * @param insql * @return * */public static string getoutsql (string insql) { int index = getsubindex (insql); if (Index! =-1) { String outsql = "SELECT count ( -1) from" + insql.substring (index); return outsql; } else{ System.out.println ("Not a corrent sql"); Return "Not a corrent sql"; } }
Arbitrarily a query SQL translates to the corresponding number of query result sets