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

0
Kamaraj SubramanianApplication Support AnalystCommented:
change str.contains

to

WordThatIneed.contains
0
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.
0
Cloud Class® Course: SQL Server Core 2016

This course will introduce you to SQL Server Core 2016, as well as teach you about SSMS, data tools, installation, server configuration, using Management Studio, and writing and executing queries.

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
0
WPCapAuthor Commented:
I have no idea how to do that.
0
Kamaraj SubramanianApplication Support AnalystCommented:
Let me give the code, once I reach home.,another 30 mins
0
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
0

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

0
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.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.