简体   繁体   English

查找单词在字符串中的位置

[英]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 str()As String = Split(checkin,“”)
  • Dim Position As Integer 昏暗的位置作为整数
  • Position =Find Position of str(0) in the string Position =查找str(0)位置
  • Check str(1) with the next word after position th word in checkin 检查str(1)与下一个单词后position日在字checkin
  • if not equal continue the second step with str(1) 如果不相等,请继续执行第二步str(1)
  • if Dim valueSearch As String="to find a game" then i have to display a message that "to find a" is present if Dim valueSearch As String="to find a game" then我必须显示一条消息,指出存在"to find a"

My question is that is it possible to find the position of a word in a string using 我的问题是,是否有可能使用以下命令在字符串中找到单词的位置

string.Contains() operation. string.Contains()操作。 or any other possibility to implement this algorithm? 或其他实现此算法的可能性?

Try Like this 像这样尝试

Create recursive Method. 创建递归方法。 Use String.Contains. 使用String.Contains。 Its true then return else continue the method. 然后返回true,否则继续该方法。

   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")

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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