Regarding the empty judgment of String:
Copy the code and the code is as follows://This is correct
if (selection != null && !selection.equals("")) {
whereClause += selection;
}
//This is wrong
if (!selection.equals("") && selection != null) {
whereClause += selection;
}
Note: "==" compares the values of the two variables themselves, that is, the first addresses of the two objects in memory. And "equals()" compares whether the content contained in the string is the same. In the second way of writing, once the selection is really null, a null pointer exception will be reported directly when the equals method is executed and execution will not continue.
Determine whether a string is a number:
Copy the code code as follows:
// Call the function that comes with java
public static boolean isNumeric(String number) {
for (int i = number.length(); --i >= 0;) {
if (!Character.isDigit(number.charAt(i))) {
return false;
}
}
return true;
}
// Use regular expressions
public static boolean isNumeric(String number) {
Pattern pattern = Pattern.compile("[0-9]*");
return pattern.matcher(str).matches();
}
//Use ASCII code
public static boolean isNumeric(String number) {
for (int i = str.length(); --i >= 0;) {
int chr = str.charAt(i);
if (chr < 48 || chr > 57)
return false;
}
return true;
}