VBScript问题 [英] VBScript Question
问题描述
你好
我意识到这并非相当",属于这里. 但是,这是我在这里可以找到的唯一VB-ish论坛.我正在尝试编写可在经典ASP/VBScript中使用的异常证明转换函数.我已经拿出了代码,但我不明白 代码具有的行为. 出于某种原因,当我遍历输入字符串并测试以查看我所在的字符是否为数字时,它总是在Chr(57)(数字为9位)上返回true,因此什么也没有从我的输入中过滤掉 细绳.此外,当循环确实在我的测试字符串中遇到数字时,当我针对我的Chhr()循环测试当前字符并且结果字符串中有几百个数字时,它在所有256个测试中均测试为true.>
从逻辑上讲,这种行为对我来说没有任何意义. 如果有人可以向我解释为什么会这样,或者我还能做些什么来实现这一点,那就太好了.
这是我的函数代码:
Function CIntSafe(input) Dim ret If IsNull(input) Then ret = -1 Else If IsEmpty(input) Then ret = -1 Else If IsObject(input) Then ret = -1 Else If IsArray(input) Then ret = -1 Else If Len(input) = 0 Then ret = -1 Else Dim typeResult typeResult = VarType(input) If typeResult = 2 Or typeResult = 3 Or typeResult = 4 Or typeResult = 5 Then 'sees if input is of types integer, long, single or double ret = CInt(input) Else Dim inputLength, i Dim chrMin, chrMax, curChr, midResult Dim inputCopy inputLength = Len(input) chrMin = 0 chrMax = 255 inputCopy = "" For i = 1 To inputLength For curChr = chrMin To chrMax midResult = Mid(input, i, 1) If midResult = Chr(48) Or midResult = Chr(49) Or midResult = Chr(50) Or midResult = Chr(51) Or midResult = Chr(52) Or midResult = Chr(53) Or midResult = Chr(54) Or midResult = Chr(55) Or midResult = Chr(56) Or curChr = 57 Then 'Chr(48) through Chr(57) is the numbers 0123456789 inputCopy = inputCopy & midResult End If Next Next ret = inputCopy End If End If End If End If End If End If CIntSafe = ret End Function
这是我的电话专线:
Response.Write(CIntSafe("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890!@#$%^&*()-=_+[]\{}|;':"",./<>?"))
这是结果:
<跨度风格= 字体-family:宋体,时报,宋体,黑体,无衬线;字体大小:10px的;颜色:#666666"> abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222233333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333334444444444444444444444444444444444444444444444444444444444444444444444444444444 4444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444445555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666677777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777778888888888888888888888888888888888888888888888888888888 !88888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888890000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 @#$%^&安培; *() - = _ + [] \ {} |;':?",/<> 跨度>
谢谢!
好吧,我在If语句上看到一个错字,在这里我可以测试一下midResult是什么.现在,不再复制任何非数字. 然而,数字字符仍会复制到结果中数百次. 更改If语句后进行检查 看看嵌套的For循环中是否midResult = Chr(57),这是同一输入的新结果:
<跨度风格= 字体-family:宋体,时报,宋体,黑体,无衬线;字体大小:10px的;颜色:#666666"> 11111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111112222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333344444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444 4444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444455555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555556666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777788888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888 8888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888899999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999990000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 跨度>
Hello,
I realize this doesn't "quite" belong here. However, it's the only VB-ish forum I could find here. I'm trying to write exception proof conversion functions to use in Classic ASP/VBScript. I've got the code out but I don't understand the behavior the code is having. For some reason when I'm walking through the input string and testing to see if the character I'm on is a number or not, it always returns true on Chr(57), the number 9 digit, and thus, nothing gets filtered out of my input string. Furthermore, when the loop does encounter a digit in my test string it tests true on all 256 tests when I test the current character against my Chr() loop and I have several hundred digits in my result string.
This behavior doesn't logically make sense to me. If someone could explain to me why that is or what else I can do to accomplish that'd be great.
Here is the code to my function:
Function CIntSafe(input) Dim ret If IsNull(input) Then ret = -1 Else If IsEmpty(input) Then ret = -1 Else If IsObject(input) Then ret = -1 Else If IsArray(input) Then ret = -1 Else If Len(input) = 0 Then ret = -1 Else Dim typeResult typeResult = VarType(input) If typeResult = 2 Or typeResult = 3 Or typeResult = 4 Or typeResult = 5 Then 'sees if input is of types integer, long, single or double ret = CInt(input) Else Dim inputLength, i Dim chrMin, chrMax, curChr, midResult Dim inputCopy inputLength = Len(input) chrMin = 0 chrMax = 255 inputCopy = "" For i = 1 To inputLength For curChr = chrMin To chrMax midResult = Mid(input, i, 1) If midResult = Chr(48) Or midResult = Chr(49) Or midResult = Chr(50) Or midResult = Chr(51) Or midResult = Chr(52) Or midResult = Chr(53) Or midResult = Chr(54) Or midResult = Chr(55) Or midResult = Chr(56) Or curChr = 57 Then 'Chr(48) through Chr(57) is the numbers 0123456789 inputCopy = inputCopy & midResult End If Next Next ret = inputCopy End If End If End If End If End If End If CIntSafe = ret End Function
Here is my call line:
Response.Write(CIntSafe("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890!@#$%^&*()-=_+[]\{}|;':"",./<>?"))
Here is the result:
abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222233333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333334444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555566666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666667777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888890000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000!@#$%^&*()-=_+[]\{}|;':",./<>?
Thanks!
Okay, I saw a typo on my If statement where I test to see what midResult is. Now, no more non-digits get copied. However, the digit characters are still copied into the result several hundred times. After changing the If statement to check to see if midResult = Chr(57) within the nested For loops, this is the new result of the same input:
1111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222233333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333334444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555566666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666667777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888899999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999990000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
这篇关于VBScript问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!