简体   繁体   中英

Find position of a word in a string

I have to implement a search in following method,

Let :

Dim checkin As String = "This is the base string, i have to find a word here"
Dim valueSearch  As String="to find a word"

Now the algorithm to be implemented is:

  • Dim str() As String = Split(checkin , " ")
  • Dim Position As Integer
  • Position =Find Position of str(0) in the string
  • Check str(1) with the next word after position th word in checkin
  • if not equal continue the second step with str(1)
  • if Dim valueSearch As String="to find a game" then i have to display a message that "to find a" is present

My question is that is it possible to find the position of a word in a string using

string.Contains() operation. or any other possibility to implement this algorithm?

Try Like this

Create recursive Method. Use String.Contains. Its true then return else continue the method.

   private Sub Recursive(ByVal xStr As String, ByVal xSearch As String)

           If xStr.Contains(xSearch) Then
                MsgBox(xSearch)
            ElseIf xSearch.Contains(" ") Then
                Recursive(xStr, xSearch.Substring(0, xSearch.LastIndexOf(" ")))
            Else
                MsgBox("Not Founded")
            End If

        End Sub

 Call Recursive("This is the base string, i have to find a word here", 
 "to find a game")

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM