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
WPCapAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Kamaraj SubramanianApplication Support AnalystCommented:
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

Kamaraj SubramanianApplication Support AnalystCommented:
change str.contains

to

WordThatIneed.contains
WPCapAuthor Commented:
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.
Exploring ASP.NET Core: Fundamentals

Learn to build web apps and services, IoT apps, and mobile backends by covering the fundamentals of ASP.NET Core and  exploring the core foundations for app libraries.

Kamaraj SubramanianApplication Support AnalystCommented:
Ok for this, you nees to split the string and store into array. And loop the array contents and check with contains method
WPCapAuthor Commented:
I have no idea how to do that.
Kamaraj SubramanianApplication Support AnalystCommented:
Let me give the code, once I reach home.,another 30 mins
CodeCruiserCommented:
Try following


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

Dim words As String() = str.Split(" ")
for each word in words
   if word.contains(yourword) Then
      msgbox(yourword & " was found in " & word)
   End If
Next

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Kamaraj SubramanianApplication Support AnalystCommented:
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

It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic.NET

From novice to tech pro — start learning today.