-2

example :

[Button click] Two Specified strings (<title>[...]</title>) Msgbox(Name of unspecified string(the string between the two title tags))

Because the String between the tags is Variating, i can't use (if a.contains(textbox1.text) = true Then [...])

Seby_Plays
  • 26
  • 5
  • You can use a string variable instead of the literal text "EXAMPLE". – Andrew Morton Dec 05 '19 at 16:18
  • ... or `"EXAMPLE".Contains(TextBox1.Text)` (btw, not `...Contains(TextBox1)`). Or, depending on the use case (IMO, not exactly clear what that is), use IndexOf() + LastIndexOf() or a Regex (e.g., `>(.*?)<`) – Jimi Dec 05 '19 at 16:23

2 Answers2

0

As per comment by @Jimi

Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
    Dim s = "<title>Hello World</title>"
    Dim startIndex = s.IndexOf(">") + 1
    Dim stringLength = s.LastIndexOf("<") - startIndex
    Dim titleText = s.Substring(startIndex, stringLength)
    Debug.Print(titleText)
End Sub

Immediate Window displays

Hello World

Mary
  • 14,926
  • 3
  • 18
  • 27
  • but "Hello world" isn't defined tho. Its variable from file to file. – Seby_Plays Dec 07 '19 at 14:39
  • This code will find any string between the 2 title tags. Hello World is just an example of what might be between the tags. – Mary Dec 08 '19 at 00:38
  • and what if, there is more than one tag in the file. I mean, ill use it to search a whole html file with that program. – Seby_Plays Dec 10 '19 at 15:51
  • That is beyond the scope of your original question. Ask a new question with an example of the string you are trying to parse. – Mary Dec 10 '19 at 16:10
0

You could use Xml Serialization, because your text looks like Xml...

Public Class Form1
    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Dim s As New System.Xml.Serialization.XmlSerializer(GetType(Title))
        Dim t As Title
        Using sr As New System.IO.StringReader("<title>[...]</title>")
            t = CType(s.Deserialize(sr), Title)
        End Using
        MessageBox.Show(t.UnspecifiedString)
    End Sub
End Class

<System.Xml.Serialization.XmlRoot("title")>
Public Class Title
    <System.Xml.Serialization.XmlText>
    Public Property UnspecifiedString As String
End Class

Ok, maybe it's overkill if this is all you are doing, but I suspect <title> is just one node in an Xml document. Then it might be worth defining the entire model in VB and deserializing the whole file. You can see some of my other recent answers related to this below

How to deserialize 5-6 level tags using VB.NET

Write and read data from XML file through VB.Net at launch

Retrieve specific part of XML tree value

djv
  • 15,168
  • 7
  • 48
  • 72