For a short sentence on String:
Copy Code code as follows:
That's the right one.
if (selection!= null &&!selection.equals ("")) {
Whereclause + = Selection;
}
This is wrong.
if (!selection.equals ("") && selection!= null) {
Whereclause + = Selection;
}
Note: "= =" compares the value of two variables themselves, that is, the first address of two objects in memory. Equals () compares whether the contents of the string contain the same content. In the second formulation, once selection is really null, the null pointer exception is reported directly when the Equals method is executed, causing the execution to no longer proceed.
To determine whether a string is a number:
Copy Code code as follows:
Call Java self-brought functions
public static Boolean IsNumeric (String number) {
for (int i = Number.length (); I. >= 0;) {
if (! Character.isdigit (Number.charat (i))) {
return false;
}
}
return true;
}
Using regular expressions
public static Boolean IsNumeric (String number) {
Pattern pattern = Pattern.compile ("[0-9]*");
return Pattern.matcher (str). matches ();
}
Using ASCII code
public static Boolean IsNumeric (String number) {
for (int i = Str.length (); I. >= 0;) {
int chr = Str.charat (i);
if (Chr < | | CHR > 57)
return false;
}
return true;
}