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.


5 Years
Discussion Span
Last Post by Reverend Jim
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
        Return retWord
    End Function

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


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)

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

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.