1

Currently I'm using InStr to find a string in a string, I'm new to VB.NET and wondering if I can use InStr to search every element of an array in a string, or a similar function like this:

InStr(string, array)

Thanks.

5 Answers 5

4

You need to loop:

Dim bFound As Boolean = False
For Each elem As String In array
    If myString.Contains(elem) Then
        bFound = True
        Exit For
    End If
Next

You can transform it into a function to call it more than once easily:

Public Function MyInStr(myString As String, array() As String) As Boolean
    For Each elem As String In array
        If myString.Contains(elem) Then return True
    Next

    return false
End Function

Then:

MyInStr("my string text", New String() {"my", "blah", "bleh"})
Sign up to request clarification or add additional context in comments.

4 Comments

@XandrUu you have a typo there.should be Boolean
This is the code: Dim accesFile As Boolean and get error at As
it's asp-classic and find the issue it should say accesFile = False, but now I get an error in the For Each loop the sintax is "For Each file In Session("AccessFiles")"
There is not a "issue" there, Dim myVar As Type is completly correct in VB.NET so there is something strange in your platform. Maybe you are talking about javascript?
3

Here goes the LINQ solution:

Dim a() = {"123", "321", "132"}
Dim v = a.Select(Function(x) InStr(x, "3")).ToArray
MessageBox.Show(String.Join(",", v)) '3,1,2

2 Comments

I would recommend using x.IndexOf in VB.NET, rather than the old InStr function.
@StevenDoggart: Good thought. Fortunately, the above code support customization, so OP can use any function there.
2

Converting SysDragon's answer to classic asp:

You need to loop:

Dim bFound
bFound = False

For Each elem In myArray
    If InStr(myString, elem)>=0 Then
        bFound = True
        Exit For
    End If
Next

You can transform it into a function to call it more than once easily:

Function MyInStr(myString, myArray)
    Dim bFound
    bFound = false

    For Each elem In myArray
        If InStr(myString, elem)>=0 Then
            bFound = True
            Exit For
        End If
    Next

    MyInStr = bFound
End Function

Then:

MyInStr("my string text", Array("my", "blah", "bleh"))

Comments

1

If you are looking at searching for a string in any of the items in a string array, then you can use array.find(<T>) method. See more here: http://msdn.microsoft.com/en-IN/library/d9hy2xwa%28v=vs.90%29.aspx

Comments

0

Instr returns an integer specifying the start position of the first occurrence of one string within another.

Refer this

To find string in a string you can use someother method

Here is an example of highlighting all the text you search for at the same time but if that is not what you want, you have to solve it on your own.

Sub findTextAndHighlight(ByVal searchtext As String, ByVal rtb As RichTextBox)
Dim textEnd As Integer = rtb.TextLength
Dim index As Integer = 0
Dim fnt As Font = New Font(rtb.Font, FontStyle.Bold)
Dim lastIndex As Integer = rtb.Text.LastIndexOf(searchtext)
While (index < lastIndex)
  rtb.Find(searchtext, index, textEnd, RichTextBoxFinds.WholeWord)
  rtb.SelectionFont = fnt
  rtb.SelectionLength = searchtext.Length
  rtb.SelectionColor = Color.Red
  rtb.SelectionBackColor = Color.Cyan
  index = rtb.Text.IndexOf(searchtext, index) + 1
End While
End Sub

This method with search for text "boy" in RichTextBox2, change the textcolor to red and back color to cyan

 Private Sub Button5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button5.Click
 findTextAndHighlight("boy", RichTextBox2)   
 End Sub

3 Comments

It's better if you can include the examples and information directly in your answer. Providing a link in addition to that is great, but if a link is the only thing in your answer, if the link is ever broken, your answer will be useless to future visitors.
@Steven Doggart, thanks for your useful advice. I'll update my answer
This link appears to not work properly anymore. Also keep in mind that many security minded US based companies without international branches have a very high probability of pre-blocking non-USA based IP connections, so keep that in mind whomever reads this.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.