Hello programmers!

I have another easy problem. I want to write code that can find a word in the string, that contains another substring.

Code needs to get or just copy specific word in string. To get that word, this word need to contain some specific string (ex: "Platypus"). If some text has a word that says PerryThePlatypus, I want to get word PerryThePlatypus in my textbox or anywhere else.

Thanks.

Function getWord(ByVal myStr As String, ByVal subStr As String) As String
        Dim strWords As String() = myStr.Split(" ")
        Dim retWord As String = ""
        For Each word As String In strWords
            If word.Contains(subStr) Then
                retWord = word
            End If
        Next
        Return retWord
    End Function

Well using this you will get only one word from string.

It works.

Thanks! ;)

You can use regular expressions to step through every word. If you define a word as any string of one or more

  • lower case letter
  • upper case letter or
  • single quote

then to read a file and list all the words you can do

Dim text As String = My.Computer.FileSystem.ReadAllText(txtMyFile.Text)
Dim rex As New Regex("[a-z,A-Z,']+")

For Each m As Match In rex.Matches(text)
    Debug.WriteLine(m.Value)
Next

This will list all of the words, one per line. You should be able to check each word to see if it contains the substring. The expression, [a-z,A-Z,']+ matches one or more (the plus sign) of any combination of lower case letters (a-z), upper case letters (A-Z) or single quotes. I don't know if it is possible to have a word with more than one single quote, but it will match those as well. Note - you have to import System.Text.RegularExpressions

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.