簡體   English   中英

查找單詞在字符串中的位置

[英]Find position of a word in a string

我必須按照以下方法實施搜索,

讓:

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

現在要實現的算法是:

  • Dim str()As String = Split(checkin,“”)
  • 昏暗的位置作為整數
  • Position =查找str(0)位置
  • 檢查str(1)與下一個單詞后position日在字checkin
  • 如果不相等,請繼續執行第二步str(1)
  • if Dim valueSearch As String="to find a game" then我必須顯示一條消息,指出存在"to find a"

我的問題是,是否有可能使用以下命令在字符串中找到單詞的位置

string.Contains()操作。 或其他實現此算法的可能性?

像這樣嘗試

創建遞歸方法。 使用String.Contains。 然后返回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