check if a string contains another string

rbreidenstein

Member
Joined
Dec 4, 2005
Messages
21
Programming Experience
1-3
I want to be able to say this:

if stringA contains stringB then
do something
else
do something different
end if



stinga will have many words in it and stringb may have one or many.
 
Ok i don't get the idea but however you can pass all words in arrays and later iterate through the arrays:
VB.NET:
[SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] strA [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]String[/COLOR][/SIZE][SIZE=2] = "Something else but still Word word"
[/SIZE][SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] strB [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]String[/COLOR][/SIZE][SIZE=2] = "Word word"
[/SIZE][SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] myArray1() [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]String[/COLOR][/SIZE][SIZE=2] = strA.Split(" ")
[/SIZE][SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] myArray2() [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]String[/COLOR][/SIZE][SIZE=2] = strB.Split(" ")
[/SIZE][SIZE=2][COLOR=#0000ff]For[/COLOR][/SIZE][SIZE=2] i [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Integer[/COLOR][/SIZE][SIZE=2] = 0 [/SIZE][SIZE=2][COLOR=#0000ff]To[/COLOR][/SIZE][SIZE=2] myArray1.GetUpperBound(0)
[/SIZE][SIZE=2][COLOR=#0000ff]  For[/COLOR][/SIZE][SIZE=2] j [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Integer[/COLOR][/SIZE][SIZE=2] = 0 [/SIZE][SIZE=2][COLOR=#0000ff]To[/COLOR][/SIZE][SIZE=2] myArray2.GetUpperBound(0)
[/SIZE][SIZE=2][COLOR=#0000ff]     If[/COLOR][/SIZE][SIZE=2] myArray1(i).StartsWith(myArray2(j)) [/SIZE][SIZE=2][COLOR=#0000ff]Then
[/COLOR][/SIZE][SIZE=2]       MessageBox.Show(myArray1(i))
[/SIZE][SIZE=2][COLOR=#0000ff]    End [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]If
[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff] Next
[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Next[/COLOR][/SIZE]

HTH
Regards ;)
 
That almost worked, but it doesn't work it i'm looking for a line with the characters bcd in it if bcd are in the string abcdef. any suggestions?
 
But in your original thread you are asking about finding "words".
However, it seems we've been very near to the solution.
Ok, now you can try to pass all chars inside 1st array without using split method.
Hmm ... actually maybe it is better idea if you proceed 1st string to RichTextBox control as you will have more available methods and functions for manipulating strings.
Let me know if you can't do it by yourself and i'll help you additionally.
Btw, please explain what is the idea of this application?

Regards ;)
 
i am using wrurlget which is passing a webpage html which i want to sort through and get only the info i want out of it.



Dim sURL As String
sURL = "http://www.cnn.com"
Dim wrGETURL As WebRequest
wrGETURL = WebRequest.Create(sURL)

Dim myProxy As New WebProxy("myproxy", 80)
'myProxy.BypassProxyOnLocal = True

'wrGETURL.Proxy = myProxy
'wrGETURL.Proxy = WebProxy.GetDefaultProxy()

Dim objStream As Stream
objStream = wrGETURL.GetResponse.GetResponseStream()
Dim objReader As New StreamReader(objStream)
Dim sLine As String = ""
Dim i As Integer = 0
Do While Not sLine Is Nothing
i += 1
sLine = objReader.ReadLine
If Not sLine Is Nothing Then
End If
Next
Next


End If
Loop
 
here is how it was used, it ran through each line on the code and if the string with the line in it matched it listed that string in a different listbox.


Dim sURL As String
sURL = "http://www.cnn.com"

Dim wrGETURL As WebRequest
wrGETURL = WebRequest.Create(sURL)

Dim myProxy As New WebProxy("myproxy", 80)
'myProxy.BypassProxyOnLocal = True

'wrGETURL.Proxy = myProxy
'wrGETURL.Proxy = WebProxy.GetDefaultProxy()

Dim objStream As Stream
objStream = wrGETURL.GetResponse.GetResponseStream()
Dim objReader As New StreamReader(objStream)
Dim sLine As String = ""
Dim i As Integer = 0
Do While Not sLine Is Nothing
i += 1

sLine = objReader.ReadLine
If Not sLine Is Nothing Then
If LCase(sLine).Contains(LCase("cnn")) = True Then
ListBox1.Items.Add(sLine)

End If
lstOutPut.Items.Add(sLine)
End If
Loop
 
i have VS2003 and this is how i do it:
VB.NET:
    Friend Function StrContains(ByVal Text As String, ByVal SearchText As String, ByVal CaseSensitive As Boolean) As Boolean
        For intCounter As Integer = 1 To Text.Length
            If CaseSensitive = True Then
                If Mid(Text, intCounter, SearchText.Length) = SearchText Then Return True
            Else
                If Mid(Text, intCounter, SearchText.Length).ToLower = SearchText.ToLower Then Return True
            End If
        Next intCounter
        Return False
    End Function
it's simple, checks for CaseSensitivity and whatnot, returns a boolean
 
Hi Juggalo,
Where you found this function? I am asking cuz i beleive it's too old function as it using Mid function for string manupulation instead .Substring method that is vb.net equivalent.
However, i beleive that M$ has done good job implementing "contains" member to string class.

Regards ;)
 
i found it in the back of my head, i just typed it into windows notepad right before i copy and pasted it into the forum's quick reply box and clicked post quick reply

IMO Mid is still a useful function only because i'm not used to using .SubString but either way works the same so it's not a big deal
 
Back
Top