Link to home
Start Free TrialLog in
Avatar of WPCap
WPCap

asked on

VB.NET CUSTOM - check if string contains substring.

Hi there.

I have a string:  
word1 word2 word3120dafsog jsdgjidasggjd WordThatIneed ifahfiashd.

The string is always random and let's assume that I'm looking for word dThatI. Now I have a code that is looking for dThatI in the whole string, but how can I display where that "dThatI" was found.

Open in new window


For example the output should be: string "dThatI" was found in: WordThatIneed
Avatar of Kamaraj Subramanian
Kamaraj Subramanian
Flag of Singapore image

Dim WordThatIneed As String
WordThatIneed = "The string is always random and let's assume that I'm looking for word dThatI. Now I have a code that is looking for dThatI in the whole string, but how can I display where that "dThatI" was found."
If str.Contains("dThatI") = True Then
	MsgBox("string dThatI was found in: WordThatIneed")
Else
	MsgBox("string dThatI was not found in: WordThatIneed")
End If

Open in new window

change str.contains

to

WordThatIneed.contains
Avatar of WPCap
WPCap

ASKER

I think I wasn't clear enough in the topic.

Let's say I have string

str = "something here and here but here is the wordthatIneed"

now I need to first check if str contains dthati and if it does, then I need to know where "dthati" was found.

So the output should be:

dthati was found in str in a word: wordthatineed.
Ok for this, you nees to split the string and store into array. And loop the array contents and check with contains method
Avatar of WPCap

ASKER

I have no idea how to do that.
Let me give the code, once I reach home.,another 30 mins
ASKER CERTIFIED SOLUTION
Avatar of Nasir Razzaq
Nasir Razzaq
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Module Module1

    Sub Main()
        Dim str As String
        Dim strArr() As String
        str = "something here and here but here is the wordthatIneed"
        strArr = str.Split(" ")
        For count = 0 To strArr.Length - 1
            If (strArr(count).Contains("thatI")) Then
                MsgBox("thatI contains in " & strArr(count))
            End If
        Next
    End Sub

End Module

Open in new window