Is there any easy method for splitting text in VB.NET?
Is there any easy method for splitting text in VB.NET? (using a start and end string to grab whats in between?)
I do this all the time in JScript with the following:
<junk> <blah> <data>someData1</data> <data>someData2</data> <data>someData3</data> </blah> </junk>
var data = string.split('<data>')[1].split('</data>')[0];
would give me "someData1" by changing the [1] index to [2] would give me "someData2" very easy
for some reason this seems to be very difficult to achieve in VB.NET.
Here is a chunk of the actual HTML I'm dealing with:
<...malformed html>
<div style='font-size:10pt;font-family:Times;color:#000000;position:absolute;top:2731.068;left:48'>Total</div>
<div 开发者_JAVA技巧style='font-size:10pt;font-family:Times;color:#000000;position:absolute;top:2731.068;left:346.2141'>18,072.59</div>
<div style='font-size:10pt;font-family:Times;color:#000000;position:absolute;top:2731.068;left:444.3433'>100.00%</div>
<div style='font-size:10pt;font-family:Times;color:#000000;position:absolute;top:2731.068;left:567.1293'>21,687.11</div>
<div style='font-size:10pt;font-family:Times;color:#000000;position:absolute;top:2731.068;left:666.3433'>100.00%</div>
<malformed html...>
I need to find the <div>Total</div> index then grab the data between the 1st and 3rd divs after that.
Dim e = XElement.Parse(str)
Dim a = e.XPathSelectElements("./blah").Elements().ToArray()
a(0).Value 'someData1
a(1).Value 'someData2
EDIT: To parse html try using the Html Agility Pack
I got it working, although this is some of the worse code I've ever written...
Dim sr As StreamReader
sr = New StreamReader("C:\test.html")
Dim xactHTML As String = sr.ReadToEnd
Dim left As Integer = xactHTML.IndexOf("Total</div>")
Dim chunk1 As String = xactHTML.Substring(left + 12)
Dim right As Integer = chunk1.IndexOf("<div style='position")
Dim chunk2 As String = chunk1.Substring(0, right - 1)
Dim xHTML As String = "<xml>" & chunk2 & "</xml>"
Dim e = XElement.Parse(xHTML)
Dim a = e.Elements().ToArray()
Dim damageAmmount As String = a(2).Value()
精彩评论