16

How to check if text1 contains text2 using vb6 ?

Dim text1 as string
Dim text2 as string

text1 = "hello world I am Anas"
text2 = "Anas"

if (check if text2 is in text1) 'the result should be true or false
Joel Coehoorn
  • 399,467
  • 113
  • 570
  • 794
faressoft
  • 19,053
  • 44
  • 104
  • 146

4 Answers4

32

You can use InStr function like this:

Dim position As Integer

position = InStr(1, stringToSearch, stringToFind)

If position > 0 Then
  ' text is inside
Else
  ' text is not inide 
End If
StayOnTarget
  • 11,743
  • 10
  • 52
  • 81
Sarfraz
  • 377,238
  • 77
  • 533
  • 578
16

Use InStr:

If InStr(text1, text2) > 0 Then
Oded
  • 489,969
  • 99
  • 883
  • 1,009
  • 1
    +1 but why not link to the VB6 manual entry for Instr http://msdn.microsoft.com/en-us/library/aa445031(v=VS.60).aspx – MarkJ Dec 31 '10 at 17:05
5

This should do the trick:

if (InStr(text1, text2) > 0) 

Check http://msdn.microsoft.com/en-us/library/8460tsh1(v=vs.80).aspx for special cases (parameters are Nothing, empty strings etc.)

Larsbj
  • 28
  • 4
  • 14
2
RTM = InStr(1, text1,text2)

if RTM > 0 then debug.print "Text2 was found at position: "; RTM
horatio
  • 1,426
  • 8
  • 7