|
-
May 27th, 2002, 02:07 PM
#1
Thread Starter
Lively Member
newb string search question
i am confused about instr function. i understand it searches string2 and finds if string1 is present in it. the function is
instr(string1,string2)
it returns a true only if string1 = string2 . i.e. it does not return true if string1 is part of string2. is there any other function that does this job ? what am i missing
-
May 27th, 2002, 02:36 PM
#2
Here you go, here is how to use the Instr function. It returns a number indicating where it found the substring you were searching for. The first string in the arguement list is the string you want searched, the second is the string you want to find in the first string. The CompareMethod can be Text or Binary.
Code:
Dim x As Integer
x = InStr("This is the string you want searched", "searched", CompareMethod.Text)
If x > 0 Then
MessageBox.Show("Found substring 'searched' at position " & x)
End If
-
May 27th, 2002, 02:42 PM
#3
Thread Starter
Lively Member
how about if i want to count the number of times the string appears in the second string . i tried doing it like this but no use -
If InStr(lookupStr, str, CompareMethod.Text) = True Then
strCounter = strCounter + 1
End If
the if is in a loop reading streamreader. right now it is satisfied if lookupStr = str but that is not what i want. i want it true if lookupStr is in the str. any other way i can do this ?
-
May 27th, 2002, 06:58 PM
#4
Code:
Dim x As Integer
Dim iCount As Integer
Dim sStringToSearch As String
Dim sStringToSearchFor As String
sStringToSearch = "Hello, I am the string to search. I am making this long so I can get some words in it"
sStringToSearchFor = "I"
iCount = 0
'Set x to 1 so you can get in the loop. x will be the place to start
'the search inside the string.
x = 1
'Loop starting at the last position that the string was found,
'increasing the count by one each time one is found.
Do While x > 0
'Changing the CompareMethod to .Binary will yeld different results,
'it won't let i and I match like .Text will.
x = InStr(x, sStringToSearch, sStringToSearchFor, CompareMethod.Text)
If x > 0 Then
'Increase the start position by one
x += 1
'Increase the count by one.
iCount += 1
End If
Loop
MessageBox.Show("Found " & iCount.ToString() & " matches")
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|