【问题标题】:select random from target string VB.NET从目标字符串 VB.NET 中随机选择
【发布时间】:2019-05-06 19:49:42
【问题描述】:

我是怎么做到的

Source_Source.Text(富文本框):

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 asdsrc="other.com" a jksdh asksjd hasdjh src="found.com"

如果我想得到随机 src=" " 怎么办,例如,如果我点击 butten 将显示 message = src="test.com" ,如果我再次点击按钮将显示另一个随机,例如 src="another.com"

我目前的代码只选择第一个字符串 which = 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

【问题讨论】:

    标签: vb.net


    【解决方案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() & ""
    

    【讨论】:

    • 错误:未声明“Rand”。由于其保护级别,它可能无法访问。
    • 声明为公共共享
    【解决方案2】:

    如果我正确理解您的问题,您是在寻找字符串中的某个短语吗?如果是这样,请查看 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
    

    【讨论】:

    • 让我更清楚一点,如果我想从网页源获取随机链接,并且每天以 [src="] 开头,然后将其放入richtextbox,所以现在我想获取找到的随机 src 链接,每次点击。
    • 试试这个链接。 link 它使用 Random 类。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-01-12
    • 1970-01-01
    • 1970-01-01
    • 2012-02-03
    相关资源
    最近更新 更多