Motivational Quote Selector in Visual Basic
Hi, I got error upon running your program:
{"Index was outside the bounds of the array."}
Imports System.Net, System.IO, System.Text.RegularExpressions
Private Function GetBetweenAll(ByVal Source As String, ByVal Str1 As String, ByVal Str2 As String) As String() Dim Results, T As New List(Of String) T.AddRange(Regex.Split(Source, Str1)) T.RemoveAt(0) For Each I As String In T Results.Add(Regex.Split(I, Str2)(0)) Next Return Results.ToArray End Function Private Function GetBetween(ByVal Source As String, ByVal Str1 As String, ByVal Str2 As String, Optional ByVal Index As Integer = 0) As String Return Regex.Split(Regex.Split(Source, Str1)(Index + 1), Str2)(0) End Function
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load Dim req As HttpWebRequest = HttpWebRequest.Create("http://www.forbes.com/sites/kevinkruse/2013/05/28/inspirational-quotes/") Dim res As HttpWebResponse = req.GetResponse() Dim src As String = New StreamReader(res.GetResponseStream()).ReadToEnd()
Dim container As String = getbetween(src, "<ol>", "</ol>")
Dim quotes As String() = getbetweenall(container, "<li>", "</li>")
Dim random As Random = New Random() MsgBox(replaceHTMLChars(quotes(random.Next(quotes.Count))))
Me.Close() End Sub
Private Function replaceHTMLChars(ByVal quote As String) Dim ret As String = quote If (ret.Contains(" ")) Then ret.Replace(" ", vbTab) If (ret.Contains("‐")) Then ret.Replace("‐", "-") If (ret.Contains("£")) Then ret.Replace("£", "£") Return ret End Function