Maybe this is the simplest thing ever but i'm a beginner trying to learn at least 1 language.
How come the Script below keeps displaying the Else message until it finds the string? I was expecting for it to display 1 single Box whether it was found or not? why does it keep itterating through the Else condition?

Dim myArray,myString,myString2,intCount

myString = "my;string;question;hope;you;can;answer"
myString2 = "question"

myArray = split(myString,";")

For Each intCount In myArray
If intCount = myString2 Then
msgBox("String Found!!")
Else
msgBox("Sorry, String Not Found")
End If
Next

Dani AI

Generated

Short answer: the MsgBox is inside the loop, so it runs once for every array element. Each iteration either shows the "found" or "not found" box — that is exactly what the original code does.

was right to suggest a flag and exiting the loop early. One important clarification: the example in Post #4 looks like VB.NET syntax. In VBScript you use plain Dim and assign values separately. A typical VBScript approach is to set a Boolean flag, loop, set the flag and Exit For when you find a match, then show one message after the loop:

Dim myArray, myString, myString2, item, isFound
myString = "my;string;question;hope;you;can;answer"
myString2 = "question"
myArray = Split(myString, ";")
isFound = False

For Each item In myArray
    If StrComp(item, myString2, vbTextCompare) = 0 Then
        isFound = True
        Exit For
    End If
Next

If isFound Then
    MsgBox "String Found!!"
Else
    MsgBox "Sorry, String Not Found"
End If

Notes and troubleshooting: StrComp(..., vbTextCompare) makes the comparison case-insensitive; otherwise use LCase/UCase. Rename intCount to something like item or tokenFor Each yields elements, not an index. If you just need to know whether the substring exists in the whole string, InStr avoids splitting. Also remember that MsgBox in server-side ASP/console scripts won’t show a client dialog — use the appropriate output method for your environment.

Recommended Answers

All 4 Replies

Maybe this is the simplest thing ever but i'm a beginner trying to learn at least 1 language.
How come the Script below keeps displaying the Else message until it finds the string? I was expecting for it to display 1 single Box whether it was found or not? why does it keep itterating through the Else condition?

Dim myArray,myString,myString2,intCount

myString = "my;string;question;hope;you;can;answer"
myString2 = "question"

myArray = split(myString,";")

For Each intCount In myArray
If intCount = myString2 Then
msgBox("String Found!!")
Else
msgBox("Sorry, String Not Found")
End If
Next

I am totally expecting it to behave like that. The process will go like this.

  1. Compare intCount, which is "my" to "question". It is not equal, then show "Sorry, String Not Found".
  2. Compare intCount, which is "string" to "question". It is not equal, then show "Sorry, String Not Found".
  3. Compare intCount, which is "question" to "question". It is equal, then show "String Found!!".

The process will go on until you loop through all the element in myArray.

Ok, Thanks, So what would be the correct way for it to only show "String Found!!" or "Not Found" only Once?

You can use variable to check whether you can find matched string or not in your search process.

Dim isFound As Boolean = False

For Each intCount In myArray
   If intCount = myString2 Then
      isFound = True
      Exit For
   End If
Next

If isFound Then
   Msgbox("Found.")
Else
   Msgbox("Not found.")
End If

Hey,
Thanks a lot, that worked! Thanks for your help.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.