asp 中判断变量为空的方法及isempty、isnull与空字符串

发布时间:2017-07-06 13:51:53编辑:丝画阁阅读(549)

asp中isempty、isnull与空字符串三者之间的区别

isnull 说明指针为空,指针指到一个无效的位置,即对象不存在,
isempty 说明指针指向一个有效位置,但是值为空
1、空字符串
例:
a)Dim strTmp
response.write(strTmp="") ' 返回true
b)response.write(str="") ' 返回 true
c)Dim strTmp
strTmp=""
response.write(strTmp="") ' 返回 true
没有赋值的变量ASP可以认为是空字符串或叫做零长度字符串。

2、IsEmpty()
如果变量未初始化或显式地设置为 Empty,则函数 IsEmpty 返回 True;
否则函数返回 False。如果 expression 包含一个以上的变量,总返回 False。
例:
a)Dim strTmp
Response.Write(IsEmpty(strTmp)) ' 返回 True

b) Response.Write(IsEmpty(str))' 返回 True
c)Dim strTmp
strTmp = Null
Response.Write(IsEmpty(strTmp)) ' 返回 Flase
d)Dim strTmp
strTmp = Empty

 Response.Write(IsEmpty(strTmp)) ' 返回 True
e)Dim strTmp
strTmp = ""
Response.Write(IsEmpty(strTmp)) ' 返回 Flase

没有赋值的变量也可以认为是Empty 即空值

可以用isdate,isarray,isnumeric替代isempty进行测试a),b)两个例子,isnumeric也是返回 True,isdate,isarray返回 False


3.empty补充

   dim a,b,c,d,e,f,

   a=0

   b=0.0

   c=""

   d=false

   e=empty

   response.write(x=empty)  'x请用a,b,c,d,e,f其中一个代替,返回都为true

   response.write(isempty(x))  'x请用a,b,c,d,e,f其中一个代替,除了e,f,其它返回都为false


4、IsNull()
Null 值指出变量不包含有效数据。Null 与 Empty 不同,后者指出变量未经初始化。Null 与零长度字符串 ("") 也不同,零长度字符串往往指的是空串。
使用 IsNull 函数可以判断表达式是否包含 Null 值。
例:
a)Dim strTmp
Response.Write(IsNull(strTmp)) ' 返回 False
b)Response.Write(IsNull(strTmp)) ' 返回 False 注意这里strTmp是一个未经声明的变量
c)Dim strTmp
strTmp = Null
Response.Write(IsNull(strTmp)) ' 返回 True
d)Dim strTmp
strTmp = Empty
Response.Write(IsNull(strTmp)) ' 返回 False

关键字