字符串不为null,空或空字符串 [英] String is not null, empty, or empty string

查看:331
本文介绍了字符串不为null,空或空字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

(在经典ASP中)检查字符串是否具有某个字符串(长度大于0)的最快,最简单的方法是什么,即NOT"Null","Nothing","Empty"或''空字符串

What is the quickest and easiest way (in Classic ASP) to check if a string has some string (that has a length greater than 0) i.e. NOT "Null", "Nothing", "Empty", or '' empty string

推荐答案

要确保您处理的Variant类型为字符串"子类型,您需要VarType或TypeName函数.要排除零长度的字符串,您需要Len().为了防止出现空格,您可以放入Trim().

To make sure that the Variant you deal with is of sub-type "string", you need the VarType or TypeName function. To rule out zero length strings, you need Len(). To guard against strings of space, you could throw in a Trim().

用于说明/实验的代码:

Code to illustrate/experiment with:

Option Explicit

Function qq(s) : qq = """" & s & """" : End Function

Function toLiteral(x)
  Select Case VarType(x)
    Case vbEmpty
      toLiteral = "<Empty>"
    Case vbNull
      toLiteral = "<Null>"
    Case vbObject
      toLiteral = "<" & TypeName(x) & " object>"
    Case vbString
      toLiteral = qq(x)
    Case Else
      toLiteral = CStr(x)
  End Select
End Function

Function isGoodStr(x)
  isGoodStr = False
  If vbString = VarType(x) Then
     If 0 < Len(x) Then
        isGoodStr = True
     End If
  End If
End Function

Dim x
For Each x In Array("ok", "", " ", 1, 1.1, True, Null, Empty, New RegExp)
    WScript.Echo toLiteral(x), CStr(isGoodStr(x))
Next

输出:

cscript 26107006.vbs
"ok" True
"" False
" " True
1 False
1.1 False
True False
<Null> False
<Empty> False
<IRegExp2 object> False

这篇关于字符串不为null,空或空字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆