The difference between IsNull, IsEmpty, and empty strings in ASP

Source: Internet
Author: User

The two parameters that we often use in ASP are IsNull and IsEmpty, which we use when we judge that a string is empty. For example, a string is str1, often we use the If IsNull (str1) or IsEmpty (STR1) then to determine whether the string is null by using the IF str1= "" then.

 

Sometimes we are very entangled, what is the difference between isNull, IsEmpty and the "empty string"?

IsNull ()

A Null value indicates that the variable does not contain valid data. Null differs from Empty, which indicates that the variable was uninitialized. Null is also different from a 0-length string (""), and a 0-length string often refers to an empty string.

Focus on using the IsNull function to determine whether an expression contains a Null value. In some cases, you want the expression to be value True, such as Ifvar=null and Ifvar<>null, but they are usually always False. This is because any expression containing a null itself is null, so the expression evaluates to False.

Instance 1

The code is as follows
Dim x
document.write (IsNull (x) & "<br/>") x=10
document.write (IsNull (x) & "<br/>") x=empty
document.write (IsNull (x) & "<br/>") x=null
document.write (IsNull (x))
Output:
False
False
False
True


The following example uses the IsNull function to determine whether a variable contains Null:

The code is as follows

Dim MyVar, MyCheck
MyCheck = IsNull (MyVar) ' returns False
MyVar = NULL ' is assigned NULL
MyCheck = IsNull (MyVar) ' returns True
MyVar = Empty ' fu as Empty
MyCheck = IsNull (MyVar) ' returns False

IsEmpty ()

If the variable is uninitialized or explicitly set to Empty, the function isempty returns True; otherwise, the function returns FALSE. If ExPRession contains more than one variable, the total returns FALSE.

The following example uses the IsEmpty function to determine whether a variable can be initialized:

The code is as follows

Dim MyVar, MyCheck
MyCheck = IsEmpty (MyVar) ' returns TRUE.
MyVar = NULL ' is assigned null.
MyCheck = IsEmpty (MyVar) ' returns FALSE.
MyVar = Empty ' Fu to Empty.
MyCheck = IsEmpty (MyVar) ' returns TRUE.

3. 0 length string ("")
A 0-length string often refers to an empty string.
Str= "", assigning an empty string to a str variable, has been assigned, and is assigned a character

The difference between the three is:
IsNull is a type test that tests whether a null value (NULL) type.
IsEmpty is a value test that tests whether it is a null value. But the methods used in this different language are different.
= "" is a string test that tests whether the value is a null value.

Say a isempty, in some languages isempty is a null-value test that can test the following:

The code is as follows

Dim str As String
IsEmpty (str) =true

str = ""
IsEmpty (str) =true

str = NULL
IsEmpty (str) =true

Dim str As Integer
str = 0
IsEmpty (str) = True

That is, part of the language of the data in the 0, the character of the empty string, null values are used as null value of the test category.

But in C # and other languages, the requirements are more stringent, VB requirements are not very strict. As long as you know a special type of NULL type, its class is certainly null. There is also an undefined type in some languages:
Undefined type, as in C #:
String str;
Then string only has this one defined, so its value is not really defined at this time, it is the undefined type. This type often exists in C family language and Class C family such as ECMAScript family (JavaScript, etc.) and Java!

Add: In the database design you can specify that a field is null so that if it is no longer assigned to him, the record is null, but when the table is exported, the null record destroys the table's structure, that is, the grid lines in the table disappear, and many people may A default value is set in the database or an empty string is assigned to the record when processing.

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.