Classic ASP InStr() Evaluates True on Empty Comparison String

时间:2015-07-31 20:55:35

标签: vbscript asp-classic

I ran into an issue with the Classic ASP VbScript InStr() function. As shown below, the second call to InStr() returns 1 when searching for an empty string in a non empty string. I'm curious why this is happening.

' InStr Test
Dim someText : someText = "So say we all"
Dim emptyString : emptyString = ""

'' I expect this to be true
If inStr(1,someText,"so",1) > 0 Then
    Response.write ( "I found ""so""<br />" )
End If 

'' I expect this to be false
If inStr(1, someText, emptyString, 1) > 0 Then
    Response.Write( "I found an empty string<br />" )
End If

EDIT: Some additional clarification: The reason for the question came up when debugging legacy code and running into a situation like this:

Function Go(value)
     If InStr(1, "Option1|Option2|Option3", value, 1) > 0 Then
          ' Do some stuff
     End If
End Function

In some cases function Go() can get called with an empty string. The original developer's intent was not to check whether value was empty, but rather, whether or not value was equal to one of the piped delimited values (Option1,Option2, etc.).

Thinking about this further it makes sense that every string is created from an empty string, and I can understand why a programming language would assume a string with all characters removed still contains the empty string.

What doesn't make sense to me is why programming languages are implementing this. Consider these 2 statements:

InStr("so say we all", "s") '' evaluates to 1
InStr("so say we all", "")  '' evaluates to 1

The InStr() function will return the position of the first occurrence of one string within another. In both of the above cases, the result is 1. However, position 1 always contains the character "s", not an empty string. Furthermore, using another string function like Len() or LenB() on an empty string alone will result in 0, indicating a character length of 0.

It seems that there is some inconsistency here. The empty string contained in all strings is not actually a character, but the InStr() function is treating it as one when other string functions are not. I find this to be un-intuitive and un-necessary.

3 个答案:

答案 0 :(得分:4)

我很困惑为什么你认为这种行为是不正确的。如果要求确实'abc'包含''?甚至有意义,答案必须是“是”:所有字符串都包含空字符串作为一个简单的案例。所以你的“为什么会发生这种情况”问题的答案是因为这是唯一理智的事情。

答案 1 :(得分:4)

Empty String是字符串的Identity Element

  

组或相关的标识元素I(也表示为E,e或1)   数学结构S是唯一的元素,使得Ia = aI = a for   S中的每个元素a。符号&#34; E&#34;源自德语单词for   团结,&#34; Einheit。&#34;标识元素也称为单元元素。

如果向数字n加0,则结果为n;如果你添加/连接&#34;&#34;到字符串s的结果是s:

>> WScript.Echo CStr(1 = 1 + 0)
>> WScript.Echo CStr("a" = "a" & "")
>>
True
True

因此每个String和SubString至少包含一个&#34;&#34;:

>> s = "abc"
>> For p = 1 To Len(s)
>>     WScript.Echo InStr(p, s, "")
>> Next
>>
1
2
3

Instr()忠实地报道。文档甚至声明:

InStr([start, ]string1, string2[, compare])
...
The InStr function returns the following values:
...
string2 is zero-length            start

写下你的

  

但是,位置1始终包含字符&#34; s&#34;,而不是空字符   字符串。

==&GT;

  

位置1始终包含字符&#34; s&#34;,因此为空   字符串也是。

答案 2 :(得分:0)

这是正确的imho。至少它是我所期望的空字符串是任何其他字符串的一部分。但也许这是一个哲学问题。 ASP是这样做的,所以忍受它。实际上,如果你需要一个不同的行为,你可以编写自己的Method,InStrNotEmpty或其他东西,它会在空搜索字符串上返回false。