问题描述
我想检查文件中的数据是否存在,是否存在数组数据.如果退出,它将返回1和0.我的文件里面是这样的:
I would like to check a data in my file exist or not in an array data that I have. It will return 1 and 0 if its exit or not. Inside my file is like this:
2j2H4F6d9d0d3hdfasgt.y7
但是我删了最后两行.我的数组数据是这样的:[2w fr 5k 2j 0w].我想检查我的文件中是否存在数组数据.
But I cut the last 2 lines. And my array data is like this: [2w fr 5k 2j 0w]. I want to check whether my array data exist inside my file.
Const wbemFlagReturnImmediately = &h10
Const wbemFlagForwardOnly = &h20
XX = 0
Set wshShell = CreateObject("WScript.Shell")
strFBString = wshShell.ExpandEnvironmentStrings("%FB%")
WScript.Echo "==>"
WScript.Echo "strFBString: " & strFBString
Set wshShell = Nothing
For i = 1 To Len(strFBString) Step 2
If StrComp(Mid(strFBString, i, 2), [2w fr 5k 2j 0w]) = 0 Then
XX = 1
End If
Next
WScript.Echo "XX: " & XX
WScript.Quit(XX)
推荐答案
一方面,[2w fr 5k 2j 0w]
在VBScript中不是有效的数组定义.如果要使用这5个字符串元素定义数组,则需要这样做:
For one thing, [2w fr 5k 2j 0w]
is not a valid array definition in VBScript. If you want to define an array with these 5 string elements you need to do it like this:
Array("2w", "fr", "5k", "2j", "0w")
此外,StrComp()
用于将一个字符串与另一个字符串进行比较.它不支持将字符串与数组进行比较.为了将字符串与数组的每个元素进行比较,您需要一个循环.但是,如何建立该循环取决于您要获得的结果.
Also, StrComp()
is for comparing a string to another string. It does not support comparing a string to an array. For comparing a string to each element of an array you need a loop. How to build that loop depends on the result you want to achieve, though.
查看您的代码,似乎您想在2j2H4...
中找到一个匹配项,而不是在w2j2H...
中找到一个匹配项,因此简单地使用InStr()
可能对您不起作用.在这种情况下,您可以使用内部循环进行比较:
Looking at your code it seems you want to find a match in 2j2H4...
, but not in w2j2H...
, so simply using InStr()
probably won't work for you. In that case you could use an inner loop for the comparison:
ref = Array("2w", "fr", "5k", "2j", "0w")
For i = 1 To Len(strFBString) Step 2
For Each s In ref
If Mid(strFBString, i, 2) = s Then
'...
End If
Next
Next
但是正如我已经说过的那样,细节取决于所需的最终结果.如果要检查输入字符串是否包含数组值的 any ,可以执行以下操作:
But like I already said, details depend on the desired end result. If you want to check if your input string contains any of the array values you could do something like this:
ref = Array("2w", "fr", "5k", "2j", "0w")
found = False
For i = 1 To Len(strFBString) Step 2
For Each s In ref
If Mid(strFBString, i, 2) = s Then
found = True
Exit For
End If
Next
Next
另一方面,如果您要检查输入字符串是否包含参考字符串的 all ,则可能会执行以下操作:
If on the other hand you wanted to check if your input string contains all of the reference strings you'd probably do something like this instead:
ref = Array("2w", "fr", "5k", "2j", "0w")
For Each s In ref
found = False
For i = 1 To Len(strFBString) Step 2
If Mid(strFBString, i, 2) = s Then
found = True
Exit For
End If
Next
If Not found Then Exit For
Next
您还可以使用完全不同的方法,例如将数据放入字典中.
You could also use an entirely different approach, like putting your data in a dictionary:
data = CreateObject("Scripting.Dictionary")
For i = 1 To Len(strFBString) Step 2
data(Mid(strFBString, i, 2)) = True
Next
使用这种方法,您可以检查数据是否包含任何参考值,如下所示:
Using that approach you could check if the data contains any of the reference values like this:
found = False
For s In Array("2w", "fr", "5k", "2j", "0w")
If data.Exists(s) Then
found = True
Exit For
End If
Next
或检查数据是否包含所有像这样的参考值:
or check if the data contains all of the reference values like this:
found = True
For s In Array("2w", "fr", "5k", "2j", "0w")
If Not data.Exists(s) Then
found = False
Exit For
End If
Next
这篇关于如何使用VBScript比较数组与数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!