Today in the project you need to parse a string of parameter ranges and then determine whether an incoming parameter is within the expression. Because the parameter range string is a combination of (0<p<10 or 0<=p<10 or 0<p<=10 or 0<=p<=10), I think it is troublesome to go directly to split, using string to Byte[] do the processing again. The code is as follows.
I don't know if we have a better way of dealing with it.
Package Test_lzg;import Java.util.arraylist;import Java.util.list;public class A {public static void main (string[] args) {String paramrangestring = "0<p<=10"; String paramstr = "11"; System.out.println (Parseparamrange (paramrangestring, paramstr));p aramrangestring = "0<=p<=10";p aramstr = "5"; System.out.println (Parseparamrange (paramrangestring, Paramstr));} /** * Parse parameter range expression * * @param the string of the paramrangestring expression. The expression may be (0<p<10 or 0<=p<10 or 0<p<=10 or 0<=p<=10) * @param paramstr parameters to be judged * @return */private STA Tic Boolean Parseparamrange (String paramrangestring, String paramstr) {byte[] Paramrangebytearr = Paramrangestring.getbytes (); Convert the string to byte[] to handle list<byte> minnumlist = new arraylist<byte> (); list<byte> maxnumlist = new arraylist<byte> (); String leftopt = ""; String rightopt = ""; Boolean isleftopt = true; The first occurrence or second occurrence of the token, for the first time truefor (int i = 0, len = paramrangebytearr.length; i < Len; i++) {if (paramrangebytearr[i] = = 6 0) {// 60 is "<" corresponding ASCII code if (paramrangebytearr[i + 1] = = 61) {//61 is "=" corresponding ASCII code if (isleftopt) {leftopt = "<=";} else {R ightopt = "<=";} i++;} else {if (isleftopt) {leftopt = "<";} else {rightopt = "<";}} isleftopt = false;continue;} if (<= paramrangebytearr[i] && Paramrangebytearr[i] <= 57) {//48 is the ASCII code for "0" and 57 is "9" ASCII if (isleftop T) {Minnumlist.add (paramrangebytearr[i]);} else {Maxnumlist.add (paramrangebytearr[i]);}}} Place the number of the left boundary from the list, put byte[], and conveniently convert to stringbyte[] Leftborder = new Byte[minnumlist.size ()];for (int i = 0, Len = minnumlist.size ( ); i < Len; i++) {Leftborder[i] = Minnumlist.get (i);} Place the number of the right boundary from the list, put byte[], and conveniently convert to stringbyte[] Rightborder = new Byte[maxnumlist.size ()];for (int i = 0, Len = maxnumlist.size (); i < Len; i++) {Rightborder[i] = Maxnumlist.get (i);} Print the test to see if parsing is correct System.out.println (new String (Leftborder) + leftopt); SYSTEM.OUT.PRINTLN (rightopt + new String (Rightborder)), return Judgeparamrange (Paramstr, new String (Leftborder), Leftopt,true) && Judgeparamrange (Paramstr, New String (Rightborder), rightopt, false);} /** * * Determine if the parameter is in the range * * @param paramstr the parameter to be judged * @param borderparamstr boundary value * @param opt-related symbol (< or <=) * @param Isleftborder is the left border * @return */private static Boolean Judgeparamrange (String paramstr, String borderparamstr, string opt , Boolean Isleftborder) {Float Borderparam = float.valueof (BORDERPARAMSTR);//Convert boundary value to floatfloat param = float.valueof (pa RAMSTR); Convert the parameters that need to be judged to Floatif (Isleftborder) {//If the left boundary is if ("<". Equals (opt)) {if (Borderparam >= param) {return false;}} els E if ("<=". Equals (opt)) {if (Borderparam > param) {return false;}}} else {//otherwise right boundary if ("<". Equals (opt)) {if (param >= borderparam) {return false;}} else if ("<=". Equals (opt)) {if ( param > Borderparam) {return false;}}} return true;}}
Parses a string in Java, such as a character (0<p<10 or 0<=p<10 or 0<p<=10 or 0<=p<=10), to see whether the passed in argument is within an expression