Reputation: 835
I have come across a puzzler regarding string behavior in ASP classic.
The code pulls in some values from session and then attempts to use those as strings.
The problem is that the value is somehow canceling string concatenation.
Dim testVar
testVar = session("test") <- this value is "aaa"
response.write "XXX - " & testVar & " - XXX"
That code prints XXX - aaa
Alternatively, if I set testVar to some string value a la testVar="bbb"
, the string concatenation works as expected.
One more thing I discovered in trying to figure this out...
StrComp(testVar, "aaa" ,0)
returns 1
StrComp(testVar, "aaa" ,1)
returns 0
So any ideas why my testVar is killing the concatenation?
Upvotes: 2
Views: 252
Reputation: 38755
As can be seen from
>> For Each s In Array("AB", "A" & Chrw(0))
>> s = "XXX - " & s & " - XXX"
>> WScript.Echo Len(s), s
>> Next
>>
14 XXX - AB - XXX
14 XXX - A
ChrW(0)s neither cancel nor prevent concatenation. The display problem is caused by pecularities of VBScript's IO methods.
If you have one place/piece of code for either getting or displaying testVar (which you probably should), one
s = Replace(s, ChrW(0), "WhateverMakesSenseInYourContext")
should do the trick.
Evidence:
>> For Each s In Array("AB", "A" & Chrw(0))
>> s = "XXX - " & s & " - XXX"
>> s = Replace(s, ChrW(0), "")
>> WScript.Echo Len(s), s
>> Next
>>
14 XXX - AB - XXX
13 XXX - A - XXX
Further food for thought:
Option Explicit
Const csFSpec = ".\29988189.data"
Dim goFS : Set goFS = CreateObject("Scripting.FileSystemObject")
Dim sData : sData = "Danger" & ChrW(0) & " Will Robinson!"
Dim tsIO : Set tsIO = goFS.CreateTextFile(csFSpec)
tsIO.WriteLine sData
tsIO.Close
sData = goFS.OpenTextFile(csFSpec).ReadLine()
WScript.Echo Len(sData), Replace(sData, ChrW(0), ",")
Output:
cscript 29988189.vbs
22 Danger, Will Robinson!
Upvotes: 0
Reputation: 175826
That StrComp
behaviour is what you would see if there were a null character on the end of the string; this would also prevent the subsequent concatenation.
You can verify this by seeing if ascw(right(testVar, 1)) = 0
Chop it off: testVar = left(testVar, len(testVar) - 1)
Upvotes: 3