Java judgment string is NULL, string is numeric _java

Source: Internet
Author: User

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;
}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.