无法使用VBScript搜索给定字符串中的字符?

无法使用VBScript搜索给定字符串中的字符?,vbscript,Vbscript,我试图找出字符是否存在于给定字符串中,但无法搜索并增加值,尽管它存在 Dim testchar,noOfSpecialChar noOfSpecialChar=0 Dim specialChars specialChars="*[@.^$|?#*+!)(_=-]." for lngIndex = 1 to Len("test@123") testchar = mid("test@123",lngIndex,1) if((InStr(specialChars,test

我试图找出字符是否存在于给定字符串中,但无法搜索并增加值,尽管它存在

  Dim testchar,noOfSpecialChar
   noOfSpecialChar=0

Dim specialChars
specialChars="*[@.^$|?#*+!)(_=-]."
 for lngIndex = 1 to Len("test@123")
    testchar = mid("test@123",lngIndex,1)
    if((InStr(specialChars,testchar))) then
        noOfSpecialChar=noOfSpecialChar+1
end if  
next

这里的问题是突出显示的
InStr()

返回一个字符串在另一个字符串中第一次出现的位置

通过检查
InStr()
的返回值大于0,我们可以使用此知识创建布尔比较

Dim testString: testString = "test@123"
Dim testchar, foundChar
Dim noOfSpecialChar: noOfSpecialChar = 0
Dim specialChars: specialChars = "*[@.^$|?#*+!)(_=-]."

For lngIndex = 1 To Len(testString)
  testchar = Mid(testString, lngIndex, 1)
  'Do we find the character in the search string?
  foundChar = (InStr(specialChars, testchar) > 0)
  If foundChar Then noOfSpecialChar = noOfSpecialChar + 1   
Next

这里的问题是突出显示的
InStr()

返回一个字符串在另一个字符串中第一次出现的位置

通过检查
InStr()
的返回值大于0,我们可以使用此知识创建布尔比较

Dim testString: testString = "test@123"
Dim testchar, foundChar
Dim noOfSpecialChar: noOfSpecialChar = 0
Dim specialChars: specialChars = "*[@.^$|?#*+!)(_=-]."

For lngIndex = 1 To Len(testString)
  testchar = Mid(testString, lngIndex, 1)
  'Do we find the character in the search string?
  foundChar = (InStr(specialChars, testchar) > 0)
  If foundChar Then noOfSpecialChar = noOfSpecialChar + 1   
Next

代码是可靠的,除了
InStr()
返回字符位置,如果在将其用作布尔条件的位置找到字符位置(
True
False
)。如果将
If
更改为
If InStr(specialChars,testchar)>0,则
将为您提供所需的内容。代码是可靠的,除了
InStr()
返回字符位置,如果您将其用作布尔条件(
True
False
)。如果将
If
更改为
If InStr(specialChars,testchar)>0,则
将满足您的期望。