【问题标题】:Is there a VBscript equivalent for .Net string.IsNullOrWhiteSpace()?.Net string.IsNullOrWhiteSpace() 是否有等效的 VBscript?
【发布时间】:2017-11-29 08:29:54
【问题描述】:

我正在用 VBscript 编写脚本,我需要检查字符串是否为空或只有空白字符(例如空格、制表符、换行符...)

在 .Net 中有这个方便的 string.IsNullOrWhiteSpace() 操作来测试它,但我似乎无法在 VBscript 中找到一个简单的等价物。
我知道我可以循环每个字符,然后将其与已知空白字符列表进行比较,或者我可以使用正则表达式,但我希望有更好的解决方案

【问题讨论】:

标签: .net vbscript


【解决方案1】:

没有这种方法,我认为这是最简单的:

Len(Trim(str)) = 0

正如 omegastripes 所指出的,这种方法与 .NET 方法 IsNullOrWhieSpace 不同,因为 white-spaces 包括空格、制表符、换行符和其他这些类别的字符。

在 VbScript 中没有等价物。因此,如果您想包含所有字符而不仅仅是空格,则需要一种正则表达式方法。 Here's 是一个。

【讨论】:

  • 该方法缺少空白字符,即。 e. vbNewLine, vbCrLf.
  • 谢谢,您的回答确实启发了我编写一个函数,该函数使用TrimReplace 的组合来获得我需要的东西。
【解决方案2】:

感谢蒂姆的回答,我想出了这个解决方案。
它并不完美,也不是最佳答案,但对于我的目的来说已经足够了。

'checks if this string is empty or has only whitespace characters
function isEmptyOrWhiteSpace(stringToCheck)
    dim returnValue
    returnValue = false
    if len(stringToCheck) = 0 then
        returnValue = true
    elseif len(trim(stringToCheck)) = 0 then
        returnValue = true
    else
        'remove all whitespace characters other then spaces
        dim replacedString
        replacedString = replace(stringToCheck, vbTab, "")
        replacedString = replace(replacedString, vbNewline, "")
        replacedString = replace(replacedString, vbCRLF, "")
        'Other characters to replace?
        if len(trim(replacedString)) = 0 then
            returnValue = true
        end if
    end if
    'return
    isEmptyOrWhiteSpace = returnValue
end function

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-07-15
    • 2018-11-10
    相关资源
    最近更新 更多