Jump to content

Ajuda com Split


tsousa

Recommended Posts

Boa noite.

Estou a criar um programa que vai buscar automaticamente os números do euromilhões, mas estou com um impasse, não consigo filtrar os números da string.

Este é o meu código até agora.

Imports System.Xml
Public Class Form1

    Private Sub Button4_Click(sender As Object, e As EventArgs) Handles Button4.Click
        Dim results(0 To 10) As String
        Dim arrpos As Integer = 0
        Dim milhao As String = "Nada"
        ListBox4().Items.Clear()
        Dim xr As XmlReader = XmlReader.Create("https://www.jogossantacasa.pt/web/SCRss/rssFeedCartRes")
        Do While xr.Read()
            If xr.NodeType = XmlNodeType.Element AndAlso xr.Name = "description" Then
                results(arrpos) = (xr.ReadElementString)
                ListBox4.Items.Add(arrpos & "º " & results(arrpos))
                arrpos += 1
            Else
                xr.Read()
            End If
        Loop
        TextBox2.Text = results(2)
        TextBox3.Text = results(4)
    End Sub
End Class

Eu consegui por isto a devolver-me só a string pretendida <b>Sorteio nº 094/2016</b>: 5 10 33 34 47 + 2 10 mas queria só a parte dos números 5 10 33 34 47 + 2 10.

Quando uso um split dá-me o erro "Value of type 'String()' cannot be converted to 'String'".

Será que alguém me poderia dar uma ajuda?

Obrigado 

Tiago Sousa

Link to comment
Share on other sites

Boas,

a devolver num array de integers:


 

Imports System.Xml

Public Class Form1

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim results(0 To 10) As String
        Dim arrpos As Integer = 0
        Dim milhao As String = "Nada"
        ListBox4().Items.Clear()
        Dim xr As XmlReader = XmlReader.Create("https://www.jogossantacasa.pt/web/SCRss/rssFeedCartRes")
        Do While xr.Read()
            If xr.NodeType = XmlNodeType.Element AndAlso xr.Name = "description" Then
                results(arrpos) = (xr.ReadElementString)
                ListBox4.Items.Add(arrpos & "º " & results(arrpos))
                arrpos += 1
            Else
                xr.Read()
            End If
        Loop
        TextBox2.Text = results(2)
        TextBox3.Text = results(4)

        Dim numbers() As Integer = GetNumbers(results(2))
        If numbers.Length = 7 Then
            For Each n As Integer In numbers
                MessageBox.Show(n.ToString())
            Next
        Else
            MessageBox.Show("problemas!")
        End If

    End Sub


    Private Function GetNumbers(ByVal str As String) As Integer()
        Dim strArray() As String = str.Split(New String() {"</b>:"}, StringSplitOptions.None)
        If strArray.Length = 2 Then
            Dim strNumbers As String = strArray(1).Replace("+ ", "").Trim()
            Return Array.ConvertAll(strNumbers.Split(" "), AddressOf Int32.Parse)
        End If
        Return New Integer() {}
    End Function

End Class
  • Vote 1
Link to comment
Share on other sites

Tentaste fazer? É quase igual mas mais simples ainda...

 

no botão:

Dim m1lhao As String = GetMilhao(results(4))
If Not String.IsNullOrEmpty(m1lhao) Then
    MessageBox.Show(m1lhao)
Else
    MessageBox.Show("problemas!")
End If

 

a função:

    Private Function GetMilhao(ByVal str As String) As String
        ' divide a string quando acha a cadeia de caracteres "</b>:"
        Dim strArray() As String = str.Split(New String() {"</b>:"}, StringSplitOptions.None)
        ' verifica se o array tem dois elementos
        If strArray.Length = 2 Then
            ' retorna o conteudo do ultimo elemento do array (podias usar "strArray.Last()")
            ' o "trim()" remove os spaces " " das pontas
            Return strArray(1).Trim()
        End If
        Return String.Empty
    End Function
Edited by vikcch
  • Vote 1
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...

Important Information

By using this site you accept our Terms of Use and Privacy Policy. We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.