从目标字符串VB.NET

时间:2019-05-06 19:49:43

标签: vb.net

我怎么做

Source_Source.Text(richtextbox):

  

src="test.com" akalsjdjalksdv src="another.com" asdbnmslkbjcxv   asdas as danms d amsn asdasd src="cold.com" asdas as dasd amnbs dma d sdf kjhds f src="find.com" asd kja sdasjhk d asd src="other.com" a jksdh asksjd hasdjh src="found.com"

如果我想获得随机的src =“”,例如,如果我单击butten将显示消息= src="test.com",那么如果我单击按钮将再次显示另一个随机值,例如src="another.com",该怎么办?

我当前的代码仅选择= src="test.com"的第一个字符串,而我想选择随机数src="[random test / cold / other / found / find]"

例如,我的下次点击可以显示src="find.com"的消息,例如。

我的代码:

 Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    Dim sSource As String = Source_Source.Text 'String that is being searched
    Dim sDelimStart As String = Search_1.Text 'First delimiting word
    Dim sDelimEnd As String = Search_2.Text  'Second delimiting word
    Dim nIndexStart As Integer = sSource.IndexOf(sDelimStart) 'Find the first occurrence of f1
    Dim nIndexEnd As Integer = sSource.IndexOf(sDelimEnd, nIndexStart + sDelimStart.Length + 1) 'Find the first occurrence of f2

    If nIndexStart > -1 AndAlso nIndexEnd > -1 Then '-1 means the word was not found.
        Dim res As String = Strings.Mid(sSource, nIndexStart + sDelimStart.Length + 1, nIndexEnd - nIndexStart - sDelimStart.Length) 'Crop the text between
         MessageBox.Show(res) 'Display
    Else
        MessageBox.Show("One or both of the delimiting words were not found!")
    End If
End Sub

2 个答案:

答案 0 :(得分:1)

Private Function RndUrl() As String
    Dim UrlStr() As String = {"whatever.com", "whatever2.com"}
    Return Rand.Next(0, UrlStr.Length) '"Rand" Declared at Class scope as Random
End Function

用法:

    TextBox1.Text = "src=""" & RndUrl() & ""

答案 1 :(得分:0)

如果我正确理解了您的问题,那么您正在寻找字符串中的某个短语?如果是这样,请查看String.Contains()方法。 例如:

If Source_Source.Text.Contains(Search_1.Text) Then
    ' Do you're logic here if found
    Source_Source.Text.SubString(0, 5) ' Or where ever that part of the string is that you want.
End If